Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1cea70f

Browse files
committed
Fix #27014 -- infinite recursion using typing.py.
1 parent e5ea1ab commit 1cea70f

3 files changed

Lines changed: 33 additions & 14 deletions

File tree

Lib/test/test_typing.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import contextlib
2+
import collections
23
import pickle
34
import re
45
import sys
@@ -1218,13 +1219,17 @@ def test_no_list_instantiation(self):
12181219
with self.assertRaises(TypeError):
12191220
typing.List[int]()
12201221

1221-
def test_list_subclass_instantiation(self):
1222+
def test_list_subclass(self):
12221223

12231224
class MyList(typing.List[int]):
12241225
pass
12251226

12261227
a = MyList()
12271228
self.assertIsInstance(a, MyList)
1229+
self.assertIsInstance(a, typing.Sequence)
1230+
1231+
self.assertIsSubclass(MyList, list)
1232+
self.assertNotIsSubclass(list, MyList)
12281233

12291234
def test_no_dict_instantiation(self):
12301235
with self.assertRaises(TypeError):
@@ -1234,13 +1239,17 @@ def test_no_dict_instantiation(self):
12341239
with self.assertRaises(TypeError):
12351240
typing.Dict[str, int]()
12361241

1237-
def test_dict_subclass_instantiation(self):
1242+
def test_dict_subclass(self):
12381243

12391244
class MyDict(typing.Dict[str, int]):
12401245
pass
12411246

12421247
d = MyDict()
12431248
self.assertIsInstance(d, MyDict)
1249+
self.assertIsInstance(d, typing.MutableMapping)
1250+
1251+
self.assertIsSubclass(MyDict, dict)
1252+
self.assertNotIsSubclass(dict, MyDict)
12441253

12451254
def test_no_defaultdict_instantiation(self):
12461255
with self.assertRaises(TypeError):
@@ -1250,14 +1259,17 @@ def test_no_defaultdict_instantiation(self):
12501259
with self.assertRaises(TypeError):
12511260
typing.DefaultDict[str, int]()
12521261

1253-
def test_defaultdict_subclass_instantiation(self):
1262+
def test_defaultdict_subclass(self):
12541263

12551264
class MyDefDict(typing.DefaultDict[str, int]):
12561265
pass
12571266

12581267
dd = MyDefDict()
12591268
self.assertIsInstance(dd, MyDefDict)
12601269

1270+
self.assertIsSubclass(MyDefDict, collections.defaultdict)
1271+
self.assertNotIsSubclass(collections.defaultdict, MyDefDict)
1272+
12611273
def test_no_set_instantiation(self):
12621274
with self.assertRaises(TypeError):
12631275
typing.Set()
@@ -1338,6 +1350,13 @@ def __len__(self):
13381350
self.assertEqual(len(MMB[str, str]()), 0)
13391351
self.assertEqual(len(MMB[KT, VT]()), 0)
13401352

1353+
self.assertNotIsSubclass(dict, MMA)
1354+
self.assertNotIsSubclass(dict, MMB)
1355+
1356+
self.assertIsSubclass(MMA, typing.Mapping)
1357+
self.assertIsSubclass(MMB, typing.Mapping)
1358+
self.assertIsSubclass(MMC, typing.Mapping)
1359+
13411360

13421361
class OtherABCTests(BaseTestCase):
13431362

Lib/typing.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -894,8 +894,6 @@ def _next_in_mro(cls):
894894
class GenericMeta(TypingMeta, abc.ABCMeta):
895895
"""Metaclass for generic types."""
896896

897-
__extra__ = None
898-
899897
def __new__(cls, name, bases, namespace,
900898
tvars=None, args=None, origin=None, extra=None):
901899
self = super().__new__(cls, name, bases, namespace, _root=True)
@@ -943,10 +941,7 @@ def __new__(cls, name, bases, namespace,
943941
self.__parameters__ = tvars
944942
self.__args__ = args
945943
self.__origin__ = origin
946-
if extra is not None:
947-
self.__extra__ = extra
948-
# Else __extra__ is inherited, eventually from the
949-
# (meta-)class default above.
944+
self.__extra__ = extra
950945
# Speed hack (https://github.com/python/typing/issues/196).
951946
self.__next_in_mro__ = _next_in_mro(self)
952947
return self
@@ -1307,6 +1302,7 @@ def _get_protocol_attrs(self):
13071302
attr != '__next_in_mro__' and
13081303
attr != '__parameters__' and
13091304
attr != '__origin__' and
1305+
attr != '__extra__' and
13101306
attr != '__module__'):
13111307
attrs.add(attr)
13121308

@@ -1470,7 +1466,7 @@ class ByteString(Sequence[int], extra=collections_abc.ByteString):
14701466
ByteString.register(type(memoryview(b'')))
14711467

14721468

1473-
class List(list, MutableSequence[T]):
1469+
class List(list, MutableSequence[T], extra=list):
14741470

14751471
def __new__(cls, *args, **kwds):
14761472
if _geqv(cls, List):
@@ -1479,7 +1475,7 @@ def __new__(cls, *args, **kwds):
14791475
return list.__new__(cls, *args, **kwds)
14801476

14811477

1482-
class Set(set, MutableSet[T]):
1478+
class Set(set, MutableSet[T], extra=set):
14831479

14841480
def __new__(cls, *args, **kwds):
14851481
if _geqv(cls, Set):
@@ -1502,7 +1498,8 @@ def __subclasscheck__(self, cls):
15021498
return super().__subclasscheck__(cls)
15031499

15041500

1505-
class FrozenSet(frozenset, AbstractSet[T_co], metaclass=_FrozenSetMeta):
1501+
class FrozenSet(frozenset, AbstractSet[T_co], metaclass=_FrozenSetMeta,
1502+
extra=frozenset):
15061503
__slots__ = ()
15071504

15081505
def __new__(cls, *args, **kwds):
@@ -1538,15 +1535,16 @@ class ContextManager(Generic[T_co], extra=contextlib.AbstractContextManager):
15381535
__all__.append('ContextManager')
15391536

15401537

1541-
class Dict(dict, MutableMapping[KT, VT]):
1538+
class Dict(dict, MutableMapping[KT, VT], extra=dict):
15421539

15431540
def __new__(cls, *args, **kwds):
15441541
if _geqv(cls, Dict):
15451542
raise TypeError("Type Dict cannot be instantiated; "
15461543
"use dict() instead")
15471544
return dict.__new__(cls, *args, **kwds)
15481545

1549-
class DefaultDict(collections.defaultdict, MutableMapping[KT, VT]):
1546+
class DefaultDict(collections.defaultdict, MutableMapping[KT, VT],
1547+
extra=collections.defaultdict):
15501548

15511549
def __new__(cls, *args, **kwds):
15521550
if _geqv(cls, DefaultDict):

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ Core and Builtins
123123
Library
124124
-------
125125

126+
- Issue #27014: Fix infinite recursion using typing.py. Thanks to Kalle Tuure!
127+
126128
- Issue #14132: Fix urllib.request redirect handling when the target only has
127129
a query string. Original fix by Ján Janech.
128130

0 commit comments

Comments
 (0)