-
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
B018 false positive on attribute access with side effect #10536
Comments
I did search the flake8-bugbear issue tracker (and this one) to see if this issue had been discussed previously, but came up empty. |
Suppress B018 errors where they occur. Ref astral-sh/ruff#10536.
I am reviewing the ruff rules in Home Assistant and my solution is to use Of course normally reading attributes or properties should not have side-effects, but there are occasions where it is done. The underscore as throw-away assignment is also quite common in the programming environment (e.g. in Rust) |
Ah yeah, I think suggesting |
Using ruff 0.3.4 and attempting to adopt bugbear (B) checks in the keyring project, I'm encountering what appear to be false positives:
(there was one other prior to jaraco/keyring@7881db6)
Looking at keyring.backend:71, it's apparent that the "useless expression" is
cls.priority
. The protocol for keyring, however, is for a backend to raise an exception in the.priority
access if the backend is not viable at all.Looking at keyring.backends.Windows:14, the purpose of the attribute access is explicitly documented (force demand import to import the module).
The advice is wrong. In particular, the expression isn't useless (the application would fail without the side effect of accessing the property), and the guidance is misleading (assigning to a variable would introduce more lint and removing it would break the the behavior).
I can imagine one possible workaround is to replace direct attribute access with a
getattr
call:However, this approach makes the code less readable and idiosyncratic (the canonical way to access an attribute is with dot notation), and making the change introduces B009 errors:
Another workaround could be to disable B018 checks for all projects (or possibly just this project; I'm not yet sure how much this issue is isolated to keyring). That would be somewhat undesirable. It would be nice to get an error for failing to call a method (e.g.
self._init
).The workaround I'm leaning toward is to put
noqa: B018
on each of these expressions, but that feels messy.Since I'm not confident that ruff could readily distinguish between a property access that has a side-effect and a useless property access, I wonder if the documentation should at least capture this scenario and make a recommendation on what to do in that case.
The text was updated successfully, but these errors were encountered: