diff --git a/include/pybind11/eigen/matrix.h b/include/pybind11/eigen/matrix.h index d8a594fdee..e4ebdd012e 100644 --- a/include/pybind11/eigen/matrix.h +++ b/include/pybind11/eigen/matrix.h @@ -224,7 +224,7 @@ struct EigenProps { static constexpr bool show_f_contiguous = !show_c_contiguous && show_order && requires_col_major; - static constexpr auto descriptor + static constexpr auto descriptor = const_name("typing.Annotated[") + io_name("numpy.typing.ArrayLike, ", "numpy.typing.NDArray[") + npy_format_descriptor::name + io_name("", "]") + const_name(", \"[") diff --git a/include/pybind11/eigen/tensor.h b/include/pybind11/eigen/tensor.h index b65f29b02e..e38c12e430 100644 --- a/include/pybind11/eigen/tensor.h +++ b/include/pybind11/eigen/tensor.h @@ -126,14 +126,14 @@ struct get_tensor_descriptor { static constexpr auto details = const_name(", \"flags.writeable\"", "") + const_name < static_cast(Type::Layout) - == static_cast(Eigen::RowMajor) - > (", \"flags.c_contiguous\"", ", \"flags.f_contiguous\""); + == static_cast(Eigen::RowMajor) + > (", \"flags.c_contiguous\"", ", \"flags.f_contiguous\""); static constexpr auto value = const_name("typing.Annotated[") - + io_name("numpy.typing.ArrayLike, ", "numpy.typing.NDArray[") - + npy_format_descriptor::name + io_name("", "]") - + const_name(", \"[") + eigen_tensor_helper>::dimensions_descriptor - + const_name("]\"") + const_name(details, const_name("")) + const_name("]"); + + io_name("numpy.typing.ArrayLike, ", "numpy.typing.NDArray[") + + npy_format_descriptor::name + io_name("", "]") + + const_name(", \"[") + eigen_tensor_helper>::dimensions_descriptor + + const_name("]\"") + const_name(details, const_name("")) + const_name("]"); }; // When EIGEN_AVOID_STL_ARRAY is defined, Eigen::DSizes does not have the begin() member diff --git a/include/pybind11/numpy.h b/include/pybind11/numpy.h index 8280f19873..3a370fe5a3 100644 --- a/include/pybind11/numpy.h +++ b/include/pybind11/numpy.h @@ -2182,7 +2182,8 @@ vectorize_helper vectorize_extractor(const Func &f, Retur template struct handle_type_name> { static constexpr auto name - = io_name("typing.Annotated[numpy.typing.ArrayLike, ", "numpy.typing.NDArray[") + npy_format_descriptor::name + const_name("]"); + = io_name("typing.Annotated[numpy.typing.ArrayLike, ", "numpy.typing.NDArray[") + + npy_format_descriptor::name + const_name("]"); }; PYBIND11_NAMESPACE_END(detail) diff --git a/tests/test_eigen_matrix.py b/tests/test_eigen_matrix.py index 47fad5d8c6..6870c29bf6 100644 --- a/tests/test_eigen_matrix.py +++ b/tests/test_eigen_matrix.py @@ -95,19 +95,19 @@ def test_mutator_descriptors(): with pytest.raises(TypeError) as excinfo: m.fixed_mutator_r(zc) assert ( - "(arg0: typing.Annotated[numpy.typing.ArrayLike, numpy.float32, \"[5, 6]\"," - " \"flags.writeable\", \"flags.c_contiguous\"]) -> None" in str(excinfo.value) + '(arg0: typing.Annotated[numpy.typing.ArrayLike, numpy.float32, "[5, 6]",' + ' "flags.writeable", "flags.c_contiguous"]) -> None' in str(excinfo.value) ) with pytest.raises(TypeError) as excinfo: m.fixed_mutator_c(zr) assert ( - "(arg0: typing.Annotated[numpy.typing.ArrayLike, numpy.float32, \"[5, 6]\"," - " \"flags.writeable\", \"flags.f_contiguous\"]) -> None" in str(excinfo.value) + '(arg0: typing.Annotated[numpy.typing.ArrayLike, numpy.float32, "[5, 6]",' + ' "flags.writeable", "flags.f_contiguous"]) -> None' in str(excinfo.value) ) with pytest.raises(TypeError) as excinfo: m.fixed_mutator_a(np.array([[1, 2], [3, 4]], dtype="float32")) assert ( - "(arg0: typing.Annotated[numpy.typing.ArrayLike, numpy.float32, \"[5, 6]\", \"flags.writeable\"]) -> None" + '(arg0: typing.Annotated[numpy.typing.ArrayLike, numpy.float32, "[5, 6]", "flags.writeable"]) -> None' in str(excinfo.value) ) zr.flags.writeable = False @@ -635,16 +635,16 @@ def test_nocopy_wrapper(): with pytest.raises(TypeError) as excinfo: m.get_elem_nocopy(int_matrix_colmajor) assert "get_elem_nocopy(): incompatible function arguments." in str(excinfo.value) - assert ", \"flags.f_contiguous\"" in str(excinfo.value) + assert ', "flags.f_contiguous"' in str(excinfo.value) assert m.get_elem_nocopy(dbl_matrix_colmajor) == 8 with pytest.raises(TypeError) as excinfo: m.get_elem_nocopy(int_matrix_rowmajor) assert "get_elem_nocopy(): incompatible function arguments." in str(excinfo.value) - assert ", \"flags.f_contiguous\"" in str(excinfo.value) + assert ', "flags.f_contiguous"' in str(excinfo.value) with pytest.raises(TypeError) as excinfo: m.get_elem_nocopy(dbl_matrix_rowmajor) assert "get_elem_nocopy(): incompatible function arguments." in str(excinfo.value) - assert ", \"flags.f_contiguous\"" in str(excinfo.value) + assert ', "flags.f_contiguous"' in str(excinfo.value) # For the row-major test, we take a long matrix in row-major, so only the third is allowed: with pytest.raises(TypeError) as excinfo: @@ -652,20 +652,20 @@ def test_nocopy_wrapper(): assert "get_elem_rm_nocopy(): incompatible function arguments." in str( excinfo.value ) - assert ", \"flags.c_contiguous\"" in str(excinfo.value) + assert ', "flags.c_contiguous"' in str(excinfo.value) with pytest.raises(TypeError) as excinfo: m.get_elem_rm_nocopy(dbl_matrix_colmajor) assert "get_elem_rm_nocopy(): incompatible function arguments." in str( excinfo.value ) - assert ", \"flags.c_contiguous\"" in str(excinfo.value) + assert ', "flags.c_contiguous"' in str(excinfo.value) assert m.get_elem_rm_nocopy(int_matrix_rowmajor) == 8 with pytest.raises(TypeError) as excinfo: m.get_elem_rm_nocopy(dbl_matrix_rowmajor) assert "get_elem_rm_nocopy(): incompatible function arguments." in str( excinfo.value ) - assert ", \"flags.c_contiguous\"" in str(excinfo.value) + assert ', "flags.c_contiguous"' in str(excinfo.value) def test_eigen_ref_life_support(): diff --git a/tests/test_eigen_tensor.py b/tests/test_eigen_tensor.py index 527c45d1de..67c2796103 100644 --- a/tests/test_eigen_tensor.py +++ b/tests/test_eigen_tensor.py @@ -272,23 +272,23 @@ def test_round_trip_references_actually_refer(m): def test_doc_string(m, doc): assert ( doc(m.copy_tensor) - == "copy_tensor() -> typing.Annotated[numpy.typing.NDArray[numpy.float64], \"[?, ?, ?]\"]" + == 'copy_tensor() -> typing.Annotated[numpy.typing.NDArray[numpy.float64], "[?, ?, ?]"]' ) assert ( doc(m.copy_fixed_tensor) - == "copy_fixed_tensor() -> typing.Annotated[numpy.typing.NDArray[numpy.float64], \"[3, 5, 2]\"]" + == 'copy_fixed_tensor() -> typing.Annotated[numpy.typing.NDArray[numpy.float64], "[3, 5, 2]"]' ) assert ( doc(m.reference_const_tensor) - == "reference_const_tensor() -> typing.Annotated[numpy.typing.NDArray[numpy.float64], \"[?, ?, ?]\"]" + == 'reference_const_tensor() -> typing.Annotated[numpy.typing.NDArray[numpy.float64], "[?, ?, ?]"]' ) - order_flag = f"\"flags.{m.needed_options.lower()}_contiguous\"" + order_flag = f'"flags.{m.needed_options.lower()}_contiguous"' assert doc(m.round_trip_view_tensor) == ( - f"round_trip_view_tensor(arg0: typing.Annotated[numpy.typing.ArrayLike, numpy.float64, \"[?, ?, ?]\", \"flags.writeable\", {order_flag}])" - f" -> typing.Annotated[numpy.typing.NDArray[numpy.float64], \"[?, ?, ?]\", \"flags.writeable\", {order_flag}]" + f'round_trip_view_tensor(arg0: typing.Annotated[numpy.typing.ArrayLike, numpy.float64, "[?, ?, ?]", "flags.writeable", {order_flag}])' + f' -> typing.Annotated[numpy.typing.NDArray[numpy.float64], "[?, ?, ?]", "flags.writeable", {order_flag}]' ) assert doc(m.round_trip_const_view_tensor) == ( - f"round_trip_const_view_tensor(arg0: typing.Annotated[numpy.typing.ArrayLike, numpy.float64, \"[?, ?, ?]\", {order_flag}])" - " -> typing.Annotated[numpy.typing.NDArray[numpy.float64], \"[?, ?, ?]\"]" + f'round_trip_const_view_tensor(arg0: typing.Annotated[numpy.typing.ArrayLike, numpy.float64, "[?, ?, ?]", {order_flag}])' + ' -> typing.Annotated[numpy.typing.NDArray[numpy.float64], "[?, ?, ?]"]' )