Skip to content

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

Draft
serhiy-storchaka wants to merge 8 commits intopython:mainfrom
serhiy-storchaka:typing-namedtuple-multiple-inheritance
Draft

gh-116241: Add support of multiple inheritance with typing.NamedTuple#31781
serhiy-storchaka wants to merge 8 commits intopython:mainfrom
serhiy-storchaka:typing-namedtuple-multiple-inheritance

Conversation

@serhiy-storchaka
Copy link
Copy Markdown
Member

@serhiy-storchaka serhiy-storchaka commented Mar 9, 2022

@serhiy-storchaka serhiy-storchaka force-pushed the typing-namedtuple-multiple-inheritance branch from 637bdd3 to 64f0c5f Compare April 28, 2022 16:48
@serhiy-storchaka serhiy-storchaka marked this pull request as ready for review April 28, 2022 17:18
Copy link
Copy Markdown
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.

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
Copy Markdown
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
Copy Markdown
Member

So, let's just close this?

@serhiy-storchaka serhiy-storchaka changed the title bpo-43923: Add support of multiple inheritance with typing.NamedTuple gh-116241: Add support of multiple inheritance with typing.NamedTuple Mar 2, 2024
@serhiy-storchaka serhiy-storchaka force-pushed the typing-namedtuple-multiple-inheritance branch from 5077333 to d4bc711 Compare March 2, 2024 16:09
Copy link
Copy Markdown
Contributor

@Jason-Y-Z Jason-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
Copy Markdown
Contributor

@CoolCat467 CoolCat467 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-Spinner Fidget-Spinner removed their request for review October 9, 2024 14:36
@serhiy-storchaka
Copy link
Copy Markdown
Member Author

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

Copy link
Copy Markdown
Member

@AlexWaygood AlexWaygood 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
Copy Markdown
Member

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

@JelleZijlstra JelleZijlstra self-requested a review October 9, 2024 18:41
@JelleZijlstra
Copy link
Copy Markdown
Member

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

@hugovk
Copy link
Copy Markdown
Member

hugovk commented Apr 23, 2025

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

Copy link
Copy Markdown
Member

@picnixz picnixz 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
Copy Markdown
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

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

class Y(A, NamedTuple):
Copy link
Copy Markdown
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)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It will be error. This is tested in the next test.

@github-actions
Copy link
Copy Markdown

This PR is stale because it has been open for 30 days with no activity.

@github-actions github-actions bot added the stale Stale PR or inactive for long period of time. label Apr 10, 2026
@johnslavik johnslavik self-requested a review April 10, 2026 15:05
Comment on lines -3019 to -3022
for base in bases:
if base is not _NamedTuple and base is not Generic:
raise TypeError(
'can only inherit from a NamedTuple type and Generic')
Copy link
Copy Markdown
Member

@johnslavik johnslavik Apr 10, 2026

Choose a reason for hiding this comment

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

@johnslavik johnslavik removed the stale Stale PR or inactive for long period of time. label Apr 13, 2026
@serhiy-storchaka serhiy-storchaka marked this pull request as draft April 13, 2026 17:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

topic-typing type-feature A feature request or enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.