From b1b4e4c6aa5a6efb11dadbdfc327c61672536a3b Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Fri, 17 Sep 2021 09:01:30 +0300 Subject: [PATCH 1/2] bpo-43413: Revert changes in set.__init__ --- Lib/test/test_set.py | 6 ++++-- .../2021-05-30-16-37-47.bpo-43413.vYFPPC.rst | 3 +-- Objects/setobject.c | 4 +--- 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/Lib/test/test_set.py b/Lib/test/test_set.py index 65fda9e469bccc..dde9fb0b3e9b18 100644 --- a/Lib/test/test_set.py +++ b/Lib/test/test_set.py @@ -667,10 +667,12 @@ def __new__(cls, arg, newarg=None): self = super().__new__(cls, arg) self.newarg = newarg return self - u = subclass_with_new([1, 2], newarg=3) + u = subclass_with_new([1, 2]) self.assertIs(type(u), subclass_with_new) self.assertEqual(set(u), {1, 2}) - self.assertEqual(u.newarg, 3) + self.assertIsNone(u.newarg) + with self.assertRaises(TypeError): + subclass_with_new([1, 2], newarg=3) class TestFrozenSet(TestJointOps, unittest.TestCase): diff --git a/Misc/NEWS.d/next/Core and Builtins/2021-05-30-16-37-47.bpo-43413.vYFPPC.rst b/Misc/NEWS.d/next/Core and Builtins/2021-05-30-16-37-47.bpo-43413.vYFPPC.rst index 579b57ee2d4333..53fabe3ff1971d 100644 --- a/Misc/NEWS.d/next/Core and Builtins/2021-05-30-16-37-47.bpo-43413.vYFPPC.rst +++ b/Misc/NEWS.d/next/Core and Builtins/2021-05-30-16-37-47.bpo-43413.vYFPPC.rst @@ -1,4 +1,3 @@ Constructors of subclasses of some buitin classes (e.g. :class:`tuple`, :class:`list`, :class:`frozenset`) no longer accept arbitrary keyword -arguments. Subclass of :class:`set` can now define a ``__new__()`` method -with additional keyword parameters without overriding also ``__init__()``. +arguments. diff --git a/Objects/setobject.c b/Objects/setobject.c index af521b2baca41f..8542a3dc65353f 100644 --- a/Objects/setobject.c +++ b/Objects/setobject.c @@ -1946,9 +1946,7 @@ set_init(PySetObject *self, PyObject *args, PyObject *kwds) { PyObject *iterable = NULL; - if ((Py_IS_TYPE(self, &PySet_Type) || - Py_TYPE(self)->tp_new == PySet_Type.tp_new) && - !_PyArg_NoKeywords("set", kwds)) + if (!_PyArg_NoKeywords("set", kwds)) return -1; if (!PyArg_UnpackTuple(args, Py_TYPE(self)->tp_name, 0, 1, &iterable)) return -1; From 28c3abc1da1e9f3acacf89424aeb8afa9a38ca74 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Wed, 20 Oct 2021 22:35:36 +0300 Subject: [PATCH 2/2] Update Lib/test/test_set.py Co-authored-by: Jason R. Coombs --- Lib/test/test_set.py | 1 + 1 file changed, 1 insertion(+) diff --git a/Lib/test/test_set.py b/Lib/test/test_set.py index dde9fb0b3e9b18..77f3da40c063ab 100644 --- a/Lib/test/test_set.py +++ b/Lib/test/test_set.py @@ -671,6 +671,7 @@ def __new__(cls, arg, newarg=None): self.assertIs(type(u), subclass_with_new) self.assertEqual(set(u), {1, 2}) self.assertIsNone(u.newarg) + # disallow kwargs in __new__ only (https://bugs.python.org/issue43413#msg402000) with self.assertRaises(TypeError): subclass_with_new([1, 2], newarg=3)