Skip to content

Commit 950c015

Browse files
committed
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".
1 parent b22ff1e commit 950c015

File tree

2 files changed

+3
-1
lines changed

2 files changed

+3
-1
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Clarify the error message raised when a class pattern is used to match on a
2+
non-class object.

Python/ceval.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,7 @@ _PyEval_MatchClass(PyThreadState *tstate, PyObject *subject, PyObject *type,
531531
Py_ssize_t nargs, PyObject *kwargs)
532532
{
533533
if (!PyType_Check(type)) {
534-
const char *e = "called match pattern must be a class";
534+
const char *e = "class pattern must refer to a class";
535535
_PyErr_Format(tstate, PyExc_TypeError, e);
536536
return NULL;
537537
}

0 commit comments

Comments
 (0)