Skip to content

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

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

Closed
rowillia opened this issue Jan 3, 2018 · 0 comments · Fixed by #4695
Closed

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

rowillia opened this issue Jan 3, 2018 · 0 comments · Fixed by #4695
Labels

Comments

@rowillia
Copy link
Contributor

rowillia commented Jan 3, 2018

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'
@emmatyping emmatyping added bug mypy got something wrong topic-import-cycles labels Jan 4, 2018
JukkaL added a commit that referenced this issue Mar 7, 2018
JukkaL added a commit that referenced this issue Mar 8, 2018
This adds supports for some cases of importing an imported name
within an import cycle. Originally they could result in false positives
or false negatives.

The idea is to use a new node type ImportedName in semantic
analysis pass 1 to represent an indirect reference to a name in another
module. It will get resolved in semantic analysis pass 2.

ImportedName is not yet used everywhere where it could make
sense and this doesn't fix all related issues with import cycles.

Also did a bit of refactoring of type semantic analysis to avoid passing
multiple callback functions.

Fixes #4049.
Fixes #4429.
Fixes #4682.

Inspired by (and borrowed test cases from) #4495 by @carljm.

Supersedes #4495
yedpodtrzitko pushed a commit to kiwicom/mypy that referenced this issue Mar 15, 2018
This adds supports for some cases of importing an imported name
within an import cycle. Originally they could result in false positives
or false negatives.

The idea is to use a new node type ImportedName in semantic
analysis pass 1 to represent an indirect reference to a name in another
module. It will get resolved in semantic analysis pass 2.

ImportedName is not yet used everywhere where it could make
sense and this doesn't fix all related issues with import cycles.

Also did a bit of refactoring of type semantic analysis to avoid passing
multiple callback functions.

Fixes python#4049.
Fixes python#4429.
Fixes python#4682.

Inspired by (and borrowed test cases from) python#4495 by @carljm.

Supersedes python#4495
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants