Skip to content

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

Closed

Description

@mthuurne

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.

Activity

  1. JukkaL commented on Jun 21, 2019

    @JukkaL
    Collaborator

    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 :-)

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