import contextlib
+import collections
import pickle
import re
import sys
with self.assertRaises(TypeError):
typing.List[int]()
- def test_list_subclass_instantiation(self):
+ def test_list_subclass(self):
class MyList(typing.List[int]):
pass
a = MyList()
self.assertIsInstance(a, MyList)
+ self.assertIsInstance(a, typing.Sequence)
+
+ self.assertIsSubclass(MyList, list)
+ self.assertNotIsSubclass(list, MyList)
def test_no_dict_instantiation(self):
with self.assertRaises(TypeError):
with self.assertRaises(TypeError):
typing.Dict[str, int]()
- def test_dict_subclass_instantiation(self):
+ def test_dict_subclass(self):
class MyDict(typing.Dict[str, int]):
pass
d = MyDict()
self.assertIsInstance(d, MyDict)
+ self.assertIsInstance(d, typing.MutableMapping)
+
+ self.assertIsSubclass(MyDict, dict)
+ self.assertNotIsSubclass(dict, MyDict)
def test_no_defaultdict_instantiation(self):
with self.assertRaises(TypeError):
with self.assertRaises(TypeError):
typing.DefaultDict[str, int]()
- def test_defaultdict_subclass_instantiation(self):
+ def test_defaultdict_subclass(self):
class MyDefDict(typing.DefaultDict[str, int]):
pass
dd = MyDefDict()
self.assertIsInstance(dd, MyDefDict)
+ self.assertIsSubclass(MyDefDict, collections.defaultdict)
+ self.assertNotIsSubclass(collections.defaultdict, MyDefDict)
+
def test_no_set_instantiation(self):
with self.assertRaises(TypeError):
typing.Set()
self.assertEqual(len(MMB[str, str]()), 0)
self.assertEqual(len(MMB[KT, VT]()), 0)
+ self.assertNotIsSubclass(dict, MMA)
+ self.assertNotIsSubclass(dict, MMB)
+
+ self.assertIsSubclass(MMA, typing.Mapping)
+ self.assertIsSubclass(MMB, typing.Mapping)
+ self.assertIsSubclass(MMC, typing.Mapping)
+
class OtherABCTests(BaseTestCase):
class GenericMeta(TypingMeta, abc.ABCMeta):
"""Metaclass for generic types."""
- __extra__ = None
-
def __new__(cls, name, bases, namespace,
tvars=None, args=None, origin=None, extra=None):
self = super().__new__(cls, name, bases, namespace, _root=True)
self.__parameters__ = tvars
self.__args__ = args
self.__origin__ = origin
- if extra is not None:
- self.__extra__ = extra
- # Else __extra__ is inherited, eventually from the
- # (meta-)class default above.
+ self.__extra__ = extra
# Speed hack (https://github.com/python/typing/issues/196).
self.__next_in_mro__ = _next_in_mro(self)
return self
attr != '__next_in_mro__' and
attr != '__parameters__' and
attr != '__origin__' and
+ attr != '__extra__' and
attr != '__module__'):
attrs.add(attr)
ByteString.register(type(memoryview(b'')))
-class List(list, MutableSequence[T]):
+class List(list, MutableSequence[T], extra=list):
def __new__(cls, *args, **kwds):
if _geqv(cls, List):
return list.__new__(cls, *args, **kwds)
-class Set(set, MutableSet[T]):
+class Set(set, MutableSet[T], extra=set):
def __new__(cls, *args, **kwds):
if _geqv(cls, Set):
return super().__subclasscheck__(cls)
-class FrozenSet(frozenset, AbstractSet[T_co], metaclass=_FrozenSetMeta):
+class FrozenSet(frozenset, AbstractSet[T_co], metaclass=_FrozenSetMeta,
+ extra=frozenset):
__slots__ = ()
def __new__(cls, *args, **kwds):
__all__.append('ContextManager')
-class Dict(dict, MutableMapping[KT, VT]):
+class Dict(dict, MutableMapping[KT, VT], extra=dict):
def __new__(cls, *args, **kwds):
if _geqv(cls, Dict):
"use dict() instead")
return dict.__new__(cls, *args, **kwds)
-class DefaultDict(collections.defaultdict, MutableMapping[KT, VT]):
+class DefaultDict(collections.defaultdict, MutableMapping[KT, VT],
+ extra=collections.defaultdict):
def __new__(cls, *args, **kwds):
if _geqv(cls, DefaultDict):