diff --git a/include/pybind11/pytypes.h b/include/pybind11/pytypes.h index 965b12c7e..6606b7c03 100644 --- a/include/pybind11/pytypes.h +++ b/include/pybind11/pytypes.h @@ -2567,9 +2567,9 @@ str_attr_accessor object_api::doc() const { template str_attr_accessor object_api::annotations() const { - str_attr_accessor annotations_dict = attr("__annotations__"); + str_attr_accessor annotations_dict = attr("__annotations__"); // Create dict automatically - if (!isinstance(annotations_dict)){ + if (!isinstance(annotations_dict)) { annotations_dict = dict(); } return annotations_dict; diff --git a/tests/test_pytypes.cpp b/tests/test_pytypes.cpp index 18d327f87..82c11468e 100644 --- a/tests/test_pytypes.cpp +++ b/tests/test_pytypes.cpp @@ -1002,7 +1002,6 @@ TEST_SUBMODULE(pytypes, m) { m.attr_with_type>("list_int") = py::list(); m.attr_with_type>("set_str") = py::set(); - struct Empty {}; py::class_(m, "EmptyAnnotationClass"); @@ -1015,5 +1014,4 @@ TEST_SUBMODULE(pytypes, m) { point.attr_with_type>("dict_str_int") = py::dict(); m.attr_with_type>("CONST_INT") = 3; - } diff --git a/tests/test_pytypes.py b/tests/test_pytypes.py index 741a34570..9f9cadeb2 100644 --- a/tests/test_pytypes.py +++ b/tests/test_pytypes.py @@ -1106,18 +1106,19 @@ def test_dict_ranges(tested_dict, expected): def test_module_attribute_types() -> None: module_annotations = m.__annotations__ - assert module_annotations['list_int'] == 'list[int]' - assert module_annotations['set_str'] == 'set[str]' + assert module_annotations["list_int"] == "list[int]" + assert module_annotations["set_str"] == "set[str]" def test_class_attribute_types() -> None: empty_annotations = m.EmptyAnnotationClass.__annotations__ annotations = m.Point.__annotations__ - + assert empty_annotations == {} - assert annotations['x'] == 'float' - assert annotations['dict_str_int'] == 'dict[str, int]' + assert annotations["x"] == "float" + assert annotations["dict_str_int"] == "dict[str, int]" + def test_final_annotation() -> None: module_annotations = m.__annotations__ - assert module_annotations['CONST_INT'] == 'Final[int]' + assert module_annotations["CONST_INT"] == "Final[int]"