Skip to content

Ignore B018 (useless-expression) - #220

Merged
jaraco merged 1 commit into
mainfrom
ignore-b018
Oct 18, 2026
Merged

jaraco merged 1 commit into
mainfrom
ignore-b018

Conversation

@jaraco

@jaraco jaraco commented Oct 3, 2026

Copy link
Copy Markdown
Owner

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: B018 or assigning to a throwaway name) add noise without improving the code.

Use cases

Side effects in library code — jaraco/keyring carries six # noqa: B018 suppressions, e.g.:

  • win32cred.__name__ / secretstorage.__name__ to force a demand-loaded import to raise ImportError at a controlled point (Windows.py, SecretService.py).
  • cls.priority / class_.priority to 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:

with pytest.raises(AttributeError):
    self.keyring.foo

In jaraco/zipp, ruff 0.16's expanded defaults (#210) flagged root.name / root.parent inside assertRaises. The only lint-clean alternatives are _ = root.name or a noqa comment, 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 == y as a statement) is covered separately by B015 (useless-comparison), which remains enabled.

🤖 Generated with Claude Code

Ref #112, #210.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jaraco
jaraco merged commit 1989ff4 into main Oct 18, 2026
32 of 36 checks passed
@jaraco
jaraco deleted the ignore-b018 branch October 18, 2026 15:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant