-
-
Notifications
You must be signed in to change notification settings - Fork 33.9k
gh-132261: Store annotations at hidden internal keys in the class dict #132345
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
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
carljm
approved these changes
Apr 11, 2025
Member
carljm
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks pretty good to me! I expected it to be a lot gnarlier. It really just removes the ugly special handling of class annotations, everything it adds looks predictable and reasonably straightforward.
Thanks for your work on this!
Misc/NEWS.d/next/Core_and_Builtins/2025-04-09-21-51-37.gh-issue-132261.gL8thm.rst
Outdated
Show resolved
Hide resolved
…e-132261.gL8thm.rst Co-authored-by: Carl Meyer <carl@oddbird.net>
5 tasks
cjwatson
added a commit
to cjwatson/zope.interface
that referenced
this pull request
Jan 7, 2026
Following python/cpython#132345, type annotations result in the annotate function being stored at `__annotate_func`. This caused test failures in klein along the lines of: ``` zope.interface.exceptions.BrokenMethodImplementation: The object FrozenHTTPHeaders(rawHeaders=()) has failed to implement interface klein._imessage.IHTTPHeaders: The contract of klein._imessage.IHTTPHeaders.__annotate_func__(format) is violated because 'FrozenHTTPHeaders.__annotate__()' doesn't allow enough arguments. ``` Ignore this new internal name.
7 tasks
icemac
pushed a commit
to zopefoundation/zope.interface
that referenced
this pull request
Jan 9, 2026
Following python/cpython#132345, type annotations result in the annotate function being stored at `__annotate_func`. This caused test failures in klein along the lines of: ``` zope.interface.exceptions.BrokenMethodImplementation: The object FrozenHTTPHeaders(rawHeaders=()) has failed to implement interface klein._imessage.IHTTPHeaders: The contract of klein._imessage.IHTTPHeaders.__annotate_func__(format) is violated because 'FrozenHTTPHeaders.__annotate__()' doesn't allow enough arguments. ``` Ignore this new internal name.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This fixes #132261 and provides a more elegant solution for https://peps.python.org/pep-0749/#annotations-and-metaclasses. The trouble is that we store things at the
__annotate__and__annotations__keys in the class dictionary and they interfere with the descriptors on type: the solution is to not store our internal objects at those keys. Instead, we now store the annotate function at__annotate_func__and the annotations dict (once evaluated) at__annotations_cache__. These names remain undocumented implementation details.Downsides:
typing.pyneeded some updates to deal with this.__annotations__. In 3.13, this always worked: if a class has annotations there's an__annotations__key in the class dict. On current main, it sometimes works (only if somebody has accessed the.__annotations__attribute). With this PR, it never works (unless the__annotations__key was explicitly added). Breaking this pattern consistently seems better than breaking it nondeterministically though.__new__. I madeannotationlib.get_annotate_function()handle this.cls.__annotate_func__).get_annotationson instances gets an unexpected error #132261📚 Documentation preview 📚: https://cpython-previews--132345.org.readthedocs.build/