Skip to content

Conversation

@serhiy-storchaka
Copy link
Member

@serhiy-storchakaserhiy-storchaka commented Mar 9, 2022

@serhiy-storchakaserhiy-storchakaforce-pushed the typing-namedtuple-multiple-inheritance branch from 637bdd3 to 64f0c5fCompareApril 28, 2022 16:48
@serhiy-storchakaserhiy-storchaka marked this pull request as ready for review April 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
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 with Generic, 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?

@serhiy-storchakaserhiy-storchaka changed the title bpo-43923: Add support of multiple inheritance with typing.NamedTuplegh-116241: Add support of multiple inheritance with typing.NamedTupleMar 2, 2024
@serhiy-storchakaserhiy-storchakaforce-pushed the typing-namedtuple-multiple-inheritance branch from 5077333 to d4bc711CompareMarch 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 in some code of my own to get around this limitation.

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

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

Copy link
Member

@AlexWaygoodAlexWaygood left a 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.

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.)

@JelleZijlstraJelleZijlstra self-requested a review October 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.

@hugovk
Copy link
Member

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

@johnslavik
Copy link
Member

johnslavik commented May 8, 2025

I found

>>> from typing import 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, because Foo could be understood as a NamedTuple 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 reiterate python/typing#427 (please check https://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!

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
Member

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)

classY(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 test class 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

Labels

awaiting mergetopic-typingtype-featureA feature request or enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

11 participants

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