Skip to content

Conversation

cdce8p
Copy link
Collaborator

@cdce8p cdce8p commented Aug 21, 2025

Since the class pattern matches any subclass, it can also be used to check whether the matched object has a specific attribute. Mypy should not emit an error for it.

match m:
    case object(foo=_):
        m.foo

Using object for it is recommended in PEP 635 and more prominently in the precursor PEP 622.

@cdce8p cdce8p added the topic-match-statement Python 3.10's match statement label Aug 21, 2025

This comment has been minimized.

Copy link
Contributor

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

@cdce8p cdce8p requested a review from ilevkivskyi August 26, 2025 00:11
Copy link
Member

@ilevkivskyi ilevkivskyi left a comment

Choose a reason for hiding this comment

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

LG, this is essentially the same as hasattr().

@ilevkivskyi ilevkivskyi merged commit 50fc847 into python:master Aug 26, 2025
20 checks passed
@cdce8p cdce8p deleted the match-class-object branch August 26, 2025 00:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic-match-statement Python 3.10's match statement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants