mirror of
https://github.com/python/cpython.git
synced 2025-12-11 11:31:05 +00:00
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.
This commit is contained in:
parent
3c1471d971
commit
5be872350d
1 changed files with 8 additions and 10 deletions
|
|
@ -736,7 +736,7 @@ class ABCTestCase(unittest.TestCase):
|
||||||
stubs = methodstubs.copy()
|
stubs = methodstubs.copy()
|
||||||
del stubs[name]
|
del stubs[name]
|
||||||
C = type('C', (abc,), stubs)
|
C = type('C', (abc,), stubs)
|
||||||
self.assertRaises(TypeError, C, name)
|
self.assertRaises(TypeError, C)
|
||||||
|
|
||||||
def validate_isinstance(self, abc, name):
|
def validate_isinstance(self, abc, name):
|
||||||
stub = lambda s, *args: 0
|
stub = lambda s, *args: 0
|
||||||
|
|
@ -963,7 +963,7 @@ class TestOneTrickPonyABCs(ABCTestCase):
|
||||||
async def __anext__(self):
|
async def __anext__(self):
|
||||||
raise StopAsyncIteration
|
raise StopAsyncIteration
|
||||||
self.assertNotIsInstance(AnextOnly(), AsyncIterator)
|
self.assertNotIsInstance(AnextOnly(), AsyncIterator)
|
||||||
self.validate_abstract_methods(AsyncIterator, '__anext__', '__aiter__')
|
self.validate_abstract_methods(AsyncIterator, '__anext__')
|
||||||
|
|
||||||
def test_Iterable(self):
|
def test_Iterable(self):
|
||||||
# Check some non-iterables
|
# Check some non-iterables
|
||||||
|
|
@ -1159,7 +1159,7 @@ class TestOneTrickPonyABCs(ABCTestCase):
|
||||||
for x in samples:
|
for x in samples:
|
||||||
self.assertIsInstance(x, Iterator)
|
self.assertIsInstance(x, Iterator)
|
||||||
self.assertIsSubclass(type(x), Iterator)
|
self.assertIsSubclass(type(x), Iterator)
|
||||||
self.validate_abstract_methods(Iterator, '__next__', '__iter__')
|
self.validate_abstract_methods(Iterator, '__next__')
|
||||||
|
|
||||||
# Issue 10565
|
# Issue 10565
|
||||||
class NextOnly:
|
class NextOnly:
|
||||||
|
|
@ -1843,8 +1843,7 @@ class TestCollectionABCs(ABCTestCase):
|
||||||
for sample in [dict]:
|
for sample in [dict]:
|
||||||
self.assertIsInstance(sample(), Mapping)
|
self.assertIsInstance(sample(), Mapping)
|
||||||
self.assertIsSubclass(sample, Mapping)
|
self.assertIsSubclass(sample, Mapping)
|
||||||
self.validate_abstract_methods(Mapping, '__contains__', '__iter__', '__len__',
|
self.validate_abstract_methods(Mapping, '__iter__', '__len__', '__getitem__')
|
||||||
'__getitem__')
|
|
||||||
class MyMapping(Mapping):
|
class MyMapping(Mapping):
|
||||||
def __len__(self):
|
def __len__(self):
|
||||||
return 0
|
return 0
|
||||||
|
|
@ -1859,7 +1858,7 @@ class TestCollectionABCs(ABCTestCase):
|
||||||
for sample in [dict]:
|
for sample in [dict]:
|
||||||
self.assertIsInstance(sample(), MutableMapping)
|
self.assertIsInstance(sample(), MutableMapping)
|
||||||
self.assertIsSubclass(sample, MutableMapping)
|
self.assertIsSubclass(sample, MutableMapping)
|
||||||
self.validate_abstract_methods(MutableMapping, '__contains__', '__iter__', '__len__',
|
self.validate_abstract_methods(MutableMapping, '__iter__', '__len__',
|
||||||
'__getitem__', '__setitem__', '__delitem__')
|
'__getitem__', '__setitem__', '__delitem__')
|
||||||
|
|
||||||
def test_MutableMapping_subclass(self):
|
def test_MutableMapping_subclass(self):
|
||||||
|
|
@ -1898,8 +1897,7 @@ class TestCollectionABCs(ABCTestCase):
|
||||||
self.assertIsInstance(memoryview(b""), Sequence)
|
self.assertIsInstance(memoryview(b""), Sequence)
|
||||||
self.assertIsSubclass(memoryview, Sequence)
|
self.assertIsSubclass(memoryview, Sequence)
|
||||||
self.assertIsSubclass(str, Sequence)
|
self.assertIsSubclass(str, Sequence)
|
||||||
self.validate_abstract_methods(Sequence, '__contains__', '__iter__', '__len__',
|
self.validate_abstract_methods(Sequence, '__len__', '__getitem__')
|
||||||
'__getitem__')
|
|
||||||
|
|
||||||
def test_Sequence_mixins(self):
|
def test_Sequence_mixins(self):
|
||||||
class SequenceSubclass(Sequence):
|
class SequenceSubclass(Sequence):
|
||||||
|
|
@ -1954,8 +1952,8 @@ class TestCollectionABCs(ABCTestCase):
|
||||||
self.assertIsSubclass(sample, MutableSequence)
|
self.assertIsSubclass(sample, MutableSequence)
|
||||||
self.assertIsSubclass(array.array, MutableSequence)
|
self.assertIsSubclass(array.array, MutableSequence)
|
||||||
self.assertNotIsSubclass(str, MutableSequence)
|
self.assertNotIsSubclass(str, MutableSequence)
|
||||||
self.validate_abstract_methods(MutableSequence, '__contains__', '__iter__',
|
self.validate_abstract_methods(MutableSequence, '__len__', '__getitem__',
|
||||||
'__len__', '__getitem__', '__setitem__', '__delitem__', 'insert')
|
'__setitem__', '__delitem__', 'insert')
|
||||||
|
|
||||||
def test_MutableSequence_mixins(self):
|
def test_MutableSequence_mixins(self):
|
||||||
# Test the mixins of MutableSequence by creating a minimal concrete
|
# Test the mixins of MutableSequence by creating a minimal concrete
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue