Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Use standard terminology in class pattern error message
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".
  • Loading branch information
sharkdp committed Feb 11, 2026
commit 950c015cde8763e9d56deafb8f778e1decc8720d
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Clarify the error message raised when a class pattern is used to match on a
non-class object.
2 changes: 1 addition & 1 deletion Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ _PyEval_MatchClass(PyThreadState *tstate, PyObject *subject, PyObject *type,
Py_ssize_t nargs, PyObject *kwargs)
{
if (!PyType_Check(type)) {
const char *e = "called match pattern must be a class";
const char *e = "class pattern must refer to a class";
_PyErr_Format(tstate, PyExc_TypeError, e);
return NULL;
}
Expand Down
Loading