-
-
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
base: main
Are you sure you want to change the base?
Conversation
… inspect in annotationlib.get_annotations Signed-off-by: Manjusaka <me@manjusaka.me>
JelleZijlstra
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.
Could this instead be implemented with a custom __annotate__ function on partial objects, so annotationlib itself doesn't need to change?
|
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 |
Hi @JelleZijlstra , thank you for your time. Currently, the partial C API doesn't have the relevant interfaces reserved. Adding them would be relatively more complex than the Python version. 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. |
|
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 To make the implementation simpler, we can make the C code delegate to Python code, similar to how |
Nice, this is better than my original thought. I'll make a try |
Signed-off-by: Manjusaka <me@manjusaka.me>
Signed-off-by: Manjusaka <me@manjusaka.me>
__annotate__ for functools.partial and functools.partialmethod
Signed-off-by: Manjusaka <me@manjusaka.me>
|
I have made the requested changes; please review again |
|
Thanks for making the requested changes! @JelleZijlstra: please review the changes made to this pull request. |
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>
|
I have made the requested changes; please review again |
|
Thanks for making the requested changes! @JelleZijlstra: please review the changes made to this pull request. |
Doc/library/annotationlib.rst
Outdated
| ------------------------------------------- | ||
|
|
||
| Objects can support annotation introspection by implementing the :attr:`~object.__annotate__` | ||
| protocol. When an object provides an :attr:`!__annotate__` attribute, :func:`get_annotations` |
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.
More accurately: when an object’s class provides __annotate__, right?
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.
No, I think for now is right, consider the code following below
import annotationlib
class A():
pass
a=A()
def demo(a):
return {"annotate": 42}
a.__annotate__=demo
print(annotationlib.get_annotation(a))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.
That’s not how special methods work in Python.
There used to be a bug about that with the context manager protocol and __enter__, which was fixed.
There is an explicit exception for __class_getitem__ used to implement type hints like list[str] without needed a metaclass.
But otherwise dunder methods are looked up on the class of an object.
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.
Making sense. fixed
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.
Well I was wrong. __annotate__ is not defined as a special method, but a data descriptor. It’s not looked up on the class.
Signed-off-by: Manjusaka <me@manjusaka.me>
|
@JelleZijlstra PTAL when you got time |
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.
sorry, I had draft comment which were not submitted
|
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 |
Co-authored-by: Éric <merwok@netwok.org>
Co-authored-by: Éric <merwok@netwok.org>
Signed-off-by: Manjusaka <me@manjusaka.me>
Signed-off-by: Manjusaka <me@manjusaka.me>
|
@merwok Thanks for your time, PTAL when you got time |
| 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 |
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 is a strange example; whether or not they are generic has no bearing on __annotate__.
📚 Documentation preview 📚: https://cpython-previews--139753.org.readthedocs.build/