Skip to content

gh-138949: Fix non-generic children of generic TypedDicts with future annotations#150762

Open
sobolevn wants to merge 1 commit into
python:mainfrom
sobolevn:issue-138949
Open

gh-138949: Fix non-generic children of generic TypedDicts with future annotations#150762
sobolevn wants to merge 1 commit into
python:mainfrom
sobolevn:issue-138949

Conversation

@sobolevn
Copy link
Copy Markdown
Member

@sobolevn sobolevn commented Jun 2, 2026

This looks a bit messy, any suggestions on we can make this simplier are welcome.

It is really bad that TypedDict classes do not create correct __mro__ and just copies all annotations :(

Comment thread Lib/typing.py

base_type_params = getattr(typ, "__type_params__", ())
for btp in base_type_params:
if btp.__name__ in seen:
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.

this seems wrong, base classes can have another type param with the same name

@JelleZijlstra
Copy link
Copy Markdown
Member

This likely still returns wrong results and/or throws NameError if a stringified annotation refers to a (non-type param) name that is defined in the base class's module but not the child class's.

I think to do this fully correctly we'd have to add some data at TypedDict creation time: an annotation dict with just the TypedDict's own non-inherited annotations (own_annotations?) and the actual MRO (__typeddict_mro__?). The latter can already be reconstructed right now (as your code shows), but I don't think the former can be. It's not enough to e.g. look at which annotations exist in base classes, because the child class might override a base class annotation for the same name.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants