Skip to content
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

bpo-43923: Allow NamedTuple multiple inheritance #31779

Closed
wants to merge 10 commits into from

Conversation

juliusgeo
Copy link

@juliusgeo juliusgeo commented Mar 9, 2022

Copy link
Member

@JelleZijlstra JelleZijlstra left a comment

Choose a reason for hiding this comment

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

This looks like it's still mostly a revert, which I don't think is the right approach since it ends up losing a bunch of past improvements. Try to make the minimal change necessary to make things work.

Lib/typing.py Outdated
def _make_nmtuple(name, types):
msg = "NamedTuple('Name', [(f0, t0), (f1, t1), ...]); each t must be a type"
types = [(n, _type_check(t, msg)) for n, t in types]
nm_tpl = collections.namedtuple(name, [n for n, t in types])
Copy link
Member

Choose a reason for hiding this comment

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

collections.namedtuple now supports default= directly, so we should use that

Lib/typing.py Outdated
defaults=defaults, module=module)
nm_tpl.__annotations__ = nm_tpl.__new__.__annotations__ = types
def _make_nmtuple(name, types):
msg = "NamedTuple('Name', [(f0, t0), (f1, t1), ...]); each t must be a type"
Copy link
Member

Choose a reason for hiding this comment

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

You're missing the improved error message from the existing code

Lib/typing.py Outdated
default_names=', '.join(defaults_dict.keys())))
nm_tpl.__new__.__annotations__ = dict(types)
nm_tpl.__new__.__defaults__ = tuple(defaults)
nm_tpl._field_defaults = defaults_dict
Copy link
Member

Choose a reason for hiding this comment

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

No need to restore this old attribute

@bedevere-bot
Copy link

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@JelleZijlstra
Copy link
Member

JelleZijlstra commented Mar 10, 2022

Note there's an alternative PR from Serhiy: #31781.

@AlexWaygood
Copy link
Member

Note there's an alternative PR from Serhiy: #31781.

I still like Serhiy's more. In particular, I think it's best to keep NamedTuple a function, rather than a class. Given what inheriting from NamedTuple actually does (dynamically create a new tuple subclass), I think it makes much more sense for it to be a function.

@JelleZijlstra
Copy link
Member

We merged #92027 to allow generic namedtuples. I don't think we want more general multiple inheritance. Thanks for your PR though!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants