-
-
Notifications
You must be signed in to change notification settings - Fork 35k
bpo-28556: Minor fixes for typing module #6732
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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). | ||
|
|
||
|
|
@@ -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__: | ||
| obj = super().__new__(cls) | ||
| else: | ||
| obj = super().__new__(cls, *args, **kwds) | ||
| return obj | ||
|
|
||
| @_tp_cache | ||
| def __class_getitem__(cls, params): | ||
|
|
@@ -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) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why OrderedDict?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
|
||
| 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. |
There was a problem hiding this comment.
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:
And would extract the subexpression
super().__new__into a variable.There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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__andobject.__new__each ignore extra arguments only when the other is overridden (and they themselves are not).