Skip to content

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

Closed

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'

Activity

  1. added a commit that references this issue on Mar 7, 2018
    85129b1
  2. added a commit that references this issue on Mar 8, 2018
    0b7597c
  3. added a commit that references this issue on Mar 15, 2018
    e410bff
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions