Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use mypy-friendly conditional import for zoneinfo #50444

Merged
merged 5 commits into from
May 14, 2021

Conversation

KapJI
Copy link
Member

@KapJI KapJI commented May 11, 2021

Proposed change

After #50387 mypy fails locally for me with:

homeassistant/util/dt.py:12: error: Name 'zoneinfo' already defined (possibly by an import)  [no-redef]

That's probably because I use Python 3.9 locally. My idea is that CI didn't catch it, because zoneinfo is marked as Python 3.9+ and the first import was just ignored.

I think it's still nice to support Python 3.9 before CI is switched to it.

This is a known issue of mypy and using sys.version_info is suggested workaround: python/mypy#1153

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • New feature (which adds functionality to an existing integration)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

  • This PR fixes or closes issue: fixes #
  • This PR is related to issue:
  • Link to documentation pull request:

Checklist

  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • The code has been formatted using Black (black --fast homeassistant tests)
  • Tests have been added to verify that the new code works.

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • The manifest file has all fields filled out correctly.
    Updated and included derived files by running: python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • Untested files have been added to .coveragerc.

The integration reached or maintains the following Integration Quality Scale:

  • No score or internal
  • 🥈 Silver
  • 🥇 Gold
  • 🏆 Platinum

To help with the load of incoming pull requests:


import ciso8601

from homeassistant.const import MATCH_ALL

if sys.version_info[:2] >= (3, 9):
import zoneinfo # pylint: disable=import-error
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cdce8p if you want to improve pylint a little bit more.

Mypy understands blocks inside sys.version_info but pylint doesn't. But on the other side pylint doesn't complain about

try:
  import x
except ImportError
  import y

while mypy has this issue.

It'd be nice to support sys.version_info in pylint as well.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could use the ignored-modules option, documentation or wait for pylint-dev/pylint#4468 😃

@KapJI
Copy link
Member Author

KapJI commented May 13, 2021

@balloob as you authored migration to zoneinfo. What do you think?
This is the only place which doesn't pass mypy+python3.9 and this change doesn't look like a big deal.

@balloob balloob merged commit de54724 into home-assistant:dev May 14, 2021
@balloob
Copy link
Member

balloob commented May 14, 2021

Thanks for addressing this. I was already stuck with it hence I used the type ignore.

@github-actions github-actions bot locked and limited conversation to collaborators May 15, 2021
@KapJI KapJI deleted the mypy-py39 branch May 17, 2021 22:19
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants