Skip to content

[ruff] Fix non-triggering example for if-key-in-dict-del (RUF051) - #26433

Merged
ntBre merged 1 commit into
astral-sh:mainfrom
nikolauspschuetz:docs/ruf051-triggering-example
Jul 6, 2026
Merged

[ruff] Fix non-triggering example for if-key-in-dict-del (RUF051)#26433
ntBre merged 1 commit into
astral-sh:mainfrom
nikolauspschuetz:docs/ruf051-triggering-example

Conversation

@nikolauspschuetz

Copy link
Copy Markdown
Contributor

Summary

The if-key-in-dict-del (RUF051) rule docstring's ## Example does not actually trigger the rule.

RUF051 only fires when the dictionary is known to be of type dict (typing::is_known_to_be_of_type_dict). The example used a bare, unbound dictionary, whose type can't be inferred — so the snippet presented as triggering is in fact clean:

$ printf 'if key in dictionary:\n    del dictionary[key]\n' | ruff check --select RUF051 -
All checks passed!

Change

Bind dictionary = {} in both code blocks so the example triggers (and the "Use instead" stays parallel). Docstring-only; no behavior change.

# Example (now triggers)
dictionary = {}
if key in dictionary:
    del dictionary[key]

# Use instead
dictionary = {}
dictionary.pop(key, None)

This matches the convention already used by the sibling dict-type-gated rule if-else-block-instead-of-dict-get (SIM401), whose docstring example likewise binds foo = {}.

Test plan

  • Verified the old example is clean and the new one triggers:
    $ printf 'dictionary = {}\nif key in dictionary:\n    del dictionary[key]\n' | ruff check --select RUF051 -
    RUF051 [*] Use `pop` instead of `key in dict` followed by `del dict[key]`
  • cargo dev generate-docs regenerates docs/rules/if-key-in-dict-del.md cleanly.
  • scripts/check_docs_formatted.pyAll docs are formatted correctly.

…UF051)

RUF051 only fires when the dictionary is known to be of type `dict`
(`typing::is_known_to_be_of_type_dict`). The docstring's "## Example" used a
bare, unbound `dictionary`, whose type can't be inferred, so the example does
not actually trigger the rule:

    $ printf 'if key in dictionary:\n    del dictionary[key]\n' \
        | ruff check --select RUF051 -
    All checks passed!

Bind `dictionary = {}` in both code blocks so the example triggers, matching
the convention used by the sibling dict-type-gated rule
`if-else-block-instead-of-dict-get` (SIM401), whose example likewise binds
`foo = {}`. Docstring-only; no behavior change.
@astral-sh-bot
astral-sh-bot Bot requested a review from ntBre June 27, 2026 18:58
@ntBre ntBre added the documentation Improvements or additions to documentation label Jul 6, 2026

@ntBre ntBre left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks! This fixes RUF051 for the sake of #18972.

@ntBre ntBre changed the title [ruff] Fix non-triggering docstring example for if-key-in-dict-del (RUF051) [ruff] Fix non-triggering example for if-key-in-dict-del (RUF051) Jul 6, 2026
@ntBre
ntBre enabled auto-merge (squash) July 6, 2026 22:03
@ntBre
ntBre merged commit 78dc89f into astral-sh:main Jul 6, 2026
46 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants