Feature Request: Warn when instance attribute shadows class attribute
I suggest adding a linting rule to warn when an instance attribute shadows a class attribute of the same name. This can help prevent bugs from unintended overriding of class-level variables.
class A:
x = 1
def get_x(self):
return self.x
a = A()
a.x = 2 # Shadows class attribute 'x'
print(a.get_x()) # Outputs 2, but class attribute 'x' is 1
In this example, assigning to a.x creates an instance attribute that overshadows the class attribute x. Accessing self.x in get_x now refers to the instance attribute, which might not be the intended behavior.
Similarly, the subclassing is error prone:
class Parent:
def __init__(self):
self.value = 'instance'
def get_value(self):
return self.value
class Child(Parent):
value = 'class' # Class attribute shadows instance attribute
c = Child()
print(c.get_value()) # Outputs 'instance'
@MichaReiser this might be relevant here: https://github.com/astral-sh/ruff/pull/10891
Thanks for opening this rule idea.
The main challenge with this rule is that it probably requires type inference, or at least more advanced analysis than what Ruff does today to be useful:
- Ruff needs to understand that
ais an instance ofA - Ideally, Ruff understands that
ais an instance ofAeven ifAis defined in another file or ifAis the result of a function call - Ruff needs to understand whether
a.xis an instance attribute or not (I think that's doable because detecting class attributes is "easy enough).
Can we use ty in ruff to implement this?
Can we use
tyin ruff to implement this?
Not today, no. Using ty in ruff requires fundamental changes to how ruff works internally. But it's on our long-term roadmap to bring the power of ty to ruff lint rules but we don't know yet how exactly we'll do that.
@MichaReiser thanks! It’s exciting and everything seems to go in the best direction.
@MichaReiser did ruff or ty change in this question? If there is an example for linting based on type inference, I’d give it a try
No, Ruff isn't yet using ty's analysis engine