-
-
Notifications
You must be signed in to change notification settings - Fork 34.4k
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?
Changes from 3 commits
939d5f0
64f0c5f
d4bc711
3159b21
9d48f87
29dd4b7
e44773c
ad78d99
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7457,7 +7457,40 @@ def _source(self): | |
|
|
||
| def test_multiple_inheritance(self): | ||
| class A: | ||
| pass | ||
| @property | ||
| def x(self): | ||
| return 4 | ||
| @property | ||
| def y(self): | ||
| return 5 | ||
| def __len__(self): | ||
| return 10 | ||
|
|
||
| class X(NamedTuple, A): | ||
| x: int | ||
| self.assertEqual(X.__bases__, (tuple, A)) | ||
| self.assertEqual(X.__orig_bases__, (NamedTuple, A)) | ||
| self.assertEqual(X.__mro__, (X, tuple, A, object)) | ||
|
|
||
| a = X(3) | ||
| self.assertEqual(a.x, 3) | ||
| self.assertEqual(a.y, 5) | ||
| self.assertEqual(len(a), 1) | ||
|
|
||
| class Y(A, NamedTuple): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It will be error. This is tested in the next test. |
||
| x: int | ||
| self.assertEqual(Y.__bases__, (A, tuple)) | ||
| self.assertEqual(Y.__orig_bases__, (A, NamedTuple)) | ||
| self.assertEqual(Y.__mro__, (Y, A, tuple, object)) | ||
|
|
||
| a = Y(3) | ||
| self.assertEqual(a.x, 3) | ||
| self.assertEqual(a.y, 5) | ||
| self.assertEqual(len(a), 10) | ||
|
|
||
| def test_multiple_inheritance_errors(self): | ||
| class A(NamedTuple): | ||
| x: int | ||
| with self.assertRaises(TypeError): | ||
| class X(NamedTuple, A): | ||
| x: int | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2794,10 +2794,6 @@ def _make_nmtuple(name, types, module, defaults = ()): | |
| class NamedTupleMeta(type): | ||
| def __new__(cls, typename, bases, ns): | ||
| assert _NamedTuple in bases | ||
| 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') | ||
|
Comment on lines
-3019
to
-3022
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please consider addressing https://gist.github.com/johnslavik/4d0e22df6cb61868f74269fe7d401f5b. |
||
| bases = tuple(tuple if base is _NamedTuple else base for base in bases) | ||
| types = ns.get('__annotations__', {}) | ||
| default_names = [] | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Add support of multiple inheritance with :class:`typing.NamedTuple`. | ||
AlexWaygood marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
Uh oh!
There was an error while loading. Please reload this page.