-
-
Notifications
You must be signed in to change notification settings - Fork 31.9k
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 our terms of service and privacy 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
to
64f0c5f
Compare
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 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
to
d4bc711
Compare
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.
people really need arbitrary multiple inheritance for whatever reason
Thanks for reopening! This looks like the case now indeed
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 in some code of my own to get around this limitation.
Misc/NEWS.d/next/Library/2022-04-28-18-45-58.gh-issue-116241.hu9kRk.rst
Outdated
Show resolved
Hide resolved
Thank you @AlexWaygood and @gvanrossum. Updated to 3.14 and applied the suggestions. |
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. |
I found >>> from typing import NamedTuple
...
... class Foo(NamedTuple):
... x: int
...
... class Bar(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 >>> class Bar2(Foo, NamedTuple):
... y: int and the result may be surprising >>> import inspect
... inspect.signature(Bar2)
<Signature (y: int)> My suggestion is to reiterate python/typing#427 (please check https://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 test class Z(X, Y, NamedTuple)
? (I don't know if this leads to a MRO incompatibilty though)
https://bugs.python.org/issue43923
NamedTuple
can't inherit from another class #116241