Skip to content
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
68 changes: 68 additions & 0 deletions Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1324,6 +1324,72 @@ class D(C):
with self.assertRaises(Exception):
D[T]

def test_new_with_args(self):

class A(Generic[T]):
pass

class B:
def __new__(cls, arg):
# call object
obj = super().__new__(cls)
obj.arg = arg
return obj

# mro: C, A, Generic, B, object
class C(A, B):
pass

c = C('foo')
self.assertEqual(c.arg, 'foo')

def test_new_with_args2(self):

class A:
def __init__(self, arg):
self.from_a = arg
# call object
super().__init__()

# mro: C, Generic, A, object
class C(Generic[T], A):
def __init__(self, arg):
self.from_c = arg
# call Generic
super().__init__(arg)

c = C('foo')
self.assertEqual(c.from_a, 'foo')
self.assertEqual(c.from_c, 'foo')

def test_new_no_args(self):

class A(Generic[T]):
pass

class B:
def __new__(cls):
# call object
obj = super().__new__(cls)
obj.from_b = 'b'
return obj

# mro: C, A, Generic, B, object
class C(A, B):
def __init__(self, arg):
self.arg = arg

def __new__(cls, arg):
# call A
obj = super().__new__(cls)
obj.from_c = 'c'
return obj

c = C('foo')
self.assertEqual(c.arg, 'foo')
self.assertEqual(c.from_b, 'b')
self.assertEqual(c.from_c, 'c')


class ClassVarTests(BaseTestCase):

Expand Down Expand Up @@ -1737,6 +1803,8 @@ def test_get_type_hints_classes(self):
self.assertEqual(gth(HasForeignBaseClass),
{'some_xrepr': XRepr, 'other_a': mod_generics_cache.A,
'some_b': mod_generics_cache.B})
self.assertEqual(gth(XRepr.__new__),
{'x': int, 'y': int})
self.assertEqual(gth(mod_generics_cache.B),
{'my_inner_a1': mod_generics_cache.B.A,
'my_inner_a2': mod_generics_cache.B.A,
Expand Down
10 changes: 8 additions & 2 deletions Lib/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,8 @@ def __reduce__(self):
# * __parameters__ is a tuple of unique free type parameters of a generic
# type, for example, Dict[T, T].__parameters__ == (T,);
# * __origin__ keeps a reference to a type that was subscripted,
# e.g., Union[T, int].__origin__ == Union;
# e.g., Union[T, int].__origin__ == Union, or the non-generic version of
# the type.
# * __args__ is a tuple of all arguments used in subscripting,
# e.g., Dict[T, int].__args__ == (T, int).

Expand Down Expand Up @@ -835,7 +836,11 @@ def __new__(cls, *args, **kwds):
if cls is Generic:
raise TypeError("Type Generic cannot be instantiated; "
"it can be used only as a base class")
return super().__new__(cls)
if super().__new__ is object.__new__:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think and cls.__init__ is not object.__init__ is needed here.

The test:

        class A(Generic[T]):
            pass
        with self.assertRaises(TypeError):
            A('foo')

And would extract the subexpression super().__new__ into a variable.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, indeed, in the original PR in typing repo we were concerned that it may fail when it shouldn't, but forgot that it also should fail when it should. @gvanrossum what do you think?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, @serhiy-storchaka is right. This is definitely a wart -- object.__init__ and object.__new__ each ignore extra arguments only when the other is overridden (and they themselves are not).

obj = super().__new__(cls)
else:
obj = super().__new__(cls, *args, **kwds)
return obj

@_tp_cache
def __class_getitem__(cls, params):
Expand Down Expand Up @@ -1385,6 +1390,7 @@ def __new__(cls, typename, bases, ns):
"follow default field(s) {default_names}"
.format(field_name=field_name,
default_names=', '.join(defaults_dict.keys())))
nm_tpl.__new__.__annotations__ = collections.OrderedDict(types)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why OrderedDict? dict is ordered.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To make code closer to the backported version (plus there is another occurrence above). At some point however we might want to clean-up all this, but I propose to make it a separate PR.

nm_tpl.__new__.__defaults__ = tuple(defaults)
nm_tpl._field_defaults = defaults_dict
# update from user namespace without overriding special namedtuple attributes
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Minor fixes in typing module: add annotations to ``NamedTuple.__new__``,
pass ``*args`` and ``**kwds`` in ``Generic.__new__``. Original PRs by
Paulius Šarka and Chad Dombrova.