Skip to content

Conversation

@Zheaoli
Copy link
Contributor

@Zheaoli Zheaoli commented Oct 8, 2025

… inspect in annotationlib.get_annotations

Signed-off-by: Manjusaka <[email protected]>
Signed-off-by: Manjusaka <[email protected]>
Signed-off-by: Manjusaka <[email protected]>
Signed-off-by: Manjusaka <[email protected]>
Signed-off-by: Manjusaka <[email protected]>
Signed-off-by: Manjusaka <[email protected]>
Copy link
Member

@JelleZijlstra JelleZijlstra left a comment

Choose a reason for hiding this comment

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

Could this instead be implemented with a custom __annotate__ function on partial objects, so annotationlib itself doesn't need to change?

@bedevere-app
Copy link

bedevere-app bot commented Oct 10, 2025

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@Zheaoli
Copy link
Contributor Author

Zheaoli commented Oct 12, 2025

Could this instead be implemented with a custom __annotate__ function on partial objects, so annotationlib itself doesn't need to change?

Hi @JelleZijlstra , thank you for your time.
From my personal perspective, I would actually prefer to make the modifications in the partial C module, as this would allow us to obtain the most original information. However, I have some concerns:

Currently, the partial C API doesn't have the relevant interfaces reserved. Adding them would be relatively more complex than the Python version.
If we add the information from functools, we may need to synchronously modify the inspect library to avoid circular dependencies (note: inspect has special handling for functools.partial).

In short, if we solve this problem from functools, it would indeed be the optimal choice in terms of effect, but it would increase complexity.
Do you think this PR needs to open a discussion on discuss.python.org?

@JelleZijlstra
Copy link
Member

I'd strongly prefer if this is done in functools, not annotationlib. This sets a good example where objects can support annotation introspection themselves through overriding __annotate__, instead of through special cases in annotationlib itself.

To make the implementation simpler, we can make the C code delegate to Python code, similar to how typevarobject.c invokes the Python typing module in some cases. So partial would gain an __annotate__ descriptor implemented in C, but that descriptor would just invoke some Python code.

@Zheaoli
Copy link
Contributor Author

Zheaoli commented Oct 13, 2025

To make the implementation simpler, we can make the C code delegate to Python code, similar to how typevarobject.c

Nice, this is better than my original thought. I'll make a try

Signed-off-by: Manjusaka <[email protected]>
@Zheaoli Zheaoli requested a review from rhettinger as a code owner October 15, 2025 14:02
using ``getattr()`` and ``dict.get()`` for safety.
* For :class:`functools.partial` and :class:`functools.partialmethod` objects,
only returns annotations for parameters that have not been bound by the
partial application, along with the return annotation if present.
Copy link
Member

Choose a reason for hiding this comment

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

With the new implementation, I’m not sure how much annotationlib docs should mention support for partial. Maybe mention the generic support and link to a new section in functools doc with the info here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes! I have updated the docs

although if *obj* is a wrapped function (using
:func:`functools.update_wrapper`) or a :class:`functools.partial` object,
:func:`functools.update_wrapper`), a :class:`functools.partial` object,
or a :class:`functools.partialmethod` object,
Copy link
Member

Choose a reason for hiding this comment

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

Are these three examples the only special cases handled, or is this paragraph saying that __wrapped__ attributes are followed to unwrap wrappers?

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 have updated the docs, PTAL

Signed-off-by: Manjusaka <[email protected]>
@Zheaoli Zheaoli changed the title gh-91002: Support functools.partial and functools.partialmethod inspect in annotationlib.get_annotations gh-91002: Support __annotate__ for functools.partial and functools.partialmethod Oct 15, 2025
Signed-off-by: Manjusaka <[email protected]>
@Zheaoli
Copy link
Contributor Author

Zheaoli commented Oct 15, 2025

I have made the requested changes; please review again

@bedevere-app
Copy link

bedevere-app bot commented Oct 15, 2025

Thanks for making the requested changes!

@JelleZijlstra: please review the changes made to this pull request.

@bedevere-app bedevere-app bot requested a review from JelleZijlstra October 15, 2025 14:31
Signed-off-by: Manjusaka <[email protected]>
Signed-off-by: Manjusaka <[email protected]>

.. versionadded:: 3.14

.. _functools-objects-annotations:
Copy link
Member

Choose a reason for hiding this comment

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

I feel like this doesn't belong in the annotationlib docs, maybe in the functools ones.

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 have a different idea here. plz correct me if I'm wrong

It seems that we recommend people use annotationlib to process the annotation here. I prefer add a more detail document in here and reference this in functools

Signed-off-by: Manjusaka <[email protected]>
Signed-off-by: Manjusaka <[email protected]>
@JelleZijlstra JelleZijlstra self-requested a review October 20, 2025 21:37
@Zheaoli
Copy link
Contributor Author

Zheaoli commented Oct 22, 2025

@JelleZijlstra I have updated the PR, PTAL

@rhettinger rhettinger removed their request for review October 24, 2025 05:49
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.

3 participants