-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
builtins-ignorelist
for attributes only
#6524
Comments
Why not disable the rule entirely? It's intentionally categorized as a separate rule to enable that kind of granular on/off switch. |
Fair question 😅 I think there are some attributes that I would still want to list, like |
I ran across this as well. I really don't want |
Just get class Foo:
def exec(self):
... It's not a shadowing, cause I still can use builtin class Foo:
@staticmethod
def exec():
exec("print('it is not a shadowing')")
Foo.exec() For me, shadowing is when an object covers the original object and confusion can occur when called. def foo(exec: str):
print(exec)
...
# and the later you may want to call builtin exec, but you can't
exec("print('it is a shadowing')")
foo('bar') Also
What's wrong with reader, who can mess |
@Olegt0rr - That is working as intended but it's a strange rule. You can see an example of what it's intended to catch here: class C:
@staticmethod
def list() -> None:
pass
@staticmethod
def repeat(value: int, times: int) -> list[int]:
return [value] * times The issue is that it will be shadowed if you reference the builtin within the class scope. |
PR up that should make this rule far more useful by only flagging actual shadowed reads (like in the example above) rather than the method definitions (which will almost always be fine): #9462 |
…9462) ## Summary This PR attempts to improve `builtin-attribute-shadowing` (`A003`), a rule which has been repeatedly criticized, but _does_ have value (just not in the current form). Historically, this rule would flag cases like: ```python class Class: id: int ``` This led to an increasing number of exceptions and special-cases to the rule over time to try and improve it's specificity (e.g., ignore `TypedDict`, ignore `@override`). The crux of the issue is that given the above, referencing `id` will never resolve to `Class.id`, so the shadowing is actually fine. There's one exception, however: ```python class Class: id: int def do_thing() -> id: pass ``` Here, `id` actually resolves to the `id` attribute on the class, not the `id` builtin. So this PR completely reworks the rule around this _much_ more targeted case, which will almost always be a mistake: when you reference a class member from within the class, and that member shadows a builtin. Closes #6524. Closes #7806.
Looking at https://beta.ruff.rs/docs/rules/builtin-attribute-shadowing/#builtin-attribute-shadowing-a003: I agree with the point made here: https://stackoverflow.com/a/60423602/1342874
Specifically, I think that the following is fine, and in fact better than alternatives:
but of course the following is not fine:
My understanding is that the
builtins-ignorelist
is all or nothing; it makes it so that you can usid
everywhere without warning, or nowhere without warning.My proposal is that there is a
builtins-attributes-ignorelist
or something similar added for this.The text was updated successfully, but these errors were encountered: