gh-71339: Use assertIsSubclass() and assertNotIsSubclass() in test_collections (GH-128824)

This commit is contained in:
Serhiy Storchaka 2025-01-20 11:16:32 +02:00 committed by GitHub
parent 4967fa6a9c
commit 887f2bcf48
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -742,11 +742,11 @@ class ABCTestCase(unittest.TestCase):
C = type('C', (object,), {'__hash__': None})
setattr(C, name, stub)
self.assertIsInstance(C(), abc)
self.assertTrue(issubclass(C, abc))
self.assertIsSubclass(C, abc)
C = type('C', (object,), {'__hash__': None})
self.assertNotIsInstance(C(), abc)
self.assertFalse(issubclass(C, abc))
self.assertNotIsSubclass(C, abc)
def validate_comparison(self, instance):
ops = ['lt', 'gt', 'le', 'ge', 'ne', 'or', 'and', 'xor', 'sub']
@ -812,12 +812,12 @@ class TestOneTrickPonyABCs(ABCTestCase):
non_samples = [None, int(), gen(), object()]
for x in non_samples:
self.assertNotIsInstance(x, Awaitable)
self.assertFalse(issubclass(type(x), Awaitable), repr(type(x)))
self.assertNotIsSubclass(type(x), Awaitable)
samples = [Bar(), MinimalCoro()]
for x in samples:
self.assertIsInstance(x, Awaitable)
self.assertTrue(issubclass(type(x), Awaitable))
self.assertIsSubclass(type(x), Awaitable)
c = coro()
# Iterable coroutines (generators with CO_ITERABLE_COROUTINE
@ -831,8 +831,8 @@ class TestOneTrickPonyABCs(ABCTestCase):
class CoroLike: pass
Coroutine.register(CoroLike)
self.assertTrue(isinstance(CoroLike(), Awaitable))
self.assertTrue(issubclass(CoroLike, Awaitable))
self.assertIsInstance(CoroLike(), Awaitable)
self.assertIsSubclass(CoroLike, Awaitable)
CoroLike = None
support.gc_collect() # Kill CoroLike to clean-up ABCMeta cache
@ -864,12 +864,12 @@ class TestOneTrickPonyABCs(ABCTestCase):
non_samples = [None, int(), gen(), object(), Bar()]
for x in non_samples:
self.assertNotIsInstance(x, Coroutine)
self.assertFalse(issubclass(type(x), Coroutine), repr(type(x)))
self.assertNotIsSubclass(type(x), Coroutine)
samples = [MinimalCoro()]
for x in samples:
self.assertIsInstance(x, Awaitable)
self.assertTrue(issubclass(type(x), Awaitable))
self.assertIsSubclass(type(x), Awaitable)
c = coro()
# Iterable coroutines (generators with CO_ITERABLE_COROUTINE
@ -890,8 +890,8 @@ class TestOneTrickPonyABCs(ABCTestCase):
pass
def __await__(self):
pass
self.assertTrue(isinstance(CoroLike(), Coroutine))
self.assertTrue(issubclass(CoroLike, Coroutine))
self.assertIsInstance(CoroLike(), Coroutine)
self.assertIsSubclass(CoroLike, Coroutine)
class CoroLike:
def send(self, value):
@ -900,15 +900,15 @@ class TestOneTrickPonyABCs(ABCTestCase):
pass
def __await__(self):
pass
self.assertFalse(isinstance(CoroLike(), Coroutine))
self.assertFalse(issubclass(CoroLike, Coroutine))
self.assertNotIsInstance(CoroLike(), Coroutine)
self.assertNotIsSubclass(CoroLike, Coroutine)
def test_Hashable(self):
# Check some non-hashables
non_samples = [bytearray(), list(), set(), dict()]
for x in non_samples:
self.assertNotIsInstance(x, Hashable)
self.assertFalse(issubclass(type(x), Hashable), repr(type(x)))
self.assertNotIsSubclass(type(x), Hashable)
# Check some hashables
samples = [None,
int(), float(), complex(),
@ -918,14 +918,14 @@ class TestOneTrickPonyABCs(ABCTestCase):
]
for x in samples:
self.assertIsInstance(x, Hashable)
self.assertTrue(issubclass(type(x), Hashable), repr(type(x)))
self.assertIsSubclass(type(x), Hashable)
self.assertRaises(TypeError, Hashable)
# Check direct subclassing
class H(Hashable):
def __hash__(self):
return super().__hash__()
self.assertEqual(hash(H()), 0)
self.assertFalse(issubclass(int, H))
self.assertNotIsSubclass(int, H)
self.validate_abstract_methods(Hashable, '__hash__')
self.validate_isinstance(Hashable, '__hash__')
@ -933,13 +933,13 @@ class TestOneTrickPonyABCs(ABCTestCase):
class AI:
def __aiter__(self):
return self
self.assertTrue(isinstance(AI(), AsyncIterable))
self.assertTrue(issubclass(AI, AsyncIterable))
self.assertIsInstance(AI(), AsyncIterable)
self.assertIsSubclass(AI, AsyncIterable)
# Check some non-iterables
non_samples = [None, object, []]
for x in non_samples:
self.assertNotIsInstance(x, AsyncIterable)
self.assertFalse(issubclass(type(x), AsyncIterable), repr(type(x)))
self.assertNotIsSubclass(type(x), AsyncIterable)
self.validate_abstract_methods(AsyncIterable, '__aiter__')
self.validate_isinstance(AsyncIterable, '__aiter__')
@ -949,13 +949,13 @@ class TestOneTrickPonyABCs(ABCTestCase):
return self
async def __anext__(self):
raise StopAsyncIteration
self.assertTrue(isinstance(AI(), AsyncIterator))
self.assertTrue(issubclass(AI, AsyncIterator))
self.assertIsInstance(AI(), AsyncIterator)
self.assertIsSubclass(AI, AsyncIterator)
non_samples = [None, object, []]
# Check some non-iterables
for x in non_samples:
self.assertNotIsInstance(x, AsyncIterator)
self.assertFalse(issubclass(type(x), AsyncIterator), repr(type(x)))
self.assertNotIsSubclass(type(x), AsyncIterator)
# Similarly to regular iterators (see issue 10565)
class AnextOnly:
async def __anext__(self):
@ -968,7 +968,7 @@ class TestOneTrickPonyABCs(ABCTestCase):
non_samples = [None, 42, 3.14, 1j]
for x in non_samples:
self.assertNotIsInstance(x, Iterable)
self.assertFalse(issubclass(type(x), Iterable), repr(type(x)))
self.assertNotIsSubclass(type(x), Iterable)
# Check some iterables
samples = [bytes(), str(),
tuple(), list(), set(), frozenset(), dict(),
@ -978,13 +978,13 @@ class TestOneTrickPonyABCs(ABCTestCase):
]
for x in samples:
self.assertIsInstance(x, Iterable)
self.assertTrue(issubclass(type(x), Iterable), repr(type(x)))
self.assertIsSubclass(type(x), Iterable)
# Check direct subclassing
class I(Iterable):
def __iter__(self):
return super().__iter__()
self.assertEqual(list(I()), [])
self.assertFalse(issubclass(str, I))
self.assertNotIsSubclass(str, I)
self.validate_abstract_methods(Iterable, '__iter__')
self.validate_isinstance(Iterable, '__iter__')
# Check None blocking
@ -992,22 +992,22 @@ class TestOneTrickPonyABCs(ABCTestCase):
def __iter__(self): return iter([])
class ItBlocked(It):
__iter__ = None
self.assertTrue(issubclass(It, Iterable))
self.assertTrue(isinstance(It(), Iterable))
self.assertFalse(issubclass(ItBlocked, Iterable))
self.assertFalse(isinstance(ItBlocked(), Iterable))
self.assertIsSubclass(It, Iterable)
self.assertIsInstance(It(), Iterable)
self.assertNotIsSubclass(ItBlocked, Iterable)
self.assertNotIsInstance(ItBlocked(), Iterable)
def test_Reversible(self):
# Check some non-reversibles
non_samples = [None, 42, 3.14, 1j, set(), frozenset()]
for x in non_samples:
self.assertNotIsInstance(x, Reversible)
self.assertFalse(issubclass(type(x), Reversible), repr(type(x)))
self.assertNotIsSubclass(type(x), Reversible)
# Check some non-reversible iterables
non_reversibles = [_test_gen(), (x for x in []), iter([]), reversed([])]
for x in non_reversibles:
self.assertNotIsInstance(x, Reversible)
self.assertFalse(issubclass(type(x), Reversible), repr(type(x)))
self.assertNotIsSubclass(type(x), Reversible)
# Check some reversible iterables
samples = [bytes(), str(), tuple(), list(), OrderedDict(),
OrderedDict().keys(), OrderedDict().items(),
@ -1016,11 +1016,11 @@ class TestOneTrickPonyABCs(ABCTestCase):
dict().keys(), dict().items(), dict().values()]
for x in samples:
self.assertIsInstance(x, Reversible)
self.assertTrue(issubclass(type(x), Reversible), repr(type(x)))
self.assertIsSubclass(type(x), Reversible)
# Check also Mapping, MutableMapping, and Sequence
self.assertTrue(issubclass(Sequence, Reversible), repr(Sequence))
self.assertFalse(issubclass(Mapping, Reversible), repr(Mapping))
self.assertFalse(issubclass(MutableMapping, Reversible), repr(MutableMapping))
self.assertIsSubclass(Sequence, Reversible)
self.assertNotIsSubclass(Mapping, Reversible)
self.assertNotIsSubclass(MutableMapping, Reversible)
# Check direct subclassing
class R(Reversible):
def __iter__(self):
@ -1028,17 +1028,17 @@ class TestOneTrickPonyABCs(ABCTestCase):
def __reversed__(self):
return iter(list())
self.assertEqual(list(reversed(R())), [])
self.assertFalse(issubclass(float, R))
self.assertNotIsSubclass(float, R)
self.validate_abstract_methods(Reversible, '__reversed__', '__iter__')
# Check reversible non-iterable (which is not Reversible)
class RevNoIter:
def __reversed__(self): return reversed([])
class RevPlusIter(RevNoIter):
def __iter__(self): return iter([])
self.assertFalse(issubclass(RevNoIter, Reversible))
self.assertFalse(isinstance(RevNoIter(), Reversible))
self.assertTrue(issubclass(RevPlusIter, Reversible))
self.assertTrue(isinstance(RevPlusIter(), Reversible))
self.assertNotIsSubclass(RevNoIter, Reversible)
self.assertNotIsInstance(RevNoIter(), Reversible)
self.assertIsSubclass(RevPlusIter, Reversible)
self.assertIsInstance(RevPlusIter(), Reversible)
# Check None blocking
class Rev:
def __iter__(self): return iter([])
@ -1047,39 +1047,38 @@ class TestOneTrickPonyABCs(ABCTestCase):
__iter__ = None
class RevRevBlocked(Rev):
__reversed__ = None
self.assertTrue(issubclass(Rev, Reversible))
self.assertTrue(isinstance(Rev(), Reversible))
self.assertFalse(issubclass(RevItBlocked, Reversible))
self.assertFalse(isinstance(RevItBlocked(), Reversible))
self.assertFalse(issubclass(RevRevBlocked, Reversible))
self.assertFalse(isinstance(RevRevBlocked(), Reversible))
self.assertIsSubclass(Rev, Reversible)
self.assertIsInstance(Rev(), Reversible)
self.assertNotIsSubclass(RevItBlocked, Reversible)
self.assertNotIsInstance(RevItBlocked(), Reversible)
self.assertNotIsSubclass(RevRevBlocked, Reversible)
self.assertNotIsInstance(RevRevBlocked(), Reversible)
def test_Collection(self):
# Check some non-collections
non_collections = [None, 42, 3.14, 1j, lambda x: 2*x]
for x in non_collections:
self.assertNotIsInstance(x, Collection)
self.assertFalse(issubclass(type(x), Collection), repr(type(x)))
self.assertNotIsSubclass(type(x), Collection)
# Check some non-collection iterables
non_col_iterables = [_test_gen(), iter(b''), iter(bytearray()),
(x for x in [])]
for x in non_col_iterables:
self.assertNotIsInstance(x, Collection)
self.assertFalse(issubclass(type(x), Collection), repr(type(x)))
self.assertNotIsSubclass(type(x), Collection)
# Check some collections
samples = [set(), frozenset(), dict(), bytes(), str(), tuple(),
list(), dict().keys(), dict().items(), dict().values()]
for x in samples:
self.assertIsInstance(x, Collection)
self.assertTrue(issubclass(type(x), Collection), repr(type(x)))
self.assertIsSubclass(type(x), Collection)
# Check also Mapping, MutableMapping, etc.
self.assertTrue(issubclass(Sequence, Collection), repr(Sequence))
self.assertTrue(issubclass(Mapping, Collection), repr(Mapping))
self.assertTrue(issubclass(MutableMapping, Collection),
repr(MutableMapping))
self.assertTrue(issubclass(Set, Collection), repr(Set))
self.assertTrue(issubclass(MutableSet, Collection), repr(MutableSet))
self.assertTrue(issubclass(Sequence, Collection), repr(MutableSet))
self.assertIsSubclass(Sequence, Collection)
self.assertIsSubclass(Mapping, Collection)
self.assertIsSubclass(MutableMapping, Collection)
self.assertIsSubclass(Set, Collection)
self.assertIsSubclass(MutableSet, Collection)
self.assertIsSubclass(Sequence, Collection)
# Check direct subclassing
class Col(Collection):
def __iter__(self):
@ -1090,13 +1089,13 @@ class TestOneTrickPonyABCs(ABCTestCase):
return False
class DerCol(Col): pass
self.assertEqual(list(iter(Col())), [])
self.assertFalse(issubclass(list, Col))
self.assertFalse(issubclass(set, Col))
self.assertFalse(issubclass(float, Col))
self.assertNotIsSubclass(list, Col)
self.assertNotIsSubclass(set, Col)
self.assertNotIsSubclass(float, Col)
self.assertEqual(list(iter(DerCol())), [])
self.assertFalse(issubclass(list, DerCol))
self.assertFalse(issubclass(set, DerCol))
self.assertFalse(issubclass(float, DerCol))
self.assertNotIsSubclass(list, DerCol)
self.assertNotIsSubclass(set, DerCol)
self.assertNotIsSubclass(float, DerCol)
self.validate_abstract_methods(Collection, '__len__', '__iter__',
'__contains__')
# Check sized container non-iterable (which is not Collection) etc.
@ -1109,12 +1108,12 @@ class TestOneTrickPonyABCs(ABCTestCase):
class ColNoCont:
def __iter__(self): return iter([])
def __len__(self): return 0
self.assertFalse(issubclass(ColNoIter, Collection))
self.assertFalse(isinstance(ColNoIter(), Collection))
self.assertFalse(issubclass(ColNoSize, Collection))
self.assertFalse(isinstance(ColNoSize(), Collection))
self.assertFalse(issubclass(ColNoCont, Collection))
self.assertFalse(isinstance(ColNoCont(), Collection))
self.assertNotIsSubclass(ColNoIter, Collection)
self.assertNotIsInstance(ColNoIter(), Collection)
self.assertNotIsSubclass(ColNoSize, Collection)
self.assertNotIsInstance(ColNoSize(), Collection)
self.assertNotIsSubclass(ColNoCont, Collection)
self.assertNotIsInstance(ColNoCont(), Collection)
# Check None blocking
class SizeBlock:
def __iter__(self): return iter([])
@ -1124,10 +1123,10 @@ class TestOneTrickPonyABCs(ABCTestCase):
def __len__(self): return 0
def __contains__(self): return True
__iter__ = None
self.assertFalse(issubclass(SizeBlock, Collection))
self.assertFalse(isinstance(SizeBlock(), Collection))
self.assertFalse(issubclass(IterBlock, Collection))
self.assertFalse(isinstance(IterBlock(), Collection))
self.assertNotIsSubclass(SizeBlock, Collection)
self.assertNotIsInstance(SizeBlock(), Collection)
self.assertNotIsSubclass(IterBlock, Collection)
self.assertNotIsInstance(IterBlock(), Collection)
# Check None blocking in subclass
class ColImpl:
def __iter__(self):
@ -1138,15 +1137,15 @@ class TestOneTrickPonyABCs(ABCTestCase):
return False
class NonCol(ColImpl):
__contains__ = None
self.assertFalse(issubclass(NonCol, Collection))
self.assertFalse(isinstance(NonCol(), Collection))
self.assertNotIsSubclass(NonCol, Collection)
self.assertNotIsInstance(NonCol(), Collection)
def test_Iterator(self):
non_samples = [None, 42, 3.14, 1j, b"", "", (), [], {}, set()]
for x in non_samples:
self.assertNotIsInstance(x, Iterator)
self.assertFalse(issubclass(type(x), Iterator), repr(type(x)))
self.assertNotIsSubclass(type(x), Iterator)
samples = [iter(bytes()), iter(str()),
iter(tuple()), iter(list()), iter(dict()),
iter(set()), iter(frozenset()),
@ -1157,7 +1156,7 @@ class TestOneTrickPonyABCs(ABCTestCase):
]
for x in samples:
self.assertIsInstance(x, Iterator)
self.assertTrue(issubclass(type(x), Iterator), repr(type(x)))
self.assertIsSubclass(type(x), Iterator)
self.validate_abstract_methods(Iterator, '__next__', '__iter__')
# Issue 10565
@ -1190,7 +1189,7 @@ class TestOneTrickPonyABCs(ABCTestCase):
iter(()), iter([]), NonGen1(), NonGen2(), NonGen3()]
for x in non_samples:
self.assertNotIsInstance(x, Generator)
self.assertFalse(issubclass(type(x), Generator), repr(type(x)))
self.assertNotIsSubclass(type(x), Generator)
class Gen:
def __iter__(self): return self
@ -1212,7 +1211,7 @@ class TestOneTrickPonyABCs(ABCTestCase):
for x in samples:
self.assertIsInstance(x, Iterator)
self.assertIsInstance(x, Generator)
self.assertTrue(issubclass(type(x), Generator), repr(type(x)))
self.assertIsSubclass(type(x), Generator)
self.validate_abstract_methods(Generator, 'send', 'throw')
# mixin tests
@ -1261,7 +1260,7 @@ class TestOneTrickPonyABCs(ABCTestCase):
iter(()), iter([]), NonAGen1(), NonAGen2(), NonAGen3()]
for x in non_samples:
self.assertNotIsInstance(x, AsyncGenerator)
self.assertFalse(issubclass(type(x), AsyncGenerator), repr(type(x)))
self.assertNotIsSubclass(type(x), AsyncGenerator)
class Gen:
def __aiter__(self): return self
@ -1283,7 +1282,7 @@ class TestOneTrickPonyABCs(ABCTestCase):
for x in samples:
self.assertIsInstance(x, AsyncIterator)
self.assertIsInstance(x, AsyncGenerator)
self.assertTrue(issubclass(type(x), AsyncGenerator), repr(type(x)))
self.assertIsSubclass(type(x), AsyncGenerator)
self.validate_abstract_methods(AsyncGenerator, 'asend', 'athrow')
def run_async(coro):
@ -1326,14 +1325,14 @@ class TestOneTrickPonyABCs(ABCTestCase):
]
for x in non_samples:
self.assertNotIsInstance(x, Sized)
self.assertFalse(issubclass(type(x), Sized), repr(type(x)))
self.assertNotIsSubclass(type(x), Sized)
samples = [bytes(), str(),
tuple(), list(), set(), frozenset(), dict(),
dict().keys(), dict().items(), dict().values(),
]
for x in samples:
self.assertIsInstance(x, Sized)
self.assertTrue(issubclass(type(x), Sized), repr(type(x)))
self.assertIsSubclass(type(x), Sized)
self.validate_abstract_methods(Sized, '__len__')
self.validate_isinstance(Sized, '__len__')
@ -1344,14 +1343,14 @@ class TestOneTrickPonyABCs(ABCTestCase):
]
for x in non_samples:
self.assertNotIsInstance(x, Container)
self.assertFalse(issubclass(type(x), Container), repr(type(x)))
self.assertNotIsSubclass(type(x), Container)
samples = [bytes(), str(),
tuple(), list(), set(), frozenset(), dict(),
dict().keys(), dict().items(),
]
for x in samples:
self.assertIsInstance(x, Container)
self.assertTrue(issubclass(type(x), Container), repr(type(x)))
self.assertIsSubclass(type(x), Container)
self.validate_abstract_methods(Container, '__contains__')
self.validate_isinstance(Container, '__contains__')
@ -1363,7 +1362,7 @@ class TestOneTrickPonyABCs(ABCTestCase):
]
for x in non_samples:
self.assertNotIsInstance(x, Callable)
self.assertFalse(issubclass(type(x), Callable), repr(type(x)))
self.assertNotIsSubclass(type(x), Callable)
samples = [lambda: None,
type, int, object,
len,
@ -1371,7 +1370,7 @@ class TestOneTrickPonyABCs(ABCTestCase):
]
for x in samples:
self.assertIsInstance(x, Callable)
self.assertTrue(issubclass(type(x), Callable), repr(type(x)))
self.assertIsSubclass(type(x), Callable)
self.validate_abstract_methods(Callable, '__call__')
self.validate_isinstance(Callable, '__call__')
@ -1379,16 +1378,16 @@ class TestOneTrickPonyABCs(ABCTestCase):
for B in Hashable, Iterable, Iterator, Reversible, Sized, Container, Callable:
class C(B):
pass
self.assertTrue(issubclass(C, B))
self.assertFalse(issubclass(int, C))
self.assertIsSubclass(C, B)
self.assertNotIsSubclass(int, C)
def test_registration(self):
for B in Hashable, Iterable, Iterator, Reversible, Sized, Container, Callable:
class C:
__hash__ = None # Make sure it isn't hashable by default
self.assertFalse(issubclass(C, B), B.__name__)
self.assertNotIsSubclass(C, B)
B.register(C)
self.assertTrue(issubclass(C, B))
self.assertIsSubclass(C, B)
class WithSet(MutableSet):
@ -1419,7 +1418,7 @@ class TestCollectionABCs(ABCTestCase):
def test_Set(self):
for sample in [set, frozenset]:
self.assertIsInstance(sample(), Set)
self.assertTrue(issubclass(sample, Set))
self.assertIsSubclass(sample, Set)
self.validate_abstract_methods(Set, '__contains__', '__iter__', '__len__')
class MySet(Set):
def __contains__(self, x):
@ -1500,9 +1499,9 @@ class TestCollectionABCs(ABCTestCase):
def test_MutableSet(self):
self.assertIsInstance(set(), MutableSet)
self.assertTrue(issubclass(set, MutableSet))
self.assertIsSubclass(set, MutableSet)
self.assertNotIsInstance(frozenset(), MutableSet)
self.assertFalse(issubclass(frozenset, MutableSet))
self.assertNotIsSubclass(frozenset, MutableSet)
self.validate_abstract_methods(MutableSet, '__contains__', '__iter__', '__len__',
'add', 'discard')
@ -1841,7 +1840,7 @@ class TestCollectionABCs(ABCTestCase):
def test_Mapping(self):
for sample in [dict]:
self.assertIsInstance(sample(), Mapping)
self.assertTrue(issubclass(sample, Mapping))
self.assertIsSubclass(sample, Mapping)
self.validate_abstract_methods(Mapping, '__contains__', '__iter__', '__len__',
'__getitem__')
class MyMapping(Mapping):
@ -1857,7 +1856,7 @@ class TestCollectionABCs(ABCTestCase):
def test_MutableMapping(self):
for sample in [dict]:
self.assertIsInstance(sample(), MutableMapping)
self.assertTrue(issubclass(sample, MutableMapping))
self.assertIsSubclass(sample, MutableMapping)
self.validate_abstract_methods(MutableMapping, '__contains__', '__iter__', '__len__',
'__getitem__', '__setitem__', '__delitem__')
@ -1891,12 +1890,12 @@ class TestCollectionABCs(ABCTestCase):
def test_Sequence(self):
for sample in [tuple, list, bytes, str]:
self.assertIsInstance(sample(), Sequence)
self.assertTrue(issubclass(sample, Sequence))
self.assertIsSubclass(sample, Sequence)
self.assertIsInstance(range(10), Sequence)
self.assertTrue(issubclass(range, Sequence))
self.assertIsSubclass(range, Sequence)
self.assertIsInstance(memoryview(b""), Sequence)
self.assertTrue(issubclass(memoryview, Sequence))
self.assertTrue(issubclass(str, Sequence))
self.assertIsSubclass(memoryview, Sequence)
self.assertIsSubclass(str, Sequence)
self.validate_abstract_methods(Sequence, '__contains__', '__iter__', '__len__',
'__getitem__')
@ -1938,21 +1937,21 @@ class TestCollectionABCs(ABCTestCase):
def test_Buffer(self):
for sample in [bytes, bytearray, memoryview]:
self.assertIsInstance(sample(b"x"), Buffer)
self.assertTrue(issubclass(sample, Buffer))
self.assertIsSubclass(sample, Buffer)
for sample in [str, list, tuple]:
self.assertNotIsInstance(sample(), Buffer)
self.assertFalse(issubclass(sample, Buffer))
self.assertNotIsSubclass(sample, Buffer)
self.validate_abstract_methods(Buffer, '__buffer__')
def test_MutableSequence(self):
for sample in [tuple, str, bytes]:
self.assertNotIsInstance(sample(), MutableSequence)
self.assertFalse(issubclass(sample, MutableSequence))
self.assertNotIsSubclass(sample, MutableSequence)
for sample in [list, bytearray, deque]:
self.assertIsInstance(sample(), MutableSequence)
self.assertTrue(issubclass(sample, MutableSequence))
self.assertTrue(issubclass(array.array, MutableSequence))
self.assertFalse(issubclass(str, MutableSequence))
self.assertIsSubclass(sample, MutableSequence)
self.assertIsSubclass(array.array, MutableSequence)
self.assertNotIsSubclass(str, MutableSequence)
self.validate_abstract_methods(MutableSequence, '__contains__', '__iter__',
'__len__', '__getitem__', '__setitem__', '__delitem__', 'insert')
@ -2043,8 +2042,8 @@ class TestCounter(unittest.TestCase):
self.assertEqual(c, Counter(a=3, b=2, c=1))
self.assertIsInstance(c, dict)
self.assertIsInstance(c, Mapping)
self.assertTrue(issubclass(Counter, dict))
self.assertTrue(issubclass(Counter, Mapping))
self.assertIsSubclass(Counter, dict)
self.assertIsSubclass(Counter, Mapping)
self.assertEqual(len(c), 3)
self.assertEqual(sum(c.values()), 6)
self.assertEqual(list(c.values()), [3, 2, 1])