add compile guard

This commit is contained in:
Michael Carlstrom 2024-12-05 21:37:47 -05:00
parent 008c370ba4
commit b318d0220c
2 changed files with 17 additions and 4 deletions

View File

@ -999,6 +999,7 @@ TEST_SUBMODULE(pytypes, m) {
m.attr("defined_PYBIND11_TEST_PYTYPES_HAS_RANGES") = false;
#endif
#if defined(PYBIND11_CPP17)
m.attr_with_type<py::typing::List<int>>("list_int") = py::list();
m.attr_with_type<py::typing::Set<py::str>>("set_str") = py::set();
@ -1014,4 +1015,8 @@ TEST_SUBMODULE(pytypes, m) {
point.attr_with_type<py::typing::Dict<py::str, int>>("dict_str_int") = py::dict();
m.attr_with_type<py::typing::Final<int>>("CONST_INT") = 3;
m.attr("defined_PYBIND11_CPP17") = true;
#else
m.attr("defined_PYBIND11_CPP17") = false;
#endif
}

View File

@ -1105,19 +1105,24 @@ def test_dict_ranges(tested_dict, expected):
# https://docs.python.org/3/howto/annotations.html#accessing-the-annotations-dict-of-an-object-in-python-3-9-and-older
def get_annotations_helper(o):
print(dir(o))
if isinstance(o, type):
return o.__dict__.get("__annotations__", {})
return getattr(o, "__annotations__", {})
@pytest.mark.skipif(
not m.defined_PYBIND11_CPP17,
reason="C++17 Position Independent Code not available",
)
def test_module_attribute_types() -> None:
module_annotations = get_annotations_helper(m)
assert module_annotations["list_int"] == "list[int]"
assert module_annotations["set_str"] == "set[str]"
@pytest.mark.skipif(
not m.defined_PYBIND11_CPP17,
reason="C++17 Position Independent Code not available",
)
def test_class_attribute_types() -> None:
empty_annotations = get_annotations_helper(m.EmptyAnnotationClass)
annotations = get_annotations_helper(m.Point)
@ -1126,7 +1131,10 @@ def test_class_attribute_types() -> None:
assert annotations["x"] == "float"
assert annotations["dict_str_int"] == "dict[str, int]"
@pytest.mark.skipif(
not m.defined_PYBIND11_CPP17,
reason="C++17 Position Independent Code not available",
)
def test_final_annotation() -> None:
module_annotations = get_annotations_helper(m)
assert module_annotations["CONST_INT"] == "Final[int]"