Skip to content

multi-level hierarchies of TypedDict no longer return superclass elements in __annotations__ as of 3.14.0b1 when future annotations enabled #133701

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

Closed
zzzeek opened this issue May 8, 2025 · 1 comment
Labels
3.14 bugs and security fixes stdlib Python modules in the Lib dir topic-typing type-bug An unexpected behavior, bug, or error

Comments

@zzzeek
Copy link

zzzeek commented May 8, 2025

Bug report

Bug description:

using get_annotations() or accessing __annotations__ directly on a TypedDict subclass previously returned a dictionary of all attributes in the TypedDict hierarchy, now only returns for the sub-most class. only happens when from __future__ import annotations is used; when not used, then it works as previously. there's no workaround here because the typeddict subclass does not include declared superclasses in its __mro__.

from __future__ import annotations

from typing import TypedDict

import annotationlib

class Base(TypedDict,total=False):
    a: int
    b: int

class Sub(Base,total=False):
    c: float
    d: int

# for both, py3.14.0a7 has all four, py3.14.0b1 has only c, d
# py3.14.0a7: {'a': ForwardRef('int', module='__main__'), 'b': ForwardRef('int', module='__main__'), 'c': ForwardRef('float', module='__main__'), 'd': ForwardRef('int', module='__main__')}
# py3.14.0b1: {'c': 'float', 'd': 'int'}

print(Sub.__annotations__)
print(annotationlib.get_annotations(Sub))

# there's no way to traverse the ``__mro__`` of the Sub class to find
# these annotations since Base is not there; prints
# (<class '__main__.Sub'>, <class 'dict'>, <class 'object'>)
print(Sub.__mro__)

CPython versions tested on:

3.14

Operating systems tested on:

No response

Linked PRs

@JelleZijlstra
Copy link
Member

Thanks, fix in #133772.

miss-islington pushed a commit to miss-islington/cpython that referenced this issue May 14, 2025
… under PEP 563 (pythonGH-133772)

(cherry picked from commit 9836503)

Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
JelleZijlstra added a commit that referenced this issue May 14, 2025
…d under PEP 563 (GH-133772) (#134003)

gh-133701: Fix incorrect `__annotations__` on TypedDict defined under PEP 563 (GH-133772)
(cherry picked from commit 9836503)

Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.14 bugs and security fixes stdlib Python modules in the Lib dir topic-typing type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

3 participants