Skip to content

Conversation

cdce8p
Copy link
Member

@cdce8p cdce8p commented Sep 24, 2025

Description

Followup to #10559

Add additional checks for match class patterns.

  • match-class-bind-self
  • match-class-positional-attributes

@cdce8p cdce8p added this to the 4.0.0 milestone Sep 24, 2025
@cdce8p cdce8p added Enhancement ✨ Improvement to a component Match case labels Sep 24, 2025
@cdce8p cdce8p force-pushed the additional-match-checks branch from f6d8d9d to 42b289d Compare September 24, 2025 20:29
Copy link

codecov bot commented Sep 24, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.95%. Comparing base (68ab16f) to head (f420f28).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main   #10587   +/-   ##
=======================================
  Coverage   95.95%   95.95%           
=======================================
  Files         176      176           
  Lines       19455    19469   +14     
=======================================
+ Hits        18668    18682   +14     
  Misses        787      787           
Files with missing lines Coverage Δ
pylint/checkers/match_statements_checker.py 100.00% <100.00%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

This comment has been minimized.

Comment on lines 189 to 198
inferred = safe_infer(node.cls)
if not (
isinstance(inferred, nodes.ClassDef)
and inferred.qname() in MATCH_CLASS_SELF_NAMES
):
self.add_message(
"match-class-positional-attributes",
node=node,
confidence=HIGH,
)
Copy link
Member Author

@cdce8p cdce8p Sep 24, 2025

Choose a reason for hiding this comment

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

Looking at the sentry results, I do wonder if this is to strict and we'd end up emitting too many false positives. self matches also work for every subclass of the special builtin classes (as long as they don't implement __match_args__). Most notably NamedTuple.

Copy link
Member Author

Choose a reason for hiding this comment

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

Added some special casing for tuple that should handle it.

This comment has been minimized.

@cdce8p cdce8p force-pushed the additional-match-checks branch from 3ff329c to 7c078e5 Compare September 24, 2025 23:01
@cdce8p cdce8p force-pushed the additional-match-checks branch from 7c078e5 to 768c4c9 Compare September 24, 2025 23:02

This comment has been minimized.

"can be avoided.",
),
"R1906": (
"Use keyword attributes instead of positional ones",
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
"Use keyword attributes instead of positional ones",
"Use keyword attributes instead of positional ones ('%s')",

I think we can construct the expected here too. (And one day use it to autofix?)

case {"type": "user", "data": user_data}:
match user_data: # Nested match adds complexity
case {"name": str(name)}:
case {"name": str() as name}:
Copy link
Member

Choose a reason for hiding this comment

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

This check already making pylint better 😄

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed most of the pylint code in #10580 already. That's why there are "only" two instances in a test case left here.

This comment has been minimized.

This comment has been minimized.

@cdce8p cdce8p force-pushed the additional-match-checks branch from 18c133e to f420f28 Compare September 25, 2025 11:07
Copy link
Contributor

🤖 According to the primer, this change has no effect on the checked open source code. 🤖🎉

This comment was generated for commit f420f28

"attribute in a class pattern.",
),
"R1905": (
"Use '%s() as %s' instead",
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
"Use '%s() as %s' instead",
"Use '%s() as %s' instead, if performance is a concern",

(This is a suggestion, the message is short, there's room for explanation in it, maybe ?)

Copy link
Member Author

Choose a reason for hiding this comment

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

Seems Daniel merged it before I saw your comment 😄
Anyway, not sure we need an extra comment here. I did think about moving these checks to an extension (e.g. CodeStyle. However, there are clear performance benefits which is why I decided to keep them always enabled.

If we get reports about them, we could still consider amending the message or moving the check.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Oops, sorry! I saw Pierre's approval and thought it was a comment from his earlier review.

Will be more careful next time!

Copy link
Member

Choose a reason for hiding this comment

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

No problem, it was good to merge which is why I approved in the first place 😄

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.

LGTM, great checker !

@DanielNoord DanielNoord merged commit 19e0f3e into pylint-dev:main Sep 26, 2025
44 checks passed
@cdce8p cdce8p deleted the additional-match-checks branch September 26, 2025 19:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement ✨ Improvement to a component Match case
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants