diff --git a/include/pybind11/stl_bind.h b/include/pybind11/stl_bind.h index 4d0854f6a8..8beae42004 100644 --- a/include/pybind11/stl_bind.h +++ b/include/pybind11/stl_bind.h @@ -718,18 +718,8 @@ class_ bind_map(handle scope, const std::string &name, Args && } Class_ cl(scope, name.c_str(), pybind11::module_local(local), std::forward(args)...); - static constexpr auto key_type_descr = detail::make_caster::name; - static constexpr auto mapped_type_descr = detail::make_caster::name; - std::string key_type_name(key_type_descr.text), mapped_type_name(mapped_type_descr.text); - - // If key type isn't properly wrapped, fall back to C++ names - if (key_type_name == "%") { - key_type_name = detail::type_info_description(typeid(KeyType)); - } - // Similarly for value type: - if (mapped_type_name == "%") { - mapped_type_name = detail::type_info_description(typeid(MappedType)); - } + std::string key_type_name(detail::type_info_description(typeid(KeyType))); + std::string mapped_type_name(detail::type_info_description(typeid(MappedType))); // Wrap KeysView[KeyType] if it wasn't already wrapped if (!detail::get_type_info(typeid(KeysView))) { diff --git a/tests/test_stl_binders.cpp b/tests/test_stl_binders.cpp index e52a03b6d2..15508b8d17 100644 --- a/tests/test_stl_binders.cpp +++ b/tests/test_stl_binders.cpp @@ -192,6 +192,19 @@ TEST_SUBMODULE(stl_binders, m) { py::bind_map>(m, "UnorderedMapStringDoubleConst"); + // test_map_view_types + py::bind_map>(m, "MapIntDouble"); + py::bind_map>(m, "UnorderedMapIntDouble"); + + py::bind_map>(m, "MapIntDoubleConst"); + py::bind_map>(m, "UnorderedMapIntDoubleConst"); + + py::bind_map>(m, "MapIntFloat"); + py::bind_map>(m, "UnorderedMapIntFloat"); + + py::bind_map>(m, "MapInt64Double"); + py::bind_map>(m, "MapUInt64Double"); + py::class_(m, "ENC").def(py::init()).def_readwrite("value", &E_nc::value); // test_noncopyable_containers diff --git a/tests/test_stl_binders.py b/tests/test_stl_binders.py index c00d45b926..3d9cb241f5 100644 --- a/tests/test_stl_binders.py +++ b/tests/test_stl_binders.py @@ -312,29 +312,67 @@ def test_map_delitem(): def test_map_view_types(): - map_string_double = m.MapStringDouble() - unordered_map_string_double = m.UnorderedMapStringDouble() - map_string_double_const = m.MapStringDoubleConst() - unordered_map_string_double_const = m.UnorderedMapStringDoubleConst() - - assert map_string_double.keys().__class__.__name__ == "KeysView[str]" - assert map_string_double.values().__class__.__name__ == "ValuesView[float]" - assert map_string_double.items().__class__.__name__ == "ItemsView[str, float]" - - keys_type = type(map_string_double.keys()) - assert type(unordered_map_string_double.keys()) is keys_type - assert type(map_string_double_const.keys()) is keys_type - assert type(unordered_map_string_double_const.keys()) is keys_type - - values_type = type(map_string_double.values()) - assert type(unordered_map_string_double.values()) is values_type - assert type(map_string_double_const.values()) is values_type - assert type(unordered_map_string_double_const.values()) is values_type - - items_type = type(map_string_double.items()) - 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 + map_int_double = m.MapIntDouble() + unordered_map_int_double = m.UnorderedMapIntDouble() + map_int_double_const = m.MapIntDoubleConst() + unordered_map_int_double_const = m.UnorderedMapIntDoubleConst() + map_int_float = m.MapIntFloat() + unordered_map_int_float = m.UnorderedMapIntFloat() + map_int64_double = m.MapInt64Double() + map_uint64_double = m.MapUInt64Double() + + assert map_int_double.keys().__class__.__name__ == "KeysView[int]" + assert map_int_double.values().__class__.__name__ == "ValuesView[double]" + assert map_int_double.items().__class__.__name__ == "ItemsView[int, double]" + + assert map_int_float.keys().__class__.__name__ == "KeysView[int]" + assert map_int_float.values().__class__.__name__ == "ValuesView[float]" + assert map_int_float.items().__class__.__name__ == "ItemsView[int, float]" + + assert map_int64_double.keys().__class__.__name__ in [ + "KeysView[long]", + "KeysView[long long]", + "KeysView[__int64]", + ] + assert map_int64_double.values().__class__.__name__ == "ValuesView[double]" + assert map_int64_double.items().__class__.__name__ in [ + "ItemsView[long, double]", + "ItemsView[long long, double]", + "ItemsView[__int64, double]", + ] + + assert map_uint64_double.keys().__class__.__name__ in [ + "KeysView[unsigned long]", + "KeysView[unsigned long long]", + "KeysView[unsigned __int64]", + ] + assert map_uint64_double.values().__class__.__name__ == "ValuesView[double]" + assert map_uint64_double.items().__class__.__name__ in [ + "ItemsView[unsigned long, double]", + "ItemsView[unsigned long long, double]", + "ItemsView[unsigned __int64, double]", + ] + + keys_type = type(map_int_double.keys()) + assert type(unordered_map_int_double.keys()) is keys_type + assert type(map_int_float.keys()) is keys_type + assert type(unordered_map_int_float.keys()) is keys_type + assert type(map_int_double_const.keys()) is keys_type + assert type(unordered_map_int_double_const.keys()) is keys_type + + values_type_double = type(map_int_double.values()) + assert type(unordered_map_int_double.values()) is values_type_double + assert type(map_int_double_const.values()) is values_type_double + assert type(unordered_map_int_double_const.values()) is values_type_double + + values_type_float = type(map_int_float.values()) + assert values_type_float is not values_type_double + assert type(unordered_map_int_float.values()) is values_type_float + + items_type = type(map_int_double.items()) + assert type(unordered_map_int_double.items()) is items_type + assert type(map_int_double_const.items()) is items_type + assert type(unordered_map_int_double_const.items()) is items_type def test_recursive_vector():