Skip to content

Commit 5be8723

Browse files
gh-137463: Update validate_abstract_methods in test_collections.py (#137464)
Update `validate_abstract_methods` in `test_collections.py` The test for missing abstract methods in `validate_abstract_methods` incorrectly attempted to instantiate the generated class `C` with an argument (`C(name)`), which always raises a `TypeError: C() takes no arguments`. Although the test originally passes, it passes for the wrong reason. This change makes the test correctly validate the enforcement of abstract methods in ABCs.
1 parent 3c1471d commit 5be8723

File tree

1 file changed

+8
-10
lines changed

1 file changed

+8
-10
lines changed

Lib/test/test_collections.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -736,7 +736,7 @@ def validate_abstract_methods(self, abc, *names):
736736
stubs = methodstubs.copy()
737737
del stubs[name]
738738
C = type('C', (abc,), stubs)
739-
self.assertRaises(TypeError, C, name)
739+
self.assertRaises(TypeError, C)
740740

741741
def validate_isinstance(self, abc, name):
742742
stub = lambda s, *args: 0
@@ -963,7 +963,7 @@ class AnextOnly:
963963
async def __anext__(self):
964964
raise StopAsyncIteration
965965
self.assertNotIsInstance(AnextOnly(), AsyncIterator)
966-
self.validate_abstract_methods(AsyncIterator, '__anext__', '__aiter__')
966+
self.validate_abstract_methods(AsyncIterator, '__anext__')
967967

968968
def test_Iterable(self):
969969
# Check some non-iterables
@@ -1159,7 +1159,7 @@ def test_Iterator(self):
11591159
for x in samples:
11601160
self.assertIsInstance(x, Iterator)
11611161
self.assertIsSubclass(type(x), Iterator)
1162-
self.validate_abstract_methods(Iterator, '__next__', '__iter__')
1162+
self.validate_abstract_methods(Iterator, '__next__')
11631163

11641164
# Issue 10565
11651165
class NextOnly:
@@ -1843,8 +1843,7 @@ def test_Mapping(self):
18431843
for sample in [dict]:
18441844
self.assertIsInstance(sample(), Mapping)
18451845
self.assertIsSubclass(sample, Mapping)
1846-
self.validate_abstract_methods(Mapping, '__contains__', '__iter__', '__len__',
1847-
'__getitem__')
1846+
self.validate_abstract_methods(Mapping, '__iter__', '__len__', '__getitem__')
18481847
class MyMapping(Mapping):
18491848
def __len__(self):
18501849
return 0
@@ -1859,7 +1858,7 @@ def test_MutableMapping(self):
18591858
for sample in [dict]:
18601859
self.assertIsInstance(sample(), MutableMapping)
18611860
self.assertIsSubclass(sample, MutableMapping)
1862-
self.validate_abstract_methods(MutableMapping, '__contains__', '__iter__', '__len__',
1861+
self.validate_abstract_methods(MutableMapping, '__iter__', '__len__',
18631862
'__getitem__', '__setitem__', '__delitem__')
18641863

18651864
def test_MutableMapping_subclass(self):
@@ -1898,8 +1897,7 @@ def test_Sequence(self):
18981897
self.assertIsInstance(memoryview(b""), Sequence)
18991898
self.assertIsSubclass(memoryview, Sequence)
19001899
self.assertIsSubclass(str, Sequence)
1901-
self.validate_abstract_methods(Sequence, '__contains__', '__iter__', '__len__',
1902-
'__getitem__')
1900+
self.validate_abstract_methods(Sequence, '__len__', '__getitem__')
19031901

19041902
def test_Sequence_mixins(self):
19051903
class SequenceSubclass(Sequence):
@@ -1954,8 +1952,8 @@ def test_MutableSequence(self):
19541952
self.assertIsSubclass(sample, MutableSequence)
19551953
self.assertIsSubclass(array.array, MutableSequence)
19561954
self.assertNotIsSubclass(str, MutableSequence)
1957-
self.validate_abstract_methods(MutableSequence, '__contains__', '__iter__',
1958-
'__len__', '__getitem__', '__setitem__', '__delitem__', 'insert')
1955+
self.validate_abstract_methods(MutableSequence, '__len__', '__getitem__',
1956+
'__setitem__', '__delitem__', 'insert')
19591957

19601958
def test_MutableSequence_mixins(self):
19611959
# Test the mixins of MutableSequence by creating a minimal concrete

0 commit comments

Comments
 (0)