Skip to content

Commit 308dbe0

Browse files
authored
Backport recent typing updates (GH-6759)
1 parent 7488c79 commit 308dbe0

File tree

3 files changed

+84
-2
lines changed

3 files changed

+84
-2
lines changed

Lib/test/test_typing.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1310,6 +1310,74 @@ class D(C):
13101310
with self.assertRaises(Exception):
13111311
D[T]
13121312

1313+
def test_new_with_args(self):
1314+
1315+
class A(Generic[T]):
1316+
pass
1317+
1318+
class B:
1319+
def __new__(cls, arg):
1320+
# call object
1321+
obj = super().__new__(cls)
1322+
obj.arg = arg
1323+
return obj
1324+
1325+
# mro: C, A, Generic, B, object
1326+
class C(A, B):
1327+
pass
1328+
1329+
c = C('foo')
1330+
self.assertEqual(c.arg, 'foo')
1331+
1332+
def test_new_with_args2(self):
1333+
1334+
class A:
1335+
def __init__(self, arg):
1336+
self.from_a = arg
1337+
# call object
1338+
super().__init__()
1339+
1340+
# mro: C, Generic, A, object
1341+
class C(Generic[T], A):
1342+
def __init__(self, arg):
1343+
self.from_c = arg
1344+
# call Generic
1345+
super().__init__(arg)
1346+
1347+
c = C('foo')
1348+
self.assertEqual(c.from_a, 'foo')
1349+
self.assertEqual(c.from_c, 'foo')
1350+
1351+
def test_new_no_args(self):
1352+
1353+
class A(Generic[T]):
1354+
pass
1355+
1356+
with self.assertRaises(TypeError):
1357+
A('foo')
1358+
1359+
class B:
1360+
def __new__(cls):
1361+
# call object
1362+
obj = super().__new__(cls)
1363+
obj.from_b = 'b'
1364+
return obj
1365+
1366+
# mro: C, A, Generic, B, object
1367+
class C(A, B):
1368+
def __init__(self, arg):
1369+
self.arg = arg
1370+
1371+
def __new__(cls, arg):
1372+
# call A
1373+
obj = super().__new__(cls)
1374+
obj.from_c = 'c'
1375+
return obj
1376+
1377+
c = C('foo')
1378+
self.assertEqual(c.arg, 'foo')
1379+
self.assertEqual(c.from_b, 'b')
1380+
self.assertEqual(c.from_c, 'c')
13131381

13141382
class ClassVarTests(BaseTestCase):
13151383

@@ -1739,6 +1807,8 @@ def test_get_type_hints_classes(self):
17391807
self.assertEqual(gth(HasForeignBaseClass),
17401808
{'some_xrepr': XRepr, 'other_a': mod_generics_cache.A,
17411809
'some_b': mod_generics_cache.B})
1810+
self.assertEqual(gth(XRepr.__new__),
1811+
{'x': int, 'y': int})
17421812
self.assertEqual(gth(mod_generics_cache.B),
17431813
{'my_inner_a1': mod_generics_cache.B.A,
17441814
'my_inner_a2': mod_generics_cache.B.A,

Lib/typing.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1181,10 +1181,18 @@ def _generic_new(base_cls, cls, *args, **kwds):
11811181
# Assure type is erased on instantiation,
11821182
# but attempt to store it in __orig_class__
11831183
if cls.__origin__ is None:
1184-
return base_cls.__new__(cls)
1184+
if (base_cls.__new__ is object.__new__ and
1185+
cls.__init__ is not object.__init__):
1186+
return base_cls.__new__(cls)
1187+
else:
1188+
return base_cls.__new__(cls, *args, **kwds)
11851189
else:
11861190
origin = cls._gorg
1187-
obj = base_cls.__new__(origin)
1191+
if (base_cls.__new__ is object.__new__ and
1192+
cls.__init__ is not object.__init__):
1193+
obj = base_cls.__new__(origin)
1194+
else:
1195+
obj = base_cls.__new__(origin, *args, **kwds)
11881196
try:
11891197
obj.__orig_class__ = cls
11901198
except AttributeError:
@@ -2146,6 +2154,7 @@ def __new__(cls, typename, bases, ns):
21462154
"follow default field(s) {default_names}"
21472155
.format(field_name=field_name,
21482156
default_names=', '.join(defaults_dict.keys())))
2157+
nm_tpl.__new__.__annotations__ = collections.OrderedDict(types)
21492158
nm_tpl.__new__.__defaults__ = tuple(defaults)
21502159
nm_tpl._field_defaults = defaults_dict
21512160
# 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)