Thursday, 17 March 2022

Poetry+mypy ignoring importing modules while poetry+python works fine

I've definitely botched my python package setup in a way that is failing to properly import packages with mypy despite running fine with python itself, but I can't quite tell how or what the solution is. I've been able to repro the problem with a pretty minimal approach.

The Setup

poetry new foo
cd foo
poetry add mypy@latest
poetry install
poetry shell

then create the following file structure:

pyproject.toml
poetry.lock
    foo/
        __init__.py
        a.py
        bar/
            __init__.py
            b.py
# a.py
from foo.bar import b
will_fail: int = "a.py string"
print(will_fail)

# b.py
will_fail: int = "b.py string"
print(will_fail)

As expected running python foo/a.py executes both a.py and b.py and prints both statements. And running mypy foo gives the expected errors:

foo/bar/b.py:1: error: Incompatible types in assignment (expression has type "str", variable has type "int")
foo/a.py:3: error: Incompatible types in assignment (expression has type "str", variable has type "int")
Found 2 errors in 2 files (checked 4 source files)

The Problem

But running mypy foo/a.py only gives:

foo/a.py:3: error: Incompatible types in assignment (expression has type "str", variable has type "int")
Found 1 error in 1 file (checked 1 source file)

And running mypy foo/a.py --follow-imports=error gives:

foo/a.py: error: Ancestor package "foo" ignored
foo/a.py: note: (Using --follow-imports=error, submodule passed on command line)
foo/a.py:1: error: Import of "foo.bar.b" ignored
foo/a.py:1: note: (Using --follow-imports=error, module not passed on command line)
foo/a.py:1: error: Import of "foo.bar" ignored
foo/a.py:3: error: Incompatible types in assignment (expression has type "str", variable has type "int")
Found 4 errors in 1 file (checked 1 source file)

The Question

So why is mypy not following into the modules and type checking them as they are imported?

Am I making some critical mistake in how I'm using poetry? mypy? python?

I've read the mypy docs a million times (https://mypy.readthedocs.io/en/stable/running_mypy.html#how-mypy-handles-imports) and it seems like what's happening is some "fourth" option from the 3 described there?



from Poetry+mypy ignoring importing modules while poetry+python works fine

No comments:

Post a Comment