Skip to content

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

Closed
@davidism

Description

@davidism

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions