Skip to content

Experimenting: Annotated[Any, pybind11.CppType("cpp_namespace::UserType")] #4888

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

Draft
wants to merge 49 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
7780fbc
Try `Annotated[Any, "cpp_namespace::UserType"]` unconditionally.
rwgk Oct 19, 2023
76b4a34
Add `struct handle_type_name<...>` specializations for `object`, `lis…
rwgk Oct 19, 2023
794d97e
Revert "Add `struct handle_type_name<...>` specializations for `objec…
rwgk Oct 19, 2023
2cafdab
Add `cpp_name_needs_typing_annotated()`
rwgk Oct 19, 2023
f6ae40b
clang-tidy compatibility
rwgk Oct 20, 2023
ea00323
Merge branch 'master' into annotated_any
rwgk Oct 20, 2023
272152e
`Annotated[Any, CppTypePybind11("cpp_namespace::UserType")]` based on…
rwgk Oct 21, 2023
90b3912
Merge branch 'master' into annotated_any
rwgk Oct 21, 2023
bb8709a
Remove `cpp_name_needs_typing_annotated()`. Preparation for bringing …
rwgk Oct 21, 2023
199532e
Reapply "Add `struct handle_type_name<...>` specializations for `obje…
rwgk Oct 21, 2023
7280380
Fix `handle_type_name<anyset>` as suggested by @sizmailov:
rwgk Oct 21, 2023
70a510c
Adjust test_numpy_dtypes test_signature to new behavior.
rwgk Oct 21, 2023
ace70b0
Render `anyset` as `set | frozenset` as suggested by @sizmailov:
rwgk Oct 21, 2023
7c8991a
Use `Union[set, frozenset]` for internal consistency.
rwgk Oct 21, 2023
63a4881
Add missing `handle_type_name<slice>` specialization (discovered only…
rwgk Oct 22, 2023
3c20944
Add missing `handle_type_name<>` specializations for `bytearray`, `me…
rwgk Oct 22, 2023
74817b7
Add missing `handle_type_name<module_>`.
rwgk Oct 22, 2023
6a3a954
Add missing `handle_type_name<dtype>`.
rwgk Oct 22, 2023
169b0e5
Add test returning `py::exception<void>` (fails at runtime).
rwgk Oct 22, 2023
acfd646
Merge branch 'master' into annotated_any
rwgk Oct 23, 2023
9548fa5
Merge branch 'master' into annotated_any
rwgk Nov 11, 2023
0b98433
Remove `CppTypePybind11()` wrapping for now.
rwgk Nov 14, 2023
781304e
Add test_cases_for_stubgen
rwgk Nov 14, 2023
61ee34e
Pull in `Annotated[list[int], FixedSize(2)]` from test_stl
rwgk Nov 14, 2023
1104076
Add `Annotated[Any, "..."]` wrapping in `type_info_description()`
rwgk Nov 14, 2023
e5f210e
Rename `user_type` to `UserType`
rwgk Nov 14, 2023
d1694d9
Use locally defined bindings to avoid dependency on test_stl.
rwgk Nov 15, 2023
1b4fa71
Unmodified copy of https://github.com/python/mypy/blob/c6cb3c6282003d…
rwgk Nov 15, 2023
644d150
Minimal changes to make the basics code build.
rwgk Nov 15, 2023
1fa0065
pre-commit clang-format, NO manual changes.
rwgk Nov 15, 2023
69dac46
Add `m.basics` tests in ntest_cases_for_stubgen.py
rwgk Nov 15, 2023
1a2e8a6
C++11 compatibility.
rwgk Nov 15, 2023
79f6bdc
Replace `.stl_binders.` with `.cases_for_stubgen.`
rwgk Nov 15, 2023
2376f6e
Use py::handle instead of py::object to avoid clang-tidy errors.
rwgk Nov 15, 2023
542438f
Merge branch 'master' into annotated_any
rwgk Nov 16, 2023
bdbb10d
Add some deeply nested test cases.
rwgk Nov 16, 2023
429a1f8
Make test_cases_for_stubgen.py much more compact, and the `pytest -v`…
rwgk Nov 16, 2023
2b2ffeb
Introduce `detail::annotated_any()` helper.
rwgk Nov 16, 2023
65661fe
Change `detail::annotated_any()` to produce `pybind11.CppType(...)`
rwgk Nov 16, 2023
6b771d5
Merge branch 'master' into annotated_any
rwgk Dec 17, 2023
813660c
Change `annotated_any()` to `quote_cpp_type_name()`
rwgk Dec 17, 2023
66ee131
Test changes to adjust to previous commit (Change `annotated_any()` t…
rwgk Dec 17, 2023
d14d91e
Remove `handle_type_name` default implementation, add explicit specia…
rwgk Dec 17, 2023
1e6bea2
Merge branch 'master' into annotated_any
rwgk Mar 27, 2024
bf6077a
style: pre-commit fixes
pre-commit-ci[bot] Mar 27, 2024
b02767d
Merge branch 'master' into annotated_any
rwgk Mar 27, 2024
67c41cc
Merge branch 'master' into annotated_any
rwgk Apr 1, 2024
8c5bb07
Merge branch 'master' into annotated_any
rwgk Apr 3, 2024
d57ed51
Adjustments related to pybind/pybind11#4985
rwgk Apr 3, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions include/pybind11/cast.h
Original file line number Diff line number Diff line change
Expand Up @@ -876,6 +876,38 @@ struct handle_type_name {
static constexpr auto name = const_name<T>();
};
template <>
struct handle_type_name<object> {
static constexpr auto name = const_name("object");
};
template <>
struct handle_type_name<list> {
static constexpr auto name = const_name("list");
};
template <>
struct handle_type_name<dict> {
static constexpr auto name = const_name("dict");
};
template <>
struct handle_type_name<anyset> {
static constexpr auto name = const_name("set");
Copy link
Contributor

Choose a reason for hiding this comment

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

According to https://docs.python.org/3/c-api/set.html anyset is an alias for "set or fronzenset".

The contexts where rendering it as set | frozenset might lead to trouble are pretty exotic, I think it's ok to render as union type:

Suggested change
static constexpr auto name = const_name("set");
static constexpr auto name = const_name("set | frozenset");

Copy link
Collaborator Author

@rwgk rwgk Oct 21, 2023

Choose a reason for hiding this comment

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

Done: ace70b0

I see Union[...] is used in multiple places.

Could it be better to use Union[set, frozenset] for self-consistency?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, let's use Union for consistency

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done: 7c8991a

};
template <>
struct handle_type_name<set> {
static constexpr auto name = const_name("set");
};
template <>
struct handle_type_name<frozenset> {
static constexpr auto name = const_name("frozenset");
};
template <>
struct handle_type_name<str> {
static constexpr auto name = const_name("str");
};
template <>
struct handle_type_name<tuple> {
static constexpr auto name = const_name("tuple");
};
template <>
struct handle_type_name<bool_> {
static constexpr auto name = const_name("bool");
};
Expand Down
2 changes: 1 addition & 1 deletion include/pybind11/pybind11.h
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ class cpp_function : public function {
} else {
std::string tname(t->name());
detail::clean_type_id(tname);
signature += tname;
signature += "Annotated[Any, CppTypePybind11(\"" + tname + "\")]";
}
} else {
signature += c;
Expand Down
4 changes: 2 additions & 2 deletions tests/test_numpy_dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,8 +342,8 @@ def test_complex_array():

def test_signature(doc):
assert (
doc(m.create_rec_nested)
== "create_rec_nested(arg0: int) -> numpy.ndarray[NestedStruct]"
doc(m.create_rec_nested) == "create_rec_nested(arg0: int) "
'-> numpy.ndarray[Annotated[Any, CppTypePybind11("NestedStruct")]]'
)


Expand Down
2 changes: 1 addition & 1 deletion tests/test_pytypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def test_set(capture, doc):
assert m.anyset_contains({"foo"}, "foo")

assert doc(m.get_set) == "get_set() -> set"
assert doc(m.print_anyset) == "print_anyset(arg0: anyset) -> None"
assert doc(m.print_anyset) == "print_anyset(arg0: set) -> None"


def test_frozenset(capture, doc):
Expand Down