Repository navigation
Ignore B018 (useless-expression) - #220
Merged
Merged
Conversation
This was referenced Oct 3, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Ruff's B018 (useless-expression) flags a bare attribute access as a statement. In practice, accessing an attribute for its side effect is a useful, intentional idiom, and the workarounds (
# noqa: B018or assigning to a throwaway name) add noise without improving the code.Use cases
Side effects in library code — jaraco/keyring carries six
# noqa: B018suppressions, e.g.:win32cred.__name__/secretstorage.__name__to force a demand-loaded import to raiseImportErrorat a controlled point (Windows.py, SecretService.py).cls.priority/class_.priorityto probe a property that raises if the backend isn't viable (backend.py, core.py).Assertions in tests — asserting that attribute access raises is common:
In jaraco/zipp, ruff 0.16's expanded defaults (#210) flagged
root.name/root.parentinsideassertRaises. The only lint-clean alternatives are_ = root.nameor anoqacomment, both of which litter tests with meaningless assignments to ignored names or suppression comments. The bare access is the most direct expression of intent.Prior discussion
In #112, B018 was noted to have "a lot of false positives, especially in keyring libraries that use the side effect of attribute access," and it was suggested to disable it at least for tests. Since the side-effect idiom is legitimate in library code too, this disables it globally.
Trade-off
B018 also catches truly useless expressions (e.g. a stray literal), but the higher-value "forgot to
assert" case (x == yas a statement) is covered separately by B015 (useless-comparison), which remains enabled.🤖 Generated with Claude Code