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! |
timohl
left a comment
There was a problem hiding this comment.
Just two minor comments.
Otherwise, looks good to me.
| 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.
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.
@InvincibleRMC could you please help with that?
rwgk
left a comment
There was a problem hiding this comment.
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 theFinaltype to mark attributes as Final.Suggested changelog entry: