Skip to content

Commit 43d12a6

Browse files
authored
bpo-28556: Minor fixes for typing module (GH-6732)
This also fixes https://bugs.python.org/issue33420
1 parent 0904f76 commit 43d12a6

File tree

3 files changed

+79
-2
lines changed

3 files changed

+79
-2
lines changed

Lib/test/test_typing.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1324,6 +1324,72 @@ class D(C):
13241324
with self.assertRaises(Exception):
13251325
D[T]
13261326

1327+
def test_new_with_args(self):
1328+
1329+
class A(Generic[T]):
1330+
pass
1331+
1332+
class B:
1333+
def __new__(cls, arg):
1334+
# call object
1335+
obj = super().__new__(cls)
1336+
obj.arg = arg
1337+
return obj
1338+
1339+
# mro: C, A, Generic, B, object
1340+
class C(A, B):
1341+
pass
1342+
1343+
c = C('foo')
1344+
self.assertEqual(c.arg, 'foo')
1345+
1346+
def test_new_with_args2(self):
1347+
1348+
class A:
1349+
def __init__(self, arg):
1350+
self.from_a = arg
1351+
# call object
1352+
super().__init__()
1353+
1354+
# mro: C, Generic, A, object
1355+
class C(Generic[T], A):
1356+
def __init__(self, arg):
1357+
self.from_c = arg
1358+
# call Generic
1359+
super().__init__(arg)
1360+
1361+
c = C('foo')
1362+
self.assertEqual(c.from_a, 'foo')
1363+
self.assertEqual(c.from_c, 'foo')
1364+
1365+
def test_new_no_args(self):
1366+
1367+
class A(Generic[T]):
1368+
pass
1369+
1370+
class B:
1371+
def __new__(cls):
1372+
# call object
1373+
obj = super().__new__(cls)
1374+
obj.from_b = 'b'
1375+
return obj
1376+
1377+
# mro: C, A, Generic, B, object
1378+
class C(A, B):
1379+
def __init__(self, arg):
1380+
self.arg = arg
1381+
1382+
def __new__(cls, arg):
1383+
# call A
1384+
obj = super().__new__(cls)
1385+
obj.from_c = 'c'
1386+
return obj
1387+
1388+
c = C('foo')
1389+
self.assertEqual(c.arg, 'foo')
1390+
self.assertEqual(c.from_b, 'b')
1391+
self.assertEqual(c.from_c, 'c')
1392+
13271393

13281394
class ClassVarTests(BaseTestCase):
13291395

@@ -1737,6 +1803,8 @@ def test_get_type_hints_classes(self):
17371803
self.assertEqual(gth(HasForeignBaseClass),
17381804
{'some_xrepr': XRepr, 'other_a': mod_generics_cache.A,
17391805
'some_b': mod_generics_cache.B})
1806+
self.assertEqual(gth(XRepr.__new__),
1807+
{'x': int, 'y': int})
17401808
self.assertEqual(gth(mod_generics_cache.B),
17411809
{'my_inner_a1': mod_generics_cache.B.A,
17421810
'my_inner_a2': mod_generics_cache.B.A,

Lib/typing.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,8 @@ def __reduce__(self):
607607
# * __parameters__ is a tuple of unique free type parameters of a generic
608608
# type, for example, Dict[T, T].__parameters__ == (T,);
609609
# * __origin__ keeps a reference to a type that was subscripted,
610-
# e.g., Union[T, int].__origin__ == Union;
610+
# e.g., Union[T, int].__origin__ == Union, or the non-generic version of
611+
# the type.
611612
# * __args__ is a tuple of all arguments used in subscripting,
612613
# e.g., Dict[T, int].__args__ == (T, int).
613614

@@ -835,7 +836,11 @@ def __new__(cls, *args, **kwds):
835836
if cls is Generic:
836837
raise TypeError("Type Generic cannot be instantiated; "
837838
"it can be used only as a base class")
838-
return super().__new__(cls)
839+
if super().__new__ is object.__new__:
840+
obj = super().__new__(cls)
841+
else:
842+
obj = super().__new__(cls, *args, **kwds)
843+
return obj
839844

840845
@_tp_cache
841846
def __class_getitem__(cls, params):
@@ -1385,6 +1390,7 @@ def __new__(cls, typename, bases, ns):
13851390
"follow default field(s) {default_names}"
13861391
.format(field_name=field_name,
13871392
default_names=', '.join(defaults_dict.keys())))
1393+
nm_tpl.__new__.__annotations__ = collections.OrderedDict(types)
13881394
nm_tpl.__new__.__defaults__ = tuple(defaults)
13891395
nm_tpl._field_defaults = defaults_dict
13901396
# update from user namespace without overriding special namedtuple attributes
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Minor fixes in typing module: add annotations to ``NamedTuple.__new__``,
2+
pass ``*args`` and ``**kwds`` in ``Generic.__new__``. Original PRs by
3+
Paulius Šarka and Chad Dombrova.

0 commit comments

Comments
 (0)