Skip to content

[flake8-bugbear] Implement delattr-with-constant (B043)#23737

Merged
amyreese merged 6 commits intoastral-sh:mainfrom
Bortlesboat:implement-b043-delattr-with-constant
Mar 9, 2026
Merged

[flake8-bugbear] Implement delattr-with-constant (B043)#23737
amyreese merged 6 commits intoastral-sh:mainfrom
Bortlesboat:implement-b043-delattr-with-constant

Conversation

@Bortlesboat
Copy link
Copy Markdown
Contributor

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 use del obj.foo instead.

The implementation follows the same pattern as the existing B009 (getattr) and B010 (setattr) rules:

  • Checks that the attribute name is a valid Python identifier
  • Skips __debug__ (deleting it is a SyntaxError)
  • Skips mangled private names
  • Marks fixes as unsafe for non-NFKC attribute names and expressions containing comments
  • Only applies the fix when the delattr call is used as a standalone statement

Partially addresses #3758.

Test plan

Added test fixture B043.py covering valid usage, invalid usage, starred args, NFKC safety, comments, and builtins.delattr.

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)
@astral-sh-bot astral-sh-bot bot requested a review from amyreese March 5, 2026 12:39
@ntBre
Copy link
Copy Markdown
Contributor

ntBre commented Mar 5, 2026

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 ntBre added rule Implementing or modifying a lint rule preview Related to preview mode features labels Mar 5, 2026
@ntBre ntBre linked an issue Mar 5, 2026 that may be closed by this pull request
@astral-sh-bot
Copy link
Copy Markdown

astral-sh-bot bot commented Mar 5, 2026

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

ℹ️ ecosystem check detected linter changes. (+21 -0 violations, +0 -0 fixes in 6 projects; 50 projects unchanged)

apache/airflow (+11 -0 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --no-fix --output-format concise --preview --select ALL

+ providers/common/io/tests/unit/common/io/operators/test_file_transfer.py:78:9: B043 [*] Do not call `delattr` with a constant attribute value. It is not any safer than normal property deletion.
+ providers/common/io/tests/unit/common/io/operators/test_file_transfer.py:80:9: B043 [*] Do not call `delattr` with a constant attribute value. It is not any safer than normal property deletion.
+ providers/openlineage/tests/unit/openlineage/plugins/test_listener.py:1665:9: B043 [*] Do not call `delattr` with a constant attribute value. It is not any safer than normal property deletion.
+ providers/openlineage/tests/unit/openlineage/plugins/test_listener.py:1872:9: B043 [*] Do not call `delattr` with a constant attribute value. It is not any safer than normal property deletion.
+ providers/yandex/tests/unit/yandex/hooks/test_yandex.py:146:9: B043 [*] Do not call `delattr` with a constant attribute value. It is not any safer than normal property deletion.
+ shared/configuration/tests/configuration/test_parser.py:388:13: B043 [*] Do not call `delattr` with a constant attribute value. It is not any safer than normal property deletion.
+ shared/configuration/tests/configuration/test_parser.py:390:13: B043 [*] Do not call `delattr` with a constant attribute value. It is not any safer than normal property deletion.
+ task-sdk/src/airflow/sdk/execution_time/supervisor.py:1762:9: B043 [*] Do not call `delattr` with a constant attribute value. It is not any safer than normal property deletion.
+ task-sdk/src/airflow/sdk/execution_time/supervisor.py:1769:17: B043 [*] Do not call `delattr` with a constant attribute value. It is not any safer than normal property deletion.
+ task-sdk/tests/task_sdk/execution_time/test_supervisor.py:2681:13: B043 [*] Do not call `delattr` with a constant attribute value. It is not any safer than normal property deletion.
+ task-sdk/tests/task_sdk/execution_time/test_supervisor.py:2684:13: B043 [*] Do not call `delattr` with a constant attribute value. It is not any safer than normal property deletion.

apache/superset (+5 -0 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --no-fix --output-format concise --preview --select ALL

+ superset/commands/chart/warm_up_cache.py:76:9: B043 [*] Do not call `delattr` with a constant attribute value. It is not any safer than normal property deletion.
+ superset/utils/core.py:1505:9: B043 [*] Do not call `delattr` with a constant attribute value. It is not any safer than normal property deletion.
+ tests/integration_tests/dao/base_dao_test.py:87:9: B043 [*] Do not call `delattr` with a constant attribute value. It is not any safer than normal property deletion.
+ tests/integration_tests/utils_tests.py:344:13: B043 [*] Do not call `delattr` with a constant attribute value. It is not any safer than normal property deletion.
+ tests/unit_tests/common/test_query_context_processor.py:515:9: B043 [*] Do not call `delattr` with a constant attribute value. It is not any safer than normal property deletion.

bokeh/bokeh (+1 -0 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --no-fix --output-format concise --preview --select ALL

+ src/bokeh/core/has_props.py:723:13: B043 [*] Do not call `delattr` with a constant attribute value. It is not any safer than normal property deletion.

model-bakers/model_bakery (+1 -0 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --no-fix --output-format concise --preview

+ tests/test_filling_fields.py:62:9: B043 [*] Do not call `delattr` with a constant attribute value. It is not any safer than normal property deletion.

rotki/rotki (+2 -0 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --no-fix --output-format concise --preview

+ rotkehlchen/tests/conftest.py:116:13: B043 [*] Do not call `delattr` with a constant attribute value. It is not any safer than normal property deletion.
+ rotkehlchen/tests/unit/test_utils.py:220:5: B043 [*] Do not call `delattr` with a constant attribute value. It is not any safer than normal property deletion.

zulip/zulip (+1 -0 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --no-fix --output-format concise --preview --select ALL

+ confirmation/migrations/0016_realmcreationkey_to_realmcreationstatus.py:92:17: B043 [*] Do not call `delattr` with a constant attribute value. It is not any safer than normal property deletion.

Changes by rule (1 rules affected)

code total + violation - violation + fix - fix
B043 21 21 0 0 0

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Copy link
Copy Markdown
Member

@amyreese amyreese left a comment

Choose a reason for hiding this comment

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

Thank you for working on this

- 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 amyreese changed the title [flake8-bugbear] Implement delattr-with-constant (B043) [flake8-bugbear] Implement delattr-with-constant (B043) Mar 9, 2026
@amyreese amyreese enabled auto-merge (squash) March 9, 2026 19:37
@amyreese amyreese merged commit be5db3c into astral-sh:main Mar 9, 2026
42 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

preview Related to preview mode features rule Implementing or modifying a lint rule

Projects

None yet

Development

Successfully merging this pull request may close these issues.

New rule del-attr-with-constant

3 participants