Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork32k
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
base:main
Are you sure you want to change the base?
gh-116241: Add support of multiple inheritance with typing.NamedTuple#31781
Conversation
637bdd3
to64f0c5f
CompareThere 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 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 with |
So, let's just close this? |
5077333
tod4bc711
CompareThere 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.
people really need arbitrary multiple inheritance for whatever reason
Thanks for reopening! This looks like the case now indeed
CoolCat467 left a comment
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.
Looks great! I ended up doing nearly exactly the same thing insome code of my own to get around this limitation.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Misc/NEWS.d/next/Library/2022-04-28-18-45-58.gh-issue-116241.hu9kRk.rst OutdatedShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Thank you@AlexWaygood and@gvanrossum. Updated to 3.14 and applied the suggestions. |
AlexWaygood left a comment• edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
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.
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.
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.
Let's do it! (I approve of the idea, I haven't carefully reviewed the code.)
I'm OK with this given the discussion on the issue; will take another look at the code soon. |
Reminder for this: the 3.14 beta freeze is in two weeks. |
bswck commentedMay 8, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
I found >>>from typingimport NamedTuple......classFoo(NamedTuple):... x:int......classBar(NamedTuple,Foo):... y:int which fails with
Side note(Thanks Jelle!)
(not a very great message as well, because 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. |
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.
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 |
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.
..versionchanged::3.14 | |
..versionchanged::next |
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.
+1
self.assertEqual(a.y, 5) | ||
self.assertEqual(len(a), 1) | ||
class Y(A, NamedTuple): |
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.
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)
Uh oh!
There was an error while loading.Please reload this page.
https://bugs.python.org/issue43923
NamedTuple
can't inherit from another class #116241