Skip to content

New semantic analyzer: regression with circular import and TypeVar #7037

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
mthuurne opened this issue Jun 21, 2019 · 1 comment · Fixed by #6563
Closed

New semantic analyzer: regression with circular import and TypeVar #7037

mthuurne opened this issue Jun 21, 2019 · 1 comment · Fixed by #6563
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-0-high semantic-analyzer Problems that happen during semantic analysis topic-import-cycles

Comments

@mthuurne
Copy link
Contributor

mthuurne commented Jun 21, 2019

I found a regression when using the new semantic analyzer in mypy 0.710.

To reproduce, you need the following two modules:

# factory.py

from typing import Generic, Type

from box import BoxT

class Factory(Generic[BoxT]):
    value = 123

    def create(self, boxClass: Type[BoxT]) -> BoxT:
        return boxClass.create(self)
# box.py

from typing import TYPE_CHECKING, Type, TypeVar

if TYPE_CHECKING:
    from factory import Factory

BoxT = TypeVar('BoxT', bound='Box')

class Box:

    @classmethod
    def create(cls: Type[BoxT], f: Factory) -> BoxT:
        return cls(f.value)

    def __init__(self, value: int):
        print(value)

When mypy is run on these modules with the default arguments, no issues are reported. But when run withthe new semantic analyzer, the following happens:

$ mypy --new-semantic-analyzer box.py factory.py 
factory.py:11: error: "Type[BoxT]" has no attribute "create"

The problem does not occur if there is no circular import. It also doesn't occur if the BoxT TypeVar is defined in the factory module instead of imported from the box module.

@JukkaL JukkaL added bug mypy got something wrong false-positive mypy gave an error on correct code semantic-analyzer Problems that happen during semantic analysis priority-0-high topic-import-cycles labels Jun 21, 2019
@JukkaL
Copy link
Collaborator

JukkaL commented Jun 21, 2019

Thanks for reporting this! This seems like an important issue to fix, in particular since the new semantic analyzer is supposed to work better with import cycles :-)

ilevkivskyi added a commit that referenced this issue Jun 26, 2019
…nd fix stale upper bounds and values (#6563)

This is a follow up for #6527.

Fixes #7037
I think this also essentially concludes #6300

This PR prohibits duplicate type variable definitions, previously the second one was silently winning. Also now the first one wins. Note that it didn't require additional attribute on `TypeVarExpr` as I initially though, it looks like we can simple use `s.rvalue.analyzed` for the purpose of linking assignments to variables.

This PR also naturally fixes an issue where stale upper bound or value appears in a class that uses the type variable.
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-0-high semantic-analyzer Problems that happen during semantic analysis topic-import-cycles
Projects
None yet
2 participants