Skip to content

GH-103699: Add __orig_bases__ to various typing classes #103698

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

Merged
merged 11 commits into from
Apr 23, 2023
3 changes: 3 additions & 0 deletions Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -7125,6 +7125,9 @@ class TD(TypedDict):
a = A(a = 1)
self.assertIs(type(a), dict)
self.assertEqual(a, {'a': 1})

def test_orig_bases(self):
self.assertEqual(ChildTotalMovie.__orig_bases__, (ParentNontotalMovie,))
Copy link
Member

Choose a reason for hiding this comment

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

Could you add some more tests? Suggestions: a TypedDict with no bases, one with multiple bases, a generic TypedDict.

Copy link
Member

@AlexWaygood AlexWaygood Apr 23, 2023

Choose a reason for hiding this comment

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

__orig_bases__ on generic TypedDicts are already tested quite extensively, e.g.

self.assertEqual(A.__orig_bases__, (TypedDict, Generic[T]))

Tests for TypedDicts with no bases and multiple bases sound worth adding, though

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added quite a few :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should I remove the checks on generic TypedDicts then? Or move them all here?

Copy link
Member

Choose a reason for hiding this comment

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

Should I remove the checks on generic TypedDicts then? Or move them all here?

I think it's probably fine as is, actually. A little bit of duplication in tests isn't a massive issue, and it makes sense to both:

1). Test __orig_bases__ in the test method for testing generic TypedDicts
2). Test generic TypedDicts in the test method for testing __orig_bases__



class RequiredTests(BaseTestCase):
Expand Down
3 changes: 3 additions & 0 deletions Lib/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2994,6 +2994,9 @@ def __new__(cls, name, bases, ns, total=True):

tp_dict = type.__new__(_TypedDictMeta, name, (*generic_base, dict), ns)

if not hasattr(tp_dict, '__orig_bases__'):
tp_dict.__orig_bases__ = bases

annotations = {}
own_annotations = ns.get('__annotations__', {})
msg = "TypedDict('Name', {f0: t0, f1: t1, ...}); each t must be a type"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add ``__orig_bases__`` to non-generic TypedDicts