Skip to content

Commit ad48578

Browse files
bpo-43413: Revert changes in set.__init__ (pythonGH-28403)
Co-authored-by: Jason R. Coombs <[email protected]>
1 parent 1944434 commit ad48578

File tree

4 files changed

+8
-6
lines changed

4 files changed

+8
-6
lines changed

Lib/test/test_set.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -667,10 +667,13 @@ def __new__(cls, arg, newarg=None):
667667
self = super().__new__(cls, arg)
668668
self.newarg = newarg
669669
return self
670-
u = subclass_with_new([1, 2], newarg=3)
670+
u = subclass_with_new([1, 2])
671671
self.assertIs(type(u), subclass_with_new)
672672
self.assertEqual(set(u), {1, 2})
673-
self.assertEqual(u.newarg, 3)
673+
self.assertIsNone(u.newarg)
674+
# disallow kwargs in __new__ only (https://bugs.python.org/issue43413#msg402000)
675+
with self.assertRaises(TypeError):
676+
subclass_with_new([1, 2], newarg=3)
674677

675678

676679
class TestFrozenSet(TestJointOps, unittest.TestCase):

Misc/NEWS.d/3.11.0a1.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -1140,7 +1140,7 @@ blocks. Patch by Pablo Galindo.
11401140
11411141
Constructors of subclasses of some builtin classes (e.g. :class:`tuple`,
11421142
:class:`list`, :class:`frozenset`) no longer accept arbitrary keyword
1143-
arguments. Subclass of :class:`set` can now define a ``__new__()`` method
1143+
arguments. [reverted in 3.11a4] Subclass of :class:`set` can now define a ``__new__()`` method
11441144
with additional keyword parameters without overriding also ``__init__()``.
11451145

11461146
..
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Revert changes in ``set.__init__``. Subclass of :class:`set` needs to define a ``__init__()`` method if it defines a ``__new__()`` method with additional keyword parameters.

Objects/setobject.c

+1-3
Original file line numberDiff line numberDiff line change
@@ -1946,9 +1946,7 @@ set_init(PySetObject *self, PyObject *args, PyObject *kwds)
19461946
{
19471947
PyObject *iterable = NULL;
19481948

1949-
if ((Py_IS_TYPE(self, &PySet_Type) ||
1950-
Py_TYPE(self)->tp_new == PySet_Type.tp_new) &&
1951-
!_PyArg_NoKeywords("set", kwds))
1949+
if (!_PyArg_NoKeywords("set", kwds))
19521950
return -1;
19531951
if (!PyArg_UnpackTuple(args, Py_TYPE(self)->tp_name, 0, 1, &iterable))
19541952
return -1;

0 commit comments

Comments
 (0)