Introduce recursive_container_traits (#4623)

* Testing

* Similar fix for std::vector

* Fix infinite recursion check:

1) Apply to is_copy_assignable additionally
2) Check infinite recursion for map-like types

* style: pre-commit fixes

* Optional commit that demonstrates the limitations of this PR

* Fix positioning of container bindings

The bindings were previously in a block that was only activated if numpy
was available.

* Suggestions from code review: API side

* Suggestions from code review: Test side

* Suggestions from code review

1) Renaming: is_recursive_container and
   MutuallyRecursiveContainerPair(MV|VM)
2) Avoid ambiguous specializations of is_recursive_container

* Some little fixes

* Reordering of structs

* Add recursive checks for is_move_constructible

* Static testing for pybind11 type traits

* More precise checking of recursive types

Instead of a trait `is_recursive_container`, use a trait
`recursive_container_traits` with dependent type
`recursive_container_traits::type_to_check_recursively`.
So, instead of just checking if a type is recursive and then trying to
somehow deal with it, recursively-defined traits such as
is_move_constructible can now directly ask this trait where the
recursion should proceed.

* Review suggestions

1. Use std::conditional
2. Fix typo

* Remove leftover include from test

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
Franz Pöschel
2023-05-05 07:39:05 +02:00
committed by GitHub
parent b3e88ecf89
commit f701654633
5 changed files with 485 additions and 28 deletions

View File

@@ -335,3 +335,21 @@ def test_map_view_types():
assert type(unordered_map_string_double.items()) is items_type
assert type(map_string_double_const.items()) is items_type
assert type(unordered_map_string_double_const.items()) is items_type
def test_recursive_vector():
recursive_vector = m.RecursiveVector()
recursive_vector.append(m.RecursiveVector())
recursive_vector[0].append(m.RecursiveVector())
recursive_vector[0].append(m.RecursiveVector())
# Can't use len() since test_stl_binders.cpp does not include stl.h,
# so the necessary conversion is missing
assert recursive_vector[0].count(m.RecursiveVector()) == 2
def test_recursive_map():
recursive_map = m.RecursiveMap()
recursive_map[100] = m.RecursiveMap()
recursive_map[100][101] = m.RecursiveMap()
recursive_map[100][102] = m.RecursiveMap()
assert list(recursive_map[100].keys()) == [101, 102]