Skip to content

Add new checker and bare-name-capture-pattern message. #10424

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 16 commits into
base: main
Choose a base branch
from

Conversation

mbyrnepr2
Copy link
Member

Type of Changes

Type
πŸ› Bug fix
βœ“ ✨ New feature
πŸ”¨ Refactoring
πŸ“œ Docs

Description

This will emit an error message when a name capture pattern is used in a match statement which would make the remaining patterns unreachable. This code is a SyntaxError at runtime.

Closes #7128

Copy link

codecov bot commented Jun 11, 2025

Codecov Report

βœ… All modified and coverable lines are covered by tests.
βœ… Project coverage is 95.85%. Comparing base (707323a) to head (034d78f).
⚠️ Report is 23 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main   #10424      +/-   ##
==========================================
- Coverage   95.86%   95.85%   -0.02%     
==========================================
  Files         176      177       +1     
  Lines       19155    19197      +42     
==========================================
+ Hits        18363    18401      +38     
- Misses        792      796       +4     
Files with missing lines Coverage Ξ”
pylint/checkers/match_statements_checker.py 100.00% <100.00%> (ΓΈ)

... and 7 files with indirect coverage changes

πŸš€ New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Pierre-Sassoulas Pierre-Sassoulas added the Enhancement ✨ Improvement to a component label Jun 11, 2025
@mbyrnepr2 mbyrnepr2 force-pushed the 7128_remaining_patterns_unreachable branch from f1ca097 to 0e5ef7c Compare June 12, 2025 08:54
@mbyrnepr2 mbyrnepr2 changed the title Add remaining-patterns-unreachable checker. Add unreachable-match-patterns message and new checker. Jun 12, 2025
@mbyrnepr2 mbyrnepr2 marked this pull request as ready for review June 12, 2025 14:32
Copy link
Collaborator

@DanielNoord DanielNoord left a comment

Choose a reason for hiding this comment

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

Nice check!

DanielNoord
DanielNoord previously approved these changes Jun 17, 2025
Copy link
Collaborator

@DanielNoord DanielNoord left a comment

Choose a reason for hiding this comment

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

Seems very sensible to me!

@Pierre-Sassoulas Pierre-Sassoulas added this to the 4.0.0 milestone Jun 17, 2025
Copy link
Member

@Pierre-Sassoulas Pierre-Sassoulas left a comment

Choose a reason for hiding this comment

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

Let's put this in 4.0 once the remarks are handled !

This comment has been minimized.

mbyrnepr2 and others added 12 commits August 3, 2025 14:39
``unreachable-match-patterns``.

This will emit an error message when a name capture pattern is used
in a match statement which would make the remaining patterns unreachable.
This code is a SyntaxError at runtime.

Closes pylint-dev#7128
ID. (pylint/script/get_unused_message_id_category.py).
The example intentionally contains a `SyntaxError` which will also raise
a `SyntaxError` from Ruff.
Co-authored-by: Pierre Sassoulas <[email protected]>
@mbyrnepr2 mbyrnepr2 force-pushed the 7128_remaining_patterns_unreachable branch from ad3ec16 to 26a0f0a Compare August 3, 2025 12:42

This comment has been minimized.

@mbyrnepr2 mbyrnepr2 changed the title Add unreachable-match-patterns message and new checker. Add new checker and bare-name-capture-pattern message. Aug 4, 2025

This comment has been minimized.

Copy link
Member

@Pierre-Sassoulas Pierre-Sassoulas left a comment

Choose a reason for hiding this comment

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

Great work ! I'll let @jacobtylerwalls merge

This comment has been minimized.

Copy link
Member

@cdce8p cdce8p left a comment

Choose a reason for hiding this comment

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

Thanks for working on it @mbyrnepr2! This looks like a great addition.

I saw the discussion where the error should be emitted. Just to provide one more data point, pylance has a similar error message: Irrefutable pattern is allowed only for the last case statement which is emitted for the cases which currently trigger the check.

There is also another message Pattern will never be matched for subject type "Never" for the second and third cases but that's likely based on the type inference result.

Copy link
Contributor

πŸ€– According to the primer, this change has no effect on the checked open source code. πŸ€–πŸŽ‰

This comment was generated for commit 034d78f

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement ✨ Improvement to a component
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Check for name capture syntax errors
5 participants