Skip to content

Conversation

Zheaoli
Copy link
Contributor

@Zheaoli Zheaoli commented Oct 8, 2025

… inspect in annotationlib.get_annotations

Signed-off-by: Manjusaka <me@manjusaka.me>
Signed-off-by: Manjusaka <me@manjusaka.me>
Signed-off-by: Manjusaka <me@manjusaka.me>
Signed-off-by: Manjusaka <me@manjusaka.me>
Signed-off-by: Manjusaka <me@manjusaka.me>
Signed-off-by: Manjusaka <me@manjusaka.me>
>>> add_10 = partial(add, 10)
>>> get_annotations(add_10)
{'b': <class 'int'>, 'c': <class 'int'>, 'return': <class 'int'>}

Copy link
Member

Choose a reason for hiding this comment

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

This could refer to the new section below rather that repeating the example, what do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

SGTM

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?

# The first parameter (self/cls) should remain, but bound args should be removed
try:
# Get the function signature
func_sig = inspect.signature(func)
Copy link
Member

Choose a reason for hiding this comment

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

This will fail if the original function's signature has annotations that do not evaluate in the VALUE format.

return new_annotations

except (ValueError, TypeError):
# If we can't process, return the original annotations
Copy link
Member

Choose a reason for hiding this comment

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

Why? If we can't get correct annotations, we ideally shouldn't return wrong ones.

May not return a fresh dictionary.
"""
import functools
Copy link
Member

Choose a reason for hiding this comment

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

This import is in the crucial code path for annotationlib, it doesn't make sense to make it lazy.

fr.evaluate()


class TestFunctoolsPartialMethod(unittest.TestCase):
Copy link
Member

Choose a reason for hiding this comment

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

Please add tests involving functools.Placeholder.

@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.

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