Add test case for duplicate keyword attributes

This commit is contained in:
Marc Mueller 2025-12-21 23:28:26 +01:00
parent 640b205517
commit 9375e2b27e
No known key found for this signature in database
2 changed files with 8 additions and 1 deletions

View file

@ -2345,6 +2345,12 @@ Invalid pattern matching constructs:
Traceback (most recent call last):
SyntaxError: positional patterns follow keyword patterns
>>> match ...:
... case Foo(y=1, x=2, y=3):
... ...
Traceback (most recent call last):
SyntaxError: attribute name repeated in class pattern: y
>>> match ...:
... case C(a=b, c, d=e, f, g=h, i, j=k, ...):
... ...

View file

@ -887,7 +887,8 @@ _PyEval_MatchClass(PyThreadState *tstate, PyObject *subject, PyObject *type,
// So far so good:
PyObject *seen = NULL;
// Only check for duplicates if there is at least one positional attribute
// and two or more attributes in total.
// and two or more attributes in total. Duplicate keyword attributes are
// detected during the compile stage and raise a SyntaxError.
if (nargs > 0 && nattrs > 1) {
seen = PySet_New(NULL);
if (seen == NULL) {