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

module = None optional import shows error locally but not from tox #10512

Closed
davidism opened this issue May 20, 2021 · 2 comments
Closed

module = None optional import shows error locally but not from tox #10512

davidism opened this issue May 20, 2021 · 2 comments
Labels
bug mypy got something wrong

Comments

@davidism
Copy link

davidism commented May 20, 2021

In Flask, we have the following lines:

try:
    import dotenv
except ImportError:
    dotenv = None

Running mypy shows an error:

src/flask/cli.py:25: error: Incompatible types in assignment (expression has type "None", variable has type Module)

However, adding a # type: ignore comment, then running mypy through tox shows an error about the ignore:

src/flask/cli.py:25: error: unused 'type: ignore' comment

Running tox without the comment shows everything passing.

So we're left in the situation where we need to pass in CI, but if anyone runs mypy locally they see this error, fix it, then CI fails for them.

What's extra strange is that right below the failing code is another optional import, with the ignore comment, that passes both locally and through tox.

try:
    import ssl
except ImportError:
    ssl = None  # type: ignore
@davidism davidism added the bug mypy got something wrong label May 20, 2021
@hauntsaninja
Copy link
Collaborator

Duplicate of #8990 (comment) and #8823
ssl = cast(Any, None) should be a workaround, if you need an optional type inferred then you could alias and assign or use if TYPE_CHECKING to lie. Some more discussion in the linked issues.

@davidism
Copy link
Author

davidism commented May 20, 2021

Thanks. I want to make sure we're not doing anything incorrect with our configuration or testing though, it's not clear why it fails with mypy but not with tox -e typing. Same Python and mypy versions in both cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

2 participants