Skip to content

Commit 238502e

Browse files
author
Guido van Rossum
committed
In Generic.__new__(), call __init__() if _gorg(cls) != cls. Fixes #192.
1 parent 7b26dac commit 238502e

2 files changed

Lines changed: 10 additions & 1 deletion

File tree

‎src/test_typing.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -727,6 +727,9 @@ def foo(x: T):
727727
assert type(a) is Node
728728
assert type(b) is Node
729729
assert type(c) is Node
730+
assert a.label == x
731+
assert b.label == x
732+
assert c.label == x
730733

731734
foo(42)
732735

‎src/typing.py‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1069,13 +1069,19 @@ def lookup_name(mapping: Mapping[X, Y], key: X, default: Y) -> Y:
10691069

10701070
__slots__ = ()
10711071

1072+
# TODO: GenericMeta should compute next_in_mro and stuff its
1073+
# __new__ in the class dict, if origin is cls. Issue #196.
10721074
def __new__(cls, *args, **kwds):
10731075
next_in_mro = object
10741076
# Look for the last occurrence of Generic or Generic[...].
10751077
for i, c in enumerate(cls.__mro__[:-1]):
10761078
if isinstance(c, GenericMeta) and _gorg(c) is Generic:
10771079
next_in_mro = cls.__mro__[i+1]
1078-
return next_in_mro.__new__(_gorg(cls))
1080+
origin = _gorg(cls)
1081+
obj = next_in_mro.__new__(origin)
1082+
if origin is not cls:
1083+
obj.__init__(*args, **kwds)
1084+
return obj
10791085

10801086

10811087
def cast(typ, val):

0 commit comments

Comments
 (0)