Skip to content

Commit 9375e2b

Browse files
committed
Add test case for duplicate keyword attributes
1 parent 640b205 commit 9375e2b

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

Lib/test/test_syntax.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2345,6 +2345,12 @@
23452345
Traceback (most recent call last):
23462346
SyntaxError: positional patterns follow keyword patterns
23472347
2348+
>>> match ...:
2349+
... case Foo(y=1, x=2, y=3):
2350+
... ...
2351+
Traceback (most recent call last):
2352+
SyntaxError: attribute name repeated in class pattern: y
2353+
23482354
>>> match ...:
23492355
... case C(a=b, c, d=e, f, g=h, i, j=k, ...):
23502356
... ...

Python/ceval.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -887,7 +887,8 @@ _PyEval_MatchClass(PyThreadState *tstate, PyObject *subject, PyObject *type,
887887
// So far so good:
888888
PyObject *seen = NULL;
889889
// Only check for duplicates if there is at least one positional attribute
890-
// and two or more attributes in total.
890+
// and two or more attributes in total. Duplicate keyword attributes are
891+
// detected during the compile stage and raise a SyntaxError.
891892
if (nargs > 0 && nattrs > 1) {
892893
seen = PySet_New(NULL);
893894
if (seen == NULL) {

0 commit comments

Comments
 (0)