[flake8-bugbear] Implement delattr-with-constant (B043)#23737
Merged
amyreese merged 6 commits intoastral-sh:mainfrom Mar 9, 2026
Merged
[flake8-bugbear] Implement delattr-with-constant (B043)#23737amyreese merged 6 commits intoastral-sh:mainfrom
flake8-bugbear] Implement delattr-with-constant (B043)#23737amyreese merged 6 commits intoastral-sh:mainfrom
Conversation
Implement B043 from flake8-bugbear, which flags `delattr()` calls with constant string attribute arguments. These can be replaced with `del` statements for better readability. The implementation mirrors the existing B009 (getattr) and B010 (setattr) rules, including NFKC normalization safety checks and __debug__ handling. Closes #3758 (partially)
Contributor
|
Thanks! I'll leave the review to Amy but I was curious about historical issues, and this will close #18338, which links to the upstream implementation: PyCQA/flake8-bugbear#514. So I think it seems okay for us to add it too. |
ntBre
reviewed
Mar 5, 2026
crates/ruff_linter/src/rules/flake8_bugbear/rules/delattr_with_constant.rs
Outdated
Show resolved
Hide resolved
|
| code | total | + violation | - violation | + fix | - fix |
|---|---|---|---|---|---|
| B043 | 21 | 21 | 0 | 0 | 0 |
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
amyreese
requested changes
Mar 6, 2026
Member
amyreese
left a comment
There was a problem hiding this comment.
Thank you for working on this
crates/ruff_linter/src/rules/flake8_bugbear/rules/delattr_with_constant.rs
Outdated
Show resolved
Hide resolved
crates/ruff_linter/src/rules/flake8_bugbear/rules/delattr_with_constant.rs
Outdated
Show resolved
Hide resolved
crates/ruff_linter/src/rules/flake8_bugbear/rules/delattr_with_constant.rs
Outdated
Show resolved
Hide resolved
crates/ruff_linter/src/rules/flake8_bugbear/rules/delattr_with_constant.rs
Outdated
Show resolved
Hide resolved
crates/ruff_linter/src/rules/flake8_bugbear/rules/delattr_with_constant.rs
Outdated
Show resolved
Hide resolved
crates/ruff_linter/src/rules/flake8_bugbear/rules/delattr_with_constant.rs
Outdated
Show resolved
Hide resolved
- Move semantic builtin check to top for early exit - Combine args destructuring into single let - Extract attr_name earlier and use throughout - Merge guard conditions into single conditional - Use Fix::applicable_edit with Applicability enum - Rename helper to generate_del_statement and move below rule
amyreese
approved these changes
Mar 7, 2026
delattr-with-constant (B043)flake8-bugbear] Implement delattr-with-constant (B043)
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.
Summary
Implements B043 from flake8-bugbear, which flags
delattr()calls where the attribute name is a constant string literal (e.g.,delattr(obj, "foo")). These should usedel obj.fooinstead.The implementation follows the same pattern as the existing B009 (
getattr) and B010 (setattr) rules:__debug__(deleting it is a SyntaxError)delattrcall is used as a standalone statementPartially addresses #3758.
Test plan
Added test fixture
B043.pycovering valid usage, invalid usage, starred args, NFKC safety, comments, andbuiltins.delattr.