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

Processing order of methods affects inferred attribute types #7162

Open
JukkaL opened this issue Jul 5, 2019 · 1 comment
Open

Processing order of methods affects inferred attribute types #7162

JukkaL opened this issue Jul 5, 2019 · 1 comment
Assignees
Labels
bug mypy got something wrong priority-1-normal semantic-analyzer Problems that happen during semantic analysis topic-import-cycles

Comments

@JukkaL
Copy link
Collaborator

JukkaL commented Jul 5, 2019

This test case produces unexpected output when using the new semantic analyzer (after having reversed the processing order of modules within SCCs):

[case testAttributeDefOrder1]
import a

[file a.py]
from b import C

class D(C):
    def g(self) -> None:
        self.x = ''  # Error (as expected)

    def f(self) -> None:
        reveal_type(self.x)  # Revealed type is 'Any'; cannot determine type of 'x' (bad)

[file b.py]
import a

class C:
    def __init__(self) -> None:
        self.x = 0

I'd expect the type of x to be consistently int.

This test case produces unexpected output with both semantic analyzers:

[case testAttributeDefOrder2]
class D(C):
    def g(self) -> None:
        self.x = ''  # No error here (should be an error)

    def f(self) -> None:
        reveal_type(self.x)  # str (should be int)

class C:
    def __init__(self) -> None:
        self.x = 0

class E(C):
    def g(self) -> None:
        self.x = ''  # Error here

    def f(self) -> None:
        reveal_type(self.x)  # int

The reason for the weird behavior seems to be that we create duplicate Var nodes for attributes if the methods of a subclass are processed before base class methods.

@JukkaL JukkaL added bug mypy got something wrong topic-import-cycles priority-0-high semantic-analyzer Problems that happen during semantic analysis labels Jul 5, 2019
@JukkaL JukkaL self-assigned this Jul 5, 2019
@JukkaL
Copy link
Collaborator Author

JukkaL commented Jul 5, 2019

The "cannot determine type" and revealed "Any" were due to a bug in my implementation, so there is no longer a deviation between old and new semantic analyzers. Lowering priority.

davidhalter added a commit to davidhalter/mypy that referenced this issue Aug 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong priority-1-normal semantic-analyzer Problems that happen during semantic analysis topic-import-cycles
Projects
None yet
Development

No branches or pull requests

1 participant