Skip to content
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

Type aliases are not recognized correctly in some situations with import cycles #5130

Closed
dfroger opened this issue Jun 1, 2018 · 5 comments
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-1-normal semantic-analyzer Problems that happen during semantic analysis topic-import-cycles

Comments

@dfroger
Copy link
Contributor

dfroger commented Jun 1, 2018

This may be a duplicate issue, but I'm a bit lost... I'm using mypy 0.600 and Python 3.6.2.

# a.py
from typing import Union

from c import T

class A:
    x: str

class B:
    x: str

U = Union[A, B]
# b.py
from c import U

def foo(u: U):
    print(u.x)
# c.py
class T:
    pass

from a import U
from b import foo

python -c 'import c' works, Mypy . give the errors:

b.py:3: error: Invalid type "a.U"
b.py:4: error: U? has no attribute "x"

I would like to understand how here Mypy import mechanism is different from Python import mechanism, and why Mypy fails with U type.

@JelleZijlstra
Copy link
Member

Mypy doesn't exactly replicate Python's mechanism for resolving circular imports. Python can basically move back and forth between modules during execution; mypy doesn't do that.

This seems closely similar to #4930, although that one was triggered only by import *.

@dfroger
Copy link
Contributor Author

dfroger commented Jun 1, 2018

Also maybe similar to #4442

@ghost
Copy link

ghost commented Jun 1, 2019

Ran into a similar issue:

a.py

def incr(i: int) -> int:
    from b import Integer
    res: Integer = i + 1
    return res

b.py

from a import incr

Integer = int

Results in an error: a.py:4: error: Invalid type "Integer"

@ilevkivskyi
Copy link
Member

I can't reproduce the original example on master. The new example still fails, but passes with the --new-semantic-analyzer flag. This should be closed when we release the version with new analyzer.

@ilevkivskyi ilevkivskyi added bug mypy got something wrong false-positive mypy gave an error on correct code semantic-analyzer Problems that happen during semantic analysis priority-1-normal topic-import-cycles labels Jun 1, 2019
@ilevkivskyi ilevkivskyi changed the title Invalid type Type aliases are not recognized correctly in some situations with import cycles Jun 1, 2019
@ilevkivskyi
Copy link
Member

Closing as this is fixed with new semantic analyzer (will be released soon).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-1-normal semantic-analyzer Problems that happen during semantic analysis topic-import-cycles
Projects
None yet
Development

No branches or pull requests

3 participants