Skip to content
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

Merged
merged 63 commits into from
Dec 22, 2024

Conversation

InvincibleRMC
Copy link
Contributor

@InvincibleRMC InvincibleRMC commented Dec 5, 2024

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 the Final type to mark attributes as Final.

Suggested changelog entry:

add `attr_with_type` for declaring attribute types and `Final`, `ClassVar` type annotations.

@InvincibleRMC InvincibleRMC marked this pull request as draft December 5, 2024 21:18
@InvincibleRMC InvincibleRMC marked this pull request as ready for review December 6, 2024 03:17
@InvincibleRMC
Copy link
Contributor Author

Only works on C++ 17 and above. I honestly was getting many strange errors around make_caster<T>::name.text;. There might be some more proper way of accessing the internals of make_caster that I'm not aware of that could address them. My googling of the errors seemed to suggest it had something to do with the code not being Position Independent.

@InvincibleRMC
Copy link
Contributor Author

@rwgk I have added the handle base implementation.

include/pybind11/cast.h Outdated Show resolved Hide resolved
@rwgk
Copy link
Collaborator

rwgk commented Dec 20, 2024

Finally, there should be some form of documentation

@InvincibleRMC could you please add comments, similar in style to the existing comments for attr?

/** \rst
Return an internal functor to access the object's attributes. Casting the
returned ``detail::obj_attr_accessor`` instance to a `handle` or `object`
subclass causes a corresponding call to ``getattr``. Assigning a `handle`
or `object` subclass causes a call to ``setattr``.
\endrst */
obj_attr_accessor attr(handle key) const;
/// See above (the only difference is that the key's reference is stolen)
obj_attr_accessor attr(object &&key) const;
/// See above (the only difference is that the key is provided as a string literal)
str_attr_accessor attr(const char *key) const;

Other than that this PR looks good to me now.

@InvincibleRMC
Copy link
Contributor Author

@rwgk I added some comments for attr_with_type_hint

@rwgk
Copy link
Collaborator

rwgk commented Dec 21, 2024

I added a couple minor commits (8f87e21, e6b1370).

@timohl could you please take a final quick look? Does it still look good to you?

@InvincibleRMC
Copy link
Contributor Author

Looks great!

Copy link
Contributor

@timohl timohl left a 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.

include/pybind11/detail/common.h Show resolved Hide resolved
@@ -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
Copy link
Contributor

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)))

Copy link
Collaborator

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

rwgk added a commit to rwgk/rwgk_config that referenced this pull request Dec 21, 2024
Copy link
Collaborator

@rwgk rwgk left a 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!

@rwgk rwgk merged commit cf020a1 into pybind:master Dec 22, 2024
76 checks passed
@github-actions github-actions bot added the needs changelog Possibly needs a changelog entry label Dec 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs changelog Possibly needs a changelog entry
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants