Skip to content

[pep8-naming] Check naming conventions in match pattern bindings (N806, N815, N816)#23899

Merged
ntBre merged 3 commits intoastral-sh:mainfrom
Acelogic:fix/naming-rules-match-patterns
Mar 12, 2026
Merged

[pep8-naming] Check naming conventions in match pattern bindings (N806, N815, N816)#23899
ntBre merged 3 commits intoastral-sh:mainfrom
Acelogic:fix/naming-rules-match-patterns

Conversation

@Acelogic
Copy link
Copy Markdown
Contributor

Summary

Fixes #16502

N806, N815, and N816 had false negatives for variables bound in match/case patterns. The rules only checked Expr::Name nodes with ExprContext::Store, but match patterns bind variables through Pattern nodes (MatchAs, MatchStar, MatchMapping rest captures), which were not checked.

Changes

  • Refactored rule functions to accept TextRange instead of &Expr (they only used expr.range())
  • Added analyze::pattern module that extracts bound names from match patterns and runs the same naming checks
  • Wired the analysis into the existing visit_pattern visitor method

Now all three rules flag violations in:

  • case Name: (capture pattern)
  • case int(Name): (class pattern keyword)
  • case [*Name]: (star pattern)
  • case pattern as Name: (as pattern)
  • case {"key": 1, **Name}: (mapping rest)

Test plan

  • Added test cases to N806.py, N815.py, and N816.py fixture files
  • Updated snapshots showing new violations detected
  • All 52 pep8_naming tests pass
  • Clippy clean (cargo clippy -p ruff_linter -- -D warnings)

…815, N816)

N806, N815, and N816 only checked variable names in assignment
expressions (Expr::Name with Store context), missing variables
bound through match/case patterns (MatchAs, MatchStar, and
MatchMapping rest captures).

Refactor the three rule functions to accept TextRange instead of
&Expr (they only used expr.range()), add an analyze::pattern module
that extracts bound names from patterns and runs the same checks,
and wire it into the visitor's visit_pattern method.

Fixes astral-sh#16502
@astral-sh-bot astral-sh-bot bot requested a review from ntBre March 11, 2026 19:27
@ntBre ntBre added the bug Something isn't working label Mar 12, 2026
@ntBre
Copy link
Copy Markdown
Contributor

ntBre commented Mar 12, 2026

(Sorry clicked the wrong button in the refined-github menu and reverted changes to a file)

@astral-sh-bot
Copy link
Copy Markdown

astral-sh-bot bot commented Mar 12, 2026

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@ntBre ntBre changed the title fix: check naming conventions in match/case pattern bindings (N806, N815, N816) [pep8-naming] Check naming conventions in match pattern bindings (N806, N815, N816) Mar 12, 2026
Copy link
Copy Markdown
Contributor

@ntBre ntBre left a comment

Choose a reason for hiding this comment

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

Makes sense to me, thank you!

@ntBre ntBre merged commit 728b9d6 into astral-sh:main Mar 12, 2026
43 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

N806, N815, and N816 ignore case patterns

2 participants