summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_tuple.py
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2021-09-12 10:27:50 (GMT)
committerGitHub <noreply@github.com>2021-09-12 10:27:50 (GMT)
commit92bf8691fb78f3484bf2daba836c416efedb1d8d (patch)
treef5e605dbb607ec58daa687300a5f59f99dd1aee4 /Lib/test/test_tuple.py
parent5277ffe12d492939544ff9c54a3aaf448b913fb3 (diff)
downloadcpython-92bf8691fb78f3484bf2daba836c416efedb1d8d.zip
cpython-92bf8691fb78f3484bf2daba836c416efedb1d8d.tar.gz
cpython-92bf8691fb78f3484bf2daba836c416efedb1d8d.tar.bz2
bpo-43413: Fix handling keyword arguments in subclasses of some buitin classes (GH-26456)
* Constructors of subclasses of some buitin classes (e.g. tuple, list, frozenset) no longer accept arbitrary keyword arguments. * Subclass of set can now define a __new__() method with additional keyword parameters without overriding also __init__().
Diffstat (limited to 'Lib/test/test_tuple.py')
-rw-r--r--Lib/test/test_tuple.py27
1 files changed, 27 insertions, 0 deletions
diff --git a/Lib/test/test_tuple.py b/Lib/test/test_tuple.py
index d2a2ed3..9ce80c5 100644
--- a/Lib/test/test_tuple.py
+++ b/Lib/test/test_tuple.py
@@ -42,6 +42,33 @@ class TupleTest(seq_tests.CommonTest):
with self.assertRaisesRegex(TypeError, 'keyword argument'):
tuple(sequence=())
+ def test_keywords_in_subclass(self):
+ class subclass(tuple):
+ pass
+ u = subclass([1, 2])
+ self.assertIs(type(u), subclass)
+ self.assertEqual(list(u), [1, 2])
+ with self.assertRaises(TypeError):
+ subclass(sequence=())
+
+ class subclass_with_init(tuple):
+ def __init__(self, arg, newarg=None):
+ self.newarg = newarg
+ u = subclass_with_init([1, 2], newarg=3)
+ self.assertIs(type(u), subclass_with_init)
+ self.assertEqual(list(u), [1, 2])
+ self.assertEqual(u.newarg, 3)
+
+ class subclass_with_new(tuple):
+ def __new__(cls, arg, newarg=None):
+ self = super().__new__(cls, arg)
+ self.newarg = newarg
+ return self
+ u = subclass_with_new([1, 2], newarg=3)
+ self.assertIs(type(u), subclass_with_new)
+ self.assertEqual(list(u), [1, 2])
+ self.assertEqual(u.newarg, 3)
+
def test_truth(self):
super().test_truth()
self.assertTrue(not ())