You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
classA:
x=1defget_x(self):
returnself.xa=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.
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 a is an instance of A
Ideally, Ruff understands that a is an instance of A even if A is defined in another file or if A is the result of a function call
Ruff needs to understand whether a.x is an instance attribute or not (I think that's doable because detecting class attributes is "easy enough).
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.
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:
The text was updated successfully, but these errors were encountered: