Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-116241: Add support of multiple inheritance with typing.NamedTuple#31781

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
serhiy-storchaka wants to merge6 commits intopython:main
base:main
Choose a base branch
Loading
fromserhiy-storchaka:typing-namedtuple-multiple-inheritance

Conversation

serhiy-storchaka
Copy link
Member

@serhiy-storchakaserhiy-storchaka commentedMar 9, 2022
edited by bedevere-appbot
Loading

@serhiy-storchakaserhiy-storchakaforce-pushed thetyping-namedtuple-multiple-inheritance branch from637bdd3 to64f0c5fCompareApril 28, 2022 16:48
@serhiy-storchakaserhiy-storchaka marked this pull request as ready for reviewApril 28, 2022 17:18
Copy link
Member

@JelleZijlstraJelleZijlstra left a comment

Choose a reason for hiding this comment

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

I think this goes too far because it's not realistic to expect type checkers to support arbitrary base classes on NamedTuple. Also, there haven't been any user requests that I can see for multiple inheritance with anything other than Generic. So I'd prefer to merge the other PR that allows multiple inheritance with Generic only.

AlexWaygood reacted with thumbs up emojiCoolCat467 reacted with thumbs down emoji
@AlexWaygood
Copy link
Member

I think this goes too far because it's not realistic to expect type checkers to support arbitrary base classes on NamedTuple. Also, there haven't been any user requests that I can see for multiple inheritance with anything other than Generic. So I'd prefer to merge the other PR that allows multiple inheritance with Generic only.

I also think it makes sense to only allow multiple inheritance withGeneric, for now. It's a smaller change; and, there should be opportunity to rethink it in the future if it turns out that people really need arbitrary multiple inheritance for whatever reason.

@gvanrossum
Copy link
Member

So, let's just close this?

AlexWaygood reacted with thumbs up emoji

@serhiy-storchakaserhiy-storchaka changed the titlebpo-43923: Add support of multiple inheritance with typing.NamedTuplegh-116241: Add support of multiple inheritance with typing.NamedTupleMar 2, 2024
@serhiy-storchakaserhiy-storchakaforce-pushed thetyping-namedtuple-multiple-inheritance branch from5077333 tod4bc711CompareMarch 2, 2024 16:09
Copy link
Contributor

@Jason-Y-ZJason-Y-Z left a comment

Choose a reason for hiding this comment

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

people really need arbitrary multiple inheritance for whatever reason

Thanks for reopening! This looks like the case now indeed

Copy link

@CoolCat467CoolCat467 left a comment

Choose a reason for hiding this comment

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

Looks great! I ended up doing nearly exactly the same thing insome code of my own to get around this limitation.

@Fidget-SpinnerFidget-Spinner removed their request for reviewOctober 9, 2024 14:36
@serhiy-storchaka
Copy link
MemberAuthor

Thank you@AlexWaygood and@gvanrossum. Updated to 3.14 and applied the suggestions.

AlexWaygood and CoolCat467 reacted with thumbs up emoji

Copy link
Member

@AlexWaygoodAlexWaygood left a comment
edited
Loading

Choose a reason for hiding this comment

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

Given that we've had multiple requests for this, I'm okay with landing it. But please get sign-offs from@gvanrossum and@JelleZijlstra before merging.

serhiy-storchaka and CoolCat467 reacted with thumbs up emoji
Copy link
Member

@gvanrossumgvanrossum left a comment

Choose a reason for hiding this comment

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

Let's do it! (I approve of the idea, I haven't carefully reviewed the code.)

CoolCat467 reacted with thumbs up emoji
@JelleZijlstraJelleZijlstra self-requested a reviewOctober 9, 2024 18:41
@JelleZijlstra
Copy link
Member

I'm OK with this given the discussion on the issue; will take another look at the code soon.

CoolCat467 reacted with thumbs up emoji

@hugovk
Copy link
Member

Reminder for this: the 3.14 beta freeze is in two weeks.

@bswck
Copy link
Contributor

bswck commentedMay 8, 2025
edited
Loading

I found

>>>from typingimport NamedTuple......classFoo(NamedTuple):...     x:int......classBar(NamedTuple,Foo):...     y:int

which fails with

Traceback (most recent call last):  File "<python-input-1>", line 6, in <module>    class Bar(NamedTuple, Foo):        y: int  File "/home/bswck/Python/cpython/Lib/typing.py", line 2938, in __new__    nm_tpl.__bases__ = bases    ^^^^^^^^^^^^^^^^TypeError: Cannot create a consistent method resolution order (MRO) for bases tuple, Foo
Side note

(Thanks Jelle!)
Outside this PR, on the main branch, the error is

Traceback (most recent call last):  File "<python-input-1>", line 6, in <module>    class Bar(NamedTuple, Foo):        y: int  File "/home/bswck/Python/cpython/Lib/typing.py", line 2902, in __new__    raise TypeError(        'can only inherit from a NamedTuple type and Generic')TypeError: can only inherit from a NamedTuple type and Generic

(not a very great message as well, becauseFoo could be understood as aNamedTuple type)

If we swap the order of bases, with multiple inheritance the error won't happen

>>>classBar2(Foo,NamedTuple):...     y:int

and the result may be surprising

>>>import inspect... inspect.signature(Bar2)<Signature (y: int)>

My suggestion is to reiteratepython/typing#427 (please checkhttps://github.com/bswck/make-typed-namedtuples-final!) and explicitly prohibit the inheritance of typed namedtuples.

Copy link
Member

@picnixzpicnixz left a comment

Choose a reason for hiding this comment

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

That'd be great to have. I needed this more than once!

@@ -2401,6 +2401,9 @@ types.
disallowed in Python 3.15. To create a NamedTuple class with 0 fields,
use ``class NT(NamedTuple): pass`` or ``NT = NamedTuple("NT", [])``.

.. versionchanged:: 3.14
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
..versionchanged::3.14
..versionchanged::next

Copy link
Contributor

Choose a reason for hiding this comment

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

+1

self.assertEqual(a.y, 5)
self.assertEqual(len(a), 1)

class Y(A, NamedTuple):
Copy link
Member

Choose a reason for hiding this comment

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

Maybe add a test to check the order of the members as well when doing unpacking? And maybe testclass Z(X, Y, NamedTuple)? (I don't know if this leads to a MRO incompatibilty though)

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@picnixzpicnixzpicnixz left review comments

@bswckbswckbswck left review comments

@gvanrossumgvanrossumgvanrossum approved these changes

@Jason-Y-ZJason-Y-ZJason-Y-Z approved these changes

@CoolCat467CoolCat467CoolCat467 approved these changes

@AlexWaygoodAlexWaygoodAlexWaygood approved these changes

@JelleZijlstraJelleZijlstraAwaiting requested review from JelleZijlstraJelleZijlstra is a code owner

Assignees
No one assigned
Labels
awaiting mergetopic-typingtype-featureA feature request or enhancement
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

11 participants
@serhiy-storchaka@AlexWaygood@gvanrossum@JelleZijlstra@hugovk@bswck@picnixz@Jason-Y-Z@CoolCat467@the-knights-who-say-ni@bedevere-bot

[8]ページ先頭

©2009-2025 Movatter.jp