Skip to content

gh-144702: Use standard terminology in class pattern error message - #144703

Merged
AlexWaygood merged 1 commit into
python:mainfrom
sharkdp:error-message-class-pattern
Feb 19, 2026
Merged

AlexWaygood merged 1 commit into
python:mainfrom
sharkdp:error-message-class-pattern

Conversation

@sharkdp

@sharkdp sharkdp commented Feb 11, 2026 •

Copy link
Copy Markdown
Contributor

When trying to use a class pattern to match on a non-class object, we currently get the following error message:

Traceback (most recent call last):
  File "/tmp/test.py", line 2, in <module>
    case len():
         ~~~^^
TypeError: called match pattern must be a class

The terminology "called match pattern" here is a bit confusing. It's not used anywhere else as far as I can tell, and for a beginner, it might read like something is actually being called here.

The documentation consistently refers to these patterns as "class patterns":

Here, we change this message to "class pattern must refer to a class".

When trying to use a class pattern to match on a non-class object, we
currently get the following error message:

    Traceback (most recent call last):
      File "/tmp/test.py", line 2, in <module>
        case len():
             ~~~^^
    TypeError: called match pattern must be a class

The terminology "called match pattern" here is a bit confusing. It's not
used anywhere else as far as I can tell, and for a beginner, it might
read like something is actually being called here.

The documentation consistently refers to these patterns as "class
patterns":

* https://docs.python.org/3/reference/compound_stmts.html#class-patterns
* https://peps.python.org/pep-0635/#class-patterns
* https://docs.python.org/3/library/ast.html#ast.MatchClass
* https://docs.python.org/3/reference/datamodel.html#customizing-positional-arguments-in-class-pattern-matching

Here, we change this message to "class pattern must refer to a class".

@johnslavik johnslavik left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I assume we don't need a test. Without the entire check, _PyEval_MatchClass will still appropriately fail with a TypeError.

@AlexWaygood
AlexWaygood merged commit 1d09916 into python:main Feb 19, 2026
60 checks passed
@AlexWaygood

Copy link
Copy Markdown
Member

Thanks for the PR! 😃

brijkapadia pushed a commit to brijkapadia/cpython that referenced this pull request Feb 28, 2026
ljfp pushed a commit to ljfp/cpython that referenced this pull request Apr 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

3 participants