Add B043: Do not call delattr with constant#514
Merged
cooperlees merged 2 commits intoPyCQA:mainfrom Nov 26, 2025
Merged
Conversation
Contributor
Author
|
@cooperlees check my PR please |
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR adds a new linting rule B043 that detects when delattr() is called with a constant string attribute name, suggesting to use direct attribute deletion instead.
- Adds rule B043 to detect
delattr(obj, "constant")usage patterns - Provides test cases demonstrating valid and invalid usage patterns
- Updates documentation to explain the rule and its rationale
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| tests/eval_files/b043.py | Test cases showing valid dynamic usage vs invalid constant usage of delattr |
| bugbear.py | Core logic to detect delattr calls with constant string arguments and error definition |
| README.rst | Documentation explaining B043 rule and recommending del x.attr over delattr(x, 'attr') |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
jakkdl
approved these changes
Nov 25, 2025
Contributor
jakkdl
left a comment
There was a problem hiding this comment.
agree with @JelleZijlstra that it's perhaps out of current scope of flake8-bugbear nowadays, but given that we have B009&B010 it seems reasonable to complete the set.
736-c41-2c1-e464fc974
added a commit
to Swiss-Armed-Forces/Loom
that referenced
this pull request
Feb 2, 2026
This MR contains the following updates: | Package | Type | Update | Change | OpenSSF | |---|---|---|---|---| | [flake8-bugbear](https://github.com/PyCQA/flake8-bugbear#change-log) ([changelog](https://github.com/PyCQA/flake8-bugbear#change-log)) | dev | major | `^24.4.26` → `^25.0.0` | [](https://securityscorecards.dev/viewer/?uri=github.com/PyCQA/flake8-bugbear) | --- ### Release Notes <details> <summary>PyCQA/flake8-bugbear (flake8-bugbear)</summary> ### [`v25.11.29`](https://github.com/PyCQA/flake8-bugbear/releases/tag/25.11.29) [Compare Source](PyCQA/flake8-bugbear@25.10.21...25.11.29) - B043: Add new check to state don't call delattr with constant ([#​514](PyCQA/flake8-bugbear#514)) - B042: ignore overloaded init, ignore if str+pickle dunder, improve README ### [`v25.10.21`](https://github.com/PyCQA/flake8-bugbear/releases/tag/25.10.21) [Compare Source](PyCQA/flake8-bugbear@24.12.12...25.10.21) - B042: New check for reminding to call super().**init** in custom exceptions - B028: Skip if skip\_file\_prefixes is used ([#​503](PyCQA/flake8-bugbear#503)) - B912: New check for `map()` without an explicit `strict=` parameter. ([#​516](PyCQA/flake8-bugbear#516)) - Add python3.14 Support / CI - Remove python3.9 support / CI - flake8-bugbear now requires at least Python 3.10, like the next release of flake8 ### [`v24.12.12`](https://github.com/PyCQA/flake8-bugbear/releases/tag/24.12.12) [Compare Source](PyCQA/flake8-bugbear@24.10.31...24.12.12) - B012 and B025 now also handle try/except\* ([#​500](PyCQA/flake8-bugbear#500)) - Skip B028 if warnings.warn is called with `*args` or `**kwargs` ([#​501](PyCQA/flake8-bugbear#501)) - Add B911: itertools.batched without strict= ([#​502](PyCQA/flake8-bugbear#502)) - Readme has anchors per check (they do not seem to render on GitHub tho) ### [`v24.10.31`](https://github.com/PyCQA/flake8-bugbear/releases/tag/24.10.31) [Compare Source](PyCQA/flake8-bugbear@24.8.19...24.10.31) - B041: New dictionary same key AND value check ([#​496](PyCQA/flake8-bugbear#496)) - B037: Fix typo in error message - B024: No longer treats assigned class variables as abstract ([#​471](PyCQA/flake8-bugbear#471)) - Bump required attrs version to 22.2.0 ### [`v24.8.19`](https://github.com/PyCQA/flake8-bugbear/releases/tag/24.8.19) [Compare Source](PyCQA/flake8-bugbear@24.4.26...24.8.19) - B910: implement to suggest using Counter() instead of defaultdict(int) ([#​489](PyCQA/flake8-bugbear#489)) - B901: Do not trigger with explicit Generator return type ([#​481](PyCQA/flake8-bugbear#481)) - B008: add some comments, rename b008\_extend\_immutable\_calls ([#​476](PyCQA/flake8-bugbear#476)) - B040: exception with note added not reraised or used ([#​477](PyCQA/flake8-bugbear#477)) - B039, Add `ContextVar` with mutable literal or function call as default - B040: Add Exception with added note not reraised. ([#​474](PyCQA/flake8-bugbear#474)) - Run tests in Python 3.13 - Type annotated code ([#​481](PyCQA/flake8-bugbear#481) + [#​483](PyCQA/flake8-bugbear#483)) - Replace hash with unsafe\_hash ([#​486](PyCQA/flake8-bugbear#486)) </details> --- - [ ] <!-- rebase-check -->If you want to rebase/retry this MR, check this box --- This MR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0My4wLjciLCJ1cGRhdGVkSW5WZXIiOiI0My4wLjciLCJ0YXJnZXRCcmFuY2giOiJtYWluIiwibGFiZWxzIjpbImRlcGVuZGVuY2llcyIsInJlbm92YXRlIl19--> See merge request swiss-armed-forces/cyber-command/cea/loom!276 Co-authored-by: Loom MR Pipeline Trigger <group_103951964_bot_9504bb8dead6d4e406ad817a607f24be@noreply.gitlab.com> Co-authored-by: shrewd-laidback palace <shrewd-laidback-palace-736-c41-2c1-e464fc974@swiss-armed-forces-open-source.ch>
736-c41-2c1-e464fc974
added a commit
to Swiss-Armed-Forces/Loom
that referenced
this pull request
Feb 2, 2026
chore(deps): update flake8-bugbear (major) This MR contains the following updates: | Package | Type | Update | Change | OpenSSF | |---|---|---|---|---| | [flake8-bugbear](https://github.com/PyCQA/flake8-bugbear#change-log) ([changelog](https://github.com/PyCQA/flake8-bugbear#change-log)) | dev | major | `^24.4.26` → `^25.0.0` | [](https://securityscorecards.dev/viewer/?uri=github.com/PyCQA/flake8-bugbear) | --- ### Release Notes <details> <summary>PyCQA/flake8-bugbear (flake8-bugbear)</summary> ### [`v25.11.29`](https://github.com/PyCQA/flake8-bugbear/releases/tag/25.11.29) [Compare Source](PyCQA/flake8-bugbear@25.10.21...25.11.29) - B043: Add new check to state don't call delattr with constant ([#​514](PyCQA/flake8-bugbear#514)) - B042: ignore overloaded init, ignore if str+pickle dunder, improve README ### [`v25.10.21`](https://github.com/PyCQA/flake8-bugbear/releases/tag/25.10.21) [Compare Source](PyCQA/flake8-bugbear@24.12.12...25.10.21) - B042: New check for reminding to call super().**init** in custom exceptions - B028: Skip if skip\_file\_prefixes is used ([#​503](PyCQA/flake8-bugbear#503)) - B912: New check for `map()` without an explicit `strict=` parameter. ([#​516](PyCQA/flake8-bugbear#516)) - Add python3.14 Support / CI - Remove python3.9 support / CI - flake8-bugbear now requires at least Python 3.10, like the next release of flake8 ### [`v24.12.12`](https://github.com/PyCQA/flake8-bugbear/releases/tag/24.12.12) [Compare Source](PyCQA/flake8-bugbear@24.10.31...24.12.12) - B012 and B025 now also handle try/except\* ([#​500](PyCQA/flake8-bugbear#500)) - Skip B028 if warnings.warn is called with `*args` or `**kwargs` ([#​501](PyCQA/flake8-bugbear#501)) - Add B911: itertools.batched without strict= ([#​502](PyCQA/flake8-bugbear#502)) - Readme has anchors per check (they do not seem to render on GitHub tho) ### [`v24.10.31`](https://github.com/PyCQA/flake8-bugbear/releases/tag/24.10.31) [Compare Source](PyCQA/flake8-bugbear@24.8.19...24.10.31) - B041: New dictionary same key AND value check ([#​496](PyCQA/flake8-bugbear#496)) - B037: Fix typo in error message - B024: No longer treats assigned class variables as abstract ([#​471](PyCQA/flake8-bugbear#471)) - Bump required attrs version to 22.2.0 ### [`v24.8.19`](https://github.com/PyCQA/flake8-bugbear/releases/tag/24.8.19) [Compare Source](PyCQA/flake8-bugbear@24.4.26...24.8.19) - B910: implement to suggest using Counter() instead of defaultdict(int) ([#​489](PyCQA/flake8-bugbear#489)) - B901: Do not trigger with explicit Generator return type ([#​481](PyCQA/flake8-bugbear#481)) - B008: add some comments, rename b008\_extend\_immutable\_calls ([#​476](PyCQA/flake8-bugbear#476)) - B040: exception with note added not reraised or used ([#​477](PyCQA/flake8-bugbear#477)) - B039, Add `ContextVar` with mutable literal or function call as default - B040: Add Exception with added note not reraised. ([#​474](PyCQA/flake8-bugbear#474)) - Run tests in Python 3.13 - Type annotated code ([#​481](PyCQA/flake8-bugbear#481) + [#​483](PyCQA/flake8-bugbear#483)) - Replace hash with unsafe\_hash ([#​486](PyCQA/flake8-bugbear#486)) </details> --- - [ ] <!-- rebase-check -->If you want to rebase/retry this MR, check this box --- This MR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0My4wLjciLCJ1cGRhdGVkSW5WZXIiOiI0My4wLjciLCJ0YXJnZXRCcmFuY2giOiJtYWluIiwibGFiZWxzIjpbImRlcGVuZGVuY2llcyIsInJlbm92YXRlIl19--> See merge request swiss-armed-forces/cyber-command/cea/loom!276 Co-authored-by: Loom MR Pipeline Trigger <group_103951964_bot_9504bb8dead6d4e406ad817a607f24be@noreply.gitlab.com> Co-authored-by: open-source Pipeline <group_90701827_bot_ed04ae348bc5f40af9966fb8b6867e99@noreply.gitlab.com>
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.
close #513
I used B043 because I see PR with B042