-
-
Notifications
You must be signed in to change notification settings - Fork 33.8k
gh-91002: Support __annotate__ for functools.partial and functools.partialmethod
#139753
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
Open
Zheaoli
wants to merge
26
commits into
python:main
Choose a base branch
from
Zheaoli:manjusaka/gh91002
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+586
−4
Open
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
0ca6df4
gh-91002: Support functools.partial and functools.partialmethod inspe…
Zheaoli 8ea4838
Add news
Zheaoli 05bc32a
fix docs
Zheaoli 6cc04af
fix docs
Zheaoli dfd72cf
fix docs
Zheaoli 95a0522
fix doctest
Zheaoli ed9b31b
fix review idea
Zheaoli 2d606cf
fix review idea
Zheaoli 9387fdf
update news and docs
Zheaoli 0e8a698
update news and docs
Zheaoli 9954da2
update news and docs
Zheaoli 295b351
fix docs
Zheaoli 70a0abc
fix test
Zheaoli 2a9333f
Merge branch 'main' of https://github.com/python/cpython into manjusa…
Zheaoli 561f067
Apply suggestion from @merwok
Zheaoli e8381c1
fix review idea
Zheaoli f2a51fc
Merge branch 'manjusaka/gh91002' of github.com:Zheaoli/cpython into m…
Zheaoli b69e8dd
fix review idea
Zheaoli 64931d4
fix review idea
Zheaoli 1e05eaf
fix review idea
Zheaoli c63a6b2
restore empty line
merwok 5ef742d
fix review idea
Zheaoli c82fb7e
Update Doc/library/annotationlib.rst
Zheaoli 7cddaca
Update Doc/library/annotationlib.rst
Zheaoli f6fe897
fix review idea
Zheaoli 09b0bbb
fix review idea
Zheaoli File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -383,6 +383,10 @@ Functions | |
| doesn't have its own annotations dict, returns an empty dict. | ||
| * All accesses to object members and dict values are done | ||
| using ``getattr()`` and ``dict.get()`` for safety. | ||
| * Supports objects that provide their own :attr:`~object.__annotate__` descriptor, | ||
| such as :class:`functools.partial` and :class:`functools.partialmethod`. | ||
| See the :mod:`functools` module documentation for details on how these | ||
| objects support annotations. | ||
|
|
||
| *eval_str* controls whether or not values of type :class:`!str` are | ||
| replaced with the result of calling :func:`eval` on those values: | ||
|
|
@@ -404,10 +408,12 @@ Functions | |
| ``sys.modules[obj.__module__].__dict__`` and *locals* defaults | ||
| to the *obj* class namespace. | ||
| * If *obj* is a callable, *globals* defaults to | ||
| :attr:`obj.__globals__ <function.__globals__>`, | ||
| although if *obj* is a wrapped function (using | ||
| :func:`functools.update_wrapper`) or a :class:`functools.partial` object, | ||
| it is unwrapped until a non-wrapped function is found. | ||
| :attr:`obj.__globals__ <function.__globals__>`. | ||
| If *obj* has a ``__wrapped__`` attribute (such as functions | ||
| decorated with :func:`functools.update_wrapper`), or if it is a | ||
| :class:`functools.partial` object, it is unwrapped by following the | ||
| ``__wrapped__`` attribute or :attr:`~functools.partial.func` attribute | ||
| repeatedly to find the underlying wrapped function's globals. | ||
|
|
||
| Calling :func:`!get_annotations` is best practice for accessing the | ||
| annotations dict of any object. See :ref:`annotations-howto` for | ||
|
|
@@ -436,6 +442,36 @@ Functions | |
|
|
||
| .. versionadded:: 3.14 | ||
|
|
||
| .. _support-annotations-custom-objects: | ||
|
|
||
| Supporting annotations in custom objects | ||
| ------------------------------------------- | ||
|
|
||
| Objects can support annotation introspection by implementing the :attr:`~object.__annotate__` | ||
| protocol. When an object's class provides an :attr:`!__annotate__` descriptor, :func:`get_annotations` | ||
| will call it to retrieve the annotations for that object. The :attr:`!__annotate__` function | ||
| should accept a single argument, a member of the :class:`Format` enum, and return a dictionary | ||
| mapping annotation names to their values in the requested format. | ||
|
|
||
| This mechanism allows objects to dynamically compute their annotations based on their state. | ||
| For example, :class:`functools.partial` and :class:`functools.partialmethod` objects use | ||
| :attr:`!__annotate__` to provide annotations that reflect only the unbound parameters, | ||
| excluding parameters that have been filled by the partial application. See the | ||
| :mod:`functools` module documentation for details on how these specific objects handle | ||
| annotations. | ||
|
|
||
| Other examples of objects that implement :attr:`!__annotate__` include: | ||
|
|
||
| * :class:`typing.TypedDict` classes created through the functional syntax | ||
| * Generic classes and functions with type parameters | ||
|
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. This is a strange example; whether or not they are generic has no bearing on |
||
|
|
||
| When implementing :attr:`!__annotate__` for custom objects, the function should handle | ||
| all three primary formats (:attr:`~Format.VALUE`, :attr:`~Format.FORWARDREF`, and | ||
| :attr:`~Format.STRING`) by either returning appropriate values or raising | ||
| :exc:`NotImplementedError` to fall back to default behavior. Helper functions like | ||
| :func:`annotations_to_string` and :func:`call_annotate_function` can assist with | ||
| implementing format support. | ||
|
|
||
|
|
||
| Recipes | ||
| ------- | ||
|
|
||
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.