feat(types) Allow setting types for attributes (#5460)

* init

* add comment

* style: pre-commit fixes

* fix extra ;

* Add annotation helper for older python versions

* style: pre-commit fixes

* test writing __annotations__ to __dict__

* style: pre-commit fixes

* revert bac to __annotations__

* use getattr for automatic init

* use std::move

* update helper

* remove stdmove

* test isinstance

* style: pre-commit fixes

* add #if 3.9

* style: pre-commit fixes

* use getattr

* add dir

* use hasattr

* try setattr

* add c++17 guard

* style: pre-commit fixes

* add compile guard

* style: pre-commit fixes

* cleanup

* comments and function name cleanup

* style: pre-commit fixes

* update test case

* style: pre-commit fixes

* add write

* added instance check

* style: pre-commit fixes

* Test for `__cpp_inline_variables` and use `static_assert()`

* Add guard: __annotations__["list_int"] was set already.

* Avoid explicit `false` in `static_assert()`.

* add redeclaration test

* style: pre-commit fixes

* add handle support to attr_with_type_hint

* store reintpreted_key

* style: pre-commit fixes

* fix str namespace

* fix scope?

* string wrap

* clang tidy

* Swap order of attr_with_type_hint implementation in cast.h (so that the `const char *` overload appears first).

* Reuse `const char *` overload from `handle` overload.

* Added comment

* style: pre-commit fixes

* line up comment

* fixed spelling error

* Add missing period

* Introduce `detail::always_false<>` to make the new `static_assert()` more readable.

* Copy `attr` comment for `const char *` overload, for consistency.

* static_assert(std::false_type::value, ...)

* Revert "static_assert(std::false_type::value, ...)"

This reverts commit 999b668688.

* Add comment for `always_false`

* add test for inspect.get_annotations()

* style: pre-commit fixes

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Ralf W. Grosse-Kunstleve <rgrossekunst@nvidia.com>
This commit is contained in:
Michael Carlstrom
2024-12-21 16:22:08 -08:00
committed by GitHub
parent 5b503f7ec9
commit cf020a1de2
6 changed files with 204 additions and 0 deletions

View File

@@ -1037,6 +1037,38 @@ TEST_SUBMODULE(pytypes, m) {
#else
m.attr("defined_PYBIND11_TEST_PYTYPES_HAS_RANGES") = false;
#endif
#if defined(__cpp_inline_variables)
// Exercises const char* overload:
m.attr_with_type_hint<py::typing::List<int>>("list_int") = py::list();
// Exercises py::handle overload:
m.attr_with_type_hint<py::typing::Set<py::str>>(py::str("set_str")) = py::set();
struct Empty {};
py::class_<Empty>(m, "EmptyAnnotationClass");
struct Static {};
auto static_class = py::class_<Static>(m, "Static");
static_class.def(py::init());
static_class.attr_with_type_hint<py::typing::ClassVar<float>>("x") = 1.0;
static_class.attr_with_type_hint<py::typing::ClassVar<py::typing::Dict<py::str, int>>>(
"dict_str_int")
= py::dict();
struct Instance {};
auto instance = py::class_<Instance>(m, "Instance", py::dynamic_attr());
instance.def(py::init());
instance.attr_with_type_hint<float>("y");
m.def("attr_with_type_hint_float_x",
[](py::handle obj) { obj.attr_with_type_hint<float>("x"); });
m.attr_with_type_hint<py::typing::Final<int>>("CONST_INT") = 3;
m.attr("defined___cpp_inline_variables") = true;
#else
m.attr("defined___cpp_inline_variables") = false;
#endif
m.def("half_of_number", [](const RealNumber &x) { return RealNumber{x.value / 2}; });
// std::vector<T>
m.def("half_of_number_vector", [](const std::vector<RealNumber> &x) {