Skip to content

Legal Python circular imports result in loss of type information #4429

Closed
@rowillia

Description

@rowillia

Example:

test/__init__.py:

import typing

from . import a
from . import b

if typing.TYPE_CHECKING:
        reveal_type(a.foo())

test/a.py:

import test

def foo() -> int:
    return test.b.x

test/b.py:

import test
import typing
x = 42

def run() -> int:
    if typing.TYPE_CHECKING:
        reveal_type(test.a.foo())
    return test.a.foo()

test/__main__.py

import test

print(test.b.run())

Python will execute the above code just fine:

$ python3 -m test
42

But mypy loses the type information for test.a in the context of b.py

$ mypy test/__init__.py test/a.py test/b.py
test/b.py:7: error: Revealed type is 'Any'
test/__init__.py:7: error: Revealed type is 'builtins.int'

What's odd is if I reorder the files to explicitly pass in b.py first, things work -

$ mypy test/b.py test/a.py test/__init__.py
test/__init__.py:7: error: Revealed type is 'builtins.int'
test/b.py:7: error: Revealed type is 'builtins.int'

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions