Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
14 changes: 2 additions & 12 deletions include/pybind11/stl_bind.h
Original file line number Diff line number Diff line change
Expand Up @@ -718,18 +718,8 @@ class_<Map, holder_type> bind_map(handle scope, const std::string &name, Args &&
}

Class_ cl(scope, name.c_str(), pybind11::module_local(local), std::forward<Args>(args)...);
static constexpr auto key_type_descr = detail::make_caster<KeyType>::name;
static constexpr auto mapped_type_descr = detail::make_caster<MappedType>::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))) {
Expand Down
4 changes: 4 additions & 0 deletions tests/test_stl_binders.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,10 @@ TEST_SUBMODULE(stl_binders, m) {
py::bind_map<std::map<std::string, double>>(m, "MapStringDouble");
py::bind_map<std::unordered_map<std::string, double>>(m, "UnorderedMapStringDouble");

// test_map_string_float
py::bind_map<std::map<std::string, float>>(m, "MapStringFloat");
py::bind_map<std::unordered_map<std::string, float>>(m, "UnorderedMapStringFloat");

// test_map_string_double_const
py::bind_map<std::map<std::string, double const>>(m, "MapStringDoubleConst");
py::bind_map<std::unordered_map<std::string, double const>>(m,
Expand Down
21 changes: 14 additions & 7 deletions tests/test_stl_binders.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,22 +314,29 @@ def test_map_delitem():
def test_map_view_types():
map_string_double = m.MapStringDouble()
unordered_map_string_double = m.UnorderedMapStringDouble()
map_string_float = m.MapStringFloat()
unordered_map_string_float = m.UnorderedMapStringFloat()
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]"
assert map_string_double.values().__class__.__name__ == "ValuesView[double]"
assert map_string_float.values().__class__.__name__ == "ValuesView[float]"
Copy link
Contributor

Choose a reason for hiding this comment

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

Please keep the tests for keys() and items() to make sure that those are also named as expected


keys_type = type(map_string_double.keys())
assert type(unordered_map_string_double.keys()) is keys_type
assert type(map_string_float.keys()) is keys_type
assert type(unordered_map_string_float.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
values_type_double = type(map_string_double.values())
assert type(unordered_map_string_double.values()) is values_type_double
assert type(map_string_double_const.values()) is values_type_double
assert type(unordered_map_string_double_const.values()) is values_type_double

values_type_float = type(map_string_float.values())
assert values_type_float is not values_type_double
assert type(unordered_map_string_float.values()) is values_type_float

items_type = type(map_string_double.items())
assert type(unordered_map_string_double.items()) is items_type
Expand Down