Skip to content

Stop generic subclasses from inheriting __extra__ #223

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 18, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions python2/test_typing.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import absolute_import, unicode_literals

import collections
import pickle
import re
import sys
Expand Down Expand Up @@ -970,13 +971,17 @@ def test_no_list_instantiation(self):
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):
Expand All @@ -986,13 +991,17 @@ def test_no_dict_instantiation(self):
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):
Expand All @@ -1002,14 +1011,17 @@ def test_no_defaultdict_instantiation(self):
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()
Expand Down Expand Up @@ -1090,6 +1102,13 @@ def __len__(self):
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 NamedTupleTests(BaseTestCase):

Expand Down
14 changes: 8 additions & 6 deletions python2/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -911,8 +911,6 @@ def _next_in_mro(cls):
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(GenericMeta, cls).__new__(cls, name, bases, namespace)
Expand Down Expand Up @@ -960,10 +958,7 @@ def __new__(cls, name, bases, namespace,
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__ = namespace.get('__extra__')
# Speed hack (https://github.com/python/typing/issues/196).
self.__next_in_mro__ = _next_in_mro(self)
return self
Expand Down Expand Up @@ -1289,6 +1284,7 @@ def _get_protocol_attrs(self):
attr != '__next_in_mro__' and
attr != '__parameters__' and
attr != '__origin__' and
attr != '__extra__' and
attr != '__module__'):
attrs.add(attr)

Expand Down Expand Up @@ -1414,10 +1410,12 @@ class ByteString(Sequence[int]):
pass


ByteString.register(str)
ByteString.register(bytearray)


class List(list, MutableSequence[T]):
__extra__ = list

def __new__(cls, *args, **kwds):
if _geqv(cls, List):
Expand All @@ -1427,6 +1425,7 @@ def __new__(cls, *args, **kwds):


class Set(set, MutableSet[T]):
__extra__ = set

def __new__(cls, *args, **kwds):
if _geqv(cls, Set):
Expand All @@ -1452,6 +1451,7 @@ def __subclasscheck__(self, cls):
class FrozenSet(frozenset, AbstractSet[T_co]):
__metaclass__ = _FrozenSetMeta
__slots__ = ()
__extra__ = frozenset

def __new__(cls, *args, **kwds):
if _geqv(cls, FrozenSet):
Expand Down Expand Up @@ -1479,6 +1479,7 @@ class ValuesView(MappingView[VT_co]):


class Dict(dict, MutableMapping[KT, VT]):
__extra__ = dict

def __new__(cls, *args, **kwds):
if _geqv(cls, Dict):
Expand All @@ -1488,6 +1489,7 @@ def __new__(cls, *args, **kwds):


class DefaultDict(collections.defaultdict, MutableMapping[KT, VT]):
__extra__ = collections.defaultdict

def __new__(cls, *args, **kwds):
if _geqv(cls, DefaultDict):
Expand Down
25 changes: 22 additions & 3 deletions src/test_typing.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import contextlib
import collections
import pickle
import re
import sys
Expand Down Expand Up @@ -1218,13 +1219,17 @@ def test_no_list_instantiation(self):
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):
Expand All @@ -1234,13 +1239,17 @@ def test_no_dict_instantiation(self):
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):
Expand All @@ -1250,14 +1259,17 @@ def test_no_defaultdict_instantiation(self):
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()
Expand Down Expand Up @@ -1338,6 +1350,13 @@ def __len__(self):
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):

Expand Down
20 changes: 9 additions & 11 deletions src/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -894,8 +894,6 @@ def _next_in_mro(cls):
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)
Expand Down Expand Up @@ -943,10 +941,7 @@ def __new__(cls, name, bases, namespace,
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
Expand Down Expand Up @@ -1307,6 +1302,7 @@ def _get_protocol_attrs(self):
attr != '__next_in_mro__' and
attr != '__parameters__' and
attr != '__origin__' and
attr != '__extra__' and
attr != '__module__'):
attrs.add(attr)

Expand Down Expand Up @@ -1470,7 +1466,7 @@ class ByteString(Sequence[int], extra=collections_abc.ByteString):
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):
Expand All @@ -1479,7 +1475,7 @@ def __new__(cls, *args, **kwds):
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):
Expand All @@ -1502,7 +1498,8 @@ def __subclasscheck__(self, cls):
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):
Expand Down Expand Up @@ -1538,15 +1535,16 @@ class ContextManager(Generic[T_co], extra=contextlib.AbstractContextManager):
__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):
raise TypeError("Type Dict cannot be instantiated; "
"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):
Expand Down