Skip to content

Add unreachable-match-patterns message and new checker. #10424

New issue

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

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

Already on GitHub? # to your account

Open
wants to merge 5 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.87%. Comparing base (a3e5bef) to head (6488491).
Report is 100 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main   #10424   +/-   ##
=======================================
  Coverage   95.86%   95.87%           
=======================================
  Files         175      177    +2     
  Lines       19074    19153   +79     
=======================================
+ Hits        18286    18363   +77     
- Misses        788      790    +2     
Files with missing lines Coverage Ξ”
pylint/checkers/match_statements_checker.py 100.00% <100.00%> (ΓΈ)

... and 24 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
``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
@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!

and case.pattern.pattern is None
and isinstance(case.pattern.name, nodes.AssignName)
and idx < len(node.cases) - 1
and self._py310_plus
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why check the version? Shouldn't this always be checked if we find these patterns?

Copy link
Member Author

@mbyrnepr2 mbyrnepr2 Jun 16, 2025

Choose a reason for hiding this comment

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

Why check the version?

My mistake. We shouldn't check it. I must have been thinking that this line would prevent an error from occurring if Pylint was run in an environment using Python 3.9 or lower since the match statement (and the ast.Match node) is only available in Python 3.10 and higher.

The solution would be to remove this line and wait until Python 3.9 is end of life before considering a merge?

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
# for free to join this conversation on GitHub. Already have an account? # 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
3 participants