microsoft / pylance-release

Documentation and issues for Pylance
Creative Commons Attribution 4.0 International
1.7k stars 767 forks source link

a subclass member variable is not recognized as the same variable in superclass unless it has an explicit type hint #5890

Open adusamr opened 5 months ago

adusamr commented 5 months ago

Environment data

Actual behavior

class A:
    def __init__(self, a):
        self.a = a

class B(A):
    def __init__(self, a):
        self.a = a

In this code, self.a in class A and self.a in class B are recognized as different variables. This causes incorrect behavior of find references or rename variable. Especially the behavior of rename variable easily can break the code.

If you have any type hint of the self.a in the superclass, this does not happen, even if the type hint is undefined.

class A:
    def __init__(self, a):
        self.a: foo = a

class B(A):
    def __init__(self, a):
        self.a = a
rchiodo commented 5 months ago

Thanks for the issue. I can reproduce this too. Very weird that the type annotation makes us understand that B.a is inherited.