Skip to content

Add B043: Do not call delattr with constant#514

Merged
cooperlees merged 2 commits intoPyCQA:mainfrom
fennr:feat/del-attr-with-constant
Nov 26, 2025
Merged

Add B043: Do not call delattr with constant#514
cooperlees merged 2 commits intoPyCQA:mainfrom
fennr:feat/del-attr-with-constant

Conversation

@fennr
Copy link
Contributor

@fennr fennr commented May 29, 2025

close #513

I used B043 because I see PR with B042

@fennr
Copy link
Contributor Author

fennr commented May 29, 2025

@cooperlees check my PR please

@cooperlees cooperlees requested a review from Copilot September 8, 2025 19:54
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Contributor

@jakkdl jakkdl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@cooperlees cooperlees merged commit 2d2fd4d into PyCQA:main Nov 26, 2025
6 checks passed
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` | [![OpenSSF Scorecard](https://api.securityscorecards.dev/projects/github.com/PyCQA/flake8-bugbear/badge)](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 ([#&#8203;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 ([#&#8203;503](PyCQA/flake8-bugbear#503))
- B912: New check for `map()` without an explicit `strict=` parameter. ([#&#8203;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\* ([#&#8203;500](PyCQA/flake8-bugbear#500))
- Skip B028 if warnings.warn is called with `*args` or `**kwargs` ([#&#8203;501](PyCQA/flake8-bugbear#501))
- Add B911: itertools.batched without strict= ([#&#8203;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 ([#&#8203;496](PyCQA/flake8-bugbear#496))
- B037: Fix typo in error message
- B024: No longer treats assigned class variables as abstract ([#&#8203;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) ([#&#8203;489](PyCQA/flake8-bugbear#489))
- B901: Do not trigger with explicit Generator return type ([#&#8203;481](PyCQA/flake8-bugbear#481))
- B008: add some comments, rename b008\_extend\_immutable\_calls ([#&#8203;476](PyCQA/flake8-bugbear#476))
- B040: exception with note added not reraised or used ([#&#8203;477](PyCQA/flake8-bugbear#477))
- B039, Add `ContextVar` with mutable literal or function call as default
- B040: Add Exception with added note not reraised. ([#&#8203;474](PyCQA/flake8-bugbear#474))
- Run tests in Python 3.13
- Type annotated code ([#&#8203;481](PyCQA/flake8-bugbear#481) + [#&#8203;483](PyCQA/flake8-bugbear#483))
- Replace hash with unsafe\_hash ([#&#8203;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` | [![OpenSSF Scorecard](https://api.securityscorecards.dev/projects/github.com/PyCQA/flake8-bugbear/badge)](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 ([#&#8203;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 ([#&#8203;503](PyCQA/flake8-bugbear#503))
- B912: New check for `map()` without an explicit `strict=` parameter. ([#&#8203;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\* ([#&#8203;500](PyCQA/flake8-bugbear#500))
- Skip B028 if warnings.warn is called with `*args` or `**kwargs` ([#&#8203;501](PyCQA/flake8-bugbear#501))
- Add B911: itertools.batched without strict= ([#&#8203;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 ([#&#8203;496](PyCQA/flake8-bugbear#496))
- B037: Fix typo in error message
- B024: No longer treats assigned class variables as abstract ([#&#8203;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) ([#&#8203;489](PyCQA/flake8-bugbear#489))
- B901: Do not trigger with explicit Generator return type ([#&#8203;481](PyCQA/flake8-bugbear#481))
- B008: add some comments, rename b008\_extend\_immutable\_calls ([#&#8203;476](PyCQA/flake8-bugbear#476))
- B040: exception with note added not reraised or used ([#&#8203;477](PyCQA/flake8-bugbear#477))
- B039, Add `ContextVar` with mutable literal or function call as default
- B040: Add Exception with added note not reraised. ([#&#8203;474](PyCQA/flake8-bugbear#474))
- Run tests in Python 3.13
- Type annotated code ([#&#8203;481](PyCQA/flake8-bugbear#481) + [#&#8203;483](PyCQA/flake8-bugbear#483))
- Replace hash with unsafe\_hash ([#&#8203;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>
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.

New rule to check delattr with constant

4 participants