Skip to content

Processing order of methods affects inferred attribute types #7162

@JukkaL

Description

@JukkaL

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.

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions