-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
feat(types) Allow setting types for attributes #5460
Conversation
Only works on C++ 17 and above. I honestly was getting many strange errors around |
@rwgk I have added the handle base implementation. |
…he `const char *` overload appears first).
@InvincibleRMC could you please add comments, similar in style to the existing comments for pybind11/include/pybind11/pytypes.h Lines 104 to 114 in 5b503f7
Other than that this PR looks good to me now. |
@rwgk I added some comments for |
Looks great! |
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.
Just two minor comments.
Otherwise, looks good to me.
@@ -1103,6 +1103,79 @@ def test_dict_ranges(tested_dict, expected): | |||
assert m.transform_dict_plus_one(tested_dict) == expected | |||
|
|||
|
|||
# https://docs.python.org/3/howto/annotations.html#accessing-the-annotations-dict-of-an-object-in-python-3-9-and-older |
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.
Python 3.10 adds a new function to the standard library: inspect.get_annotations(). In Python versions 3.10 and newer, calling this function is the best practice for accessing the annotations dict of any object that supports annotations
Is it maybe worth also adding a small test using inspect.get_annotations()
for access?
(only for 3.10+: @pytest.mark.skipif(sys.version_info < (3, 10))
)
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.
@InvincibleRMC could you please help with that?
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.
Done.
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.
Thanks @InvincibleRMC and @timohl!
Description
Until now setting specific type annotation on module or class attributes has been impossible. By using
__annotations__
we can automatically store that information for stub generators to use and be more accurate in their definitions. Also add theFinal
type to mark attributes as Final.Suggested changelog entry: