Compare commits

...

5 Commits

Author SHA1 Message Date
Michael Carlstrom
f93e9098fc
Merge 6e5df0f99b into ad9fd39e14 2024-09-17 13:46:28 -04:00
Michael Carlstrom
6e5df0f99b remove empty tuple[()] 2024-06-30 11:01:10 -04:00
Michael Carlstrom
bbd574afa5 add comment 2024-06-29 12:10:31 -04:00
Michael Carlstrom
0645585d2d lint 2024-06-29 11:58:34 -04:00
Michael Carlstrom
4d8b37688c tests passing 2024-06-29 11:57:10 -04:00
8 changed files with 46 additions and 43 deletions

View File

@ -225,16 +225,17 @@ struct EigenProps {
= !show_c_contiguous && show_order && requires_col_major; = !show_c_contiguous && show_order && requires_col_major;
static constexpr auto descriptor static constexpr auto descriptor
= const_name("numpy.ndarray[") + npy_format_descriptor<Scalar>::name + const_name("[") = const_name("numpy.typing.NDArray[") + npy_format_descriptor<Scalar>::name
+ const_name<fixed_rows>(const_name<(size_t) rows>(), const_name("m")) + const_name(", ") + const_name("[") + const_name<fixed_rows>(const_name<(size_t) rows>(), const_name("m"))
+ const_name<fixed_cols>(const_name<(size_t) cols>(), const_name("n")) + const_name("]") + const_name(", ") + const_name<fixed_cols>(const_name<(size_t) cols>(), const_name("n"))
+ + const_name("]") +
// For a reference type (e.g. Ref<MatrixXd>) we have other constraints that might need to // For a reference type (e.g. Ref<MatrixXd>) we have other constraints that might need to
// be satisfied: writeable=True (for a mutable reference), and, depending on the map's // be satisfied: writeable=True (for a mutable reference), and, depending on the map's
// stride options, possibly f_contiguous or c_contiguous. We include them in the // stride options, possibly f_contiguous or c_contiguous. We include them in the
// descriptor output to provide some hint as to why a TypeError is occurring (otherwise // descriptor output to provide some hint as to why a TypeError is occurring (otherwise
// it can be confusing to see that a function accepts a 'numpy.ndarray[float64[3,2]]' and // it can be confusing to see that a function accepts a
// an error message that you *gave* a numpy.ndarray of the right type and dimensions. // 'numpy.typing.NDArray[float64[3,2]]' and an error message that you *gave* a
// numpy.ndarray of the right type and dimensions.
const_name<show_writeable>(", flags.writeable", "") const_name<show_writeable>(", flags.writeable", "")
+ const_name<show_c_contiguous>(", flags.c_contiguous", "") + const_name<show_c_contiguous>(", flags.c_contiguous", "")
+ const_name<show_f_contiguous>(", flags.f_contiguous", "") + const_name("]"); + const_name<show_f_contiguous>(", flags.f_contiguous", "") + const_name("]");

View File

@ -128,7 +128,7 @@ struct get_tensor_descriptor {
+ const_name<static_cast<int>(Type::Layout) == static_cast<int>(Eigen::RowMajor)>( + const_name<static_cast<int>(Type::Layout) == static_cast<int>(Eigen::RowMajor)>(
", flags.c_contiguous", ", flags.f_contiguous"); ", flags.c_contiguous", ", flags.f_contiguous");
static constexpr auto value static constexpr auto value
= const_name("numpy.ndarray[") + npy_format_descriptor<typename Type::Scalar>::name = const_name("numpy.typing.NDArray[") + npy_format_descriptor<typename Type::Scalar>::name
+ const_name("[") + eigen_tensor_helper<remove_cv_t<Type>>::dimensions_descriptor + const_name("[") + eigen_tensor_helper<remove_cv_t<Type>>::dimensions_descriptor
+ const_name("]") + const_name<ShowDetails>(details, const_name("")) + const_name("]"); + const_name("]") + const_name<ShowDetails>(details, const_name("")) + const_name("]");
}; };

View File

@ -2090,7 +2090,7 @@ vectorize_helper<Func, Return, Args...> vectorize_extractor(const Func &f, Retur
template <typename T, int Flags> template <typename T, int Flags>
struct handle_type_name<array_t<T, Flags>> { struct handle_type_name<array_t<T, Flags>> {
static constexpr auto name static constexpr auto name
= const_name("numpy.ndarray[") + npy_format_descriptor<T>::name + const_name("]"); = const_name("numpy.typing.NDArray[") + npy_format_descriptor<T>::name + const_name("]");
}; };
PYBIND11_NAMESPACE_END(detail) PYBIND11_NAMESPACE_END(detail)

View File

@ -94,19 +94,20 @@ def test_mutator_descriptors():
with pytest.raises(TypeError) as excinfo: with pytest.raises(TypeError) as excinfo:
m.fixed_mutator_r(zc) m.fixed_mutator_r(zc)
assert ( assert (
"(arg0: numpy.ndarray[numpy.float32[5, 6]," "(arg0: numpy.typing.NDArray[numpy.float32[5, 6],"
" flags.writeable, flags.c_contiguous]) -> None" in str(excinfo.value) " flags.writeable, flags.c_contiguous]) -> None" in str(excinfo.value)
) )
with pytest.raises(TypeError) as excinfo: with pytest.raises(TypeError) as excinfo:
m.fixed_mutator_c(zr) m.fixed_mutator_c(zr)
assert ( assert (
"(arg0: numpy.ndarray[numpy.float32[5, 6]," "(arg0: numpy.typing.NDArray[numpy.float32[5, 6],"
" flags.writeable, flags.f_contiguous]) -> None" in str(excinfo.value) " flags.writeable, flags.f_contiguous]) -> None" in str(excinfo.value)
) )
with pytest.raises(TypeError) as excinfo: with pytest.raises(TypeError) as excinfo:
m.fixed_mutator_a(np.array([[1, 2], [3, 4]], dtype="float32")) m.fixed_mutator_a(np.array([[1, 2], [3, 4]], dtype="float32"))
assert "(arg0: numpy.ndarray[numpy.float32[5, 6], flags.writeable]) -> None" in str( assert (
excinfo.value "(arg0: numpy.typing.NDArray[numpy.float32[5, 6], flags.writeable]) -> None"
in str(excinfo.value)
) )
zr.flags.writeable = False zr.flags.writeable = False
with pytest.raises(TypeError): with pytest.raises(TypeError):
@ -200,7 +201,7 @@ def test_negative_stride_from_python(msg):
msg(excinfo.value) msg(excinfo.value)
== """ == """
double_threer(): incompatible function arguments. The following argument types are supported: double_threer(): incompatible function arguments. The following argument types are supported:
1. (arg0: numpy.ndarray[numpy.float32[1, 3], flags.writeable]) -> None 1. (arg0: numpy.typing.NDArray[numpy.float32[1, 3], flags.writeable]) -> None
Invoked with: """ Invoked with: """
+ repr(np.array([5.0, 4.0, 3.0], dtype="float32")) + repr(np.array([5.0, 4.0, 3.0], dtype="float32"))
@ -212,7 +213,7 @@ def test_negative_stride_from_python(msg):
msg(excinfo.value) msg(excinfo.value)
== """ == """
double_threec(): incompatible function arguments. The following argument types are supported: double_threec(): incompatible function arguments. The following argument types are supported:
1. (arg0: numpy.ndarray[numpy.float32[3, 1], flags.writeable]) -> None 1. (arg0: numpy.typing.NDArray[numpy.float32[3, 1], flags.writeable]) -> None
Invoked with: """ Invoked with: """
+ repr(np.array([7.0, 4.0, 1.0], dtype="float32")) + repr(np.array([7.0, 4.0, 1.0], dtype="float32"))
@ -697,25 +698,25 @@ def test_dense_signature(doc):
assert ( assert (
doc(m.double_col) doc(m.double_col)
== """ == """
double_col(arg0: numpy.ndarray[numpy.float32[m, 1]]) -> numpy.ndarray[numpy.float32[m, 1]] double_col(arg0: numpy.typing.NDArray[numpy.float32[m, 1]]) -> numpy.typing.NDArray[numpy.float32[m, 1]]
""" """
) )
assert ( assert (
doc(m.double_row) doc(m.double_row)
== """ == """
double_row(arg0: numpy.ndarray[numpy.float32[1, n]]) -> numpy.ndarray[numpy.float32[1, n]] double_row(arg0: numpy.typing.NDArray[numpy.float32[1, n]]) -> numpy.typing.NDArray[numpy.float32[1, n]]
""" """
) )
assert doc(m.double_complex) == ( assert doc(m.double_complex) == (
""" """
double_complex(arg0: numpy.ndarray[numpy.complex64[m, 1]])""" double_complex(arg0: numpy.typing.NDArray[numpy.complex64[m, 1]])"""
""" -> numpy.ndarray[numpy.complex64[m, 1]] """ -> numpy.typing.NDArray[numpy.complex64[m, 1]]
""" """
) )
assert doc(m.double_mat_rm) == ( assert doc(m.double_mat_rm) == (
""" """
double_mat_rm(arg0: numpy.ndarray[numpy.float32[m, n]])""" double_mat_rm(arg0: numpy.typing.NDArray[numpy.float32[m, n]])"""
""" -> numpy.ndarray[numpy.float32[m, n]] """ -> numpy.typing.NDArray[numpy.float32[m, n]]
""" """
) )

View File

@ -268,23 +268,24 @@ def test_round_trip_references_actually_refer(m):
@pytest.mark.parametrize("m", submodules) @pytest.mark.parametrize("m", submodules)
def test_doc_string(m, doc): def test_doc_string(m, doc):
assert ( assert (
doc(m.copy_tensor) == "copy_tensor() -> numpy.ndarray[numpy.float64[?, ?, ?]]" doc(m.copy_tensor)
== "copy_tensor() -> numpy.typing.NDArray[numpy.float64[?, ?, ?]]"
) )
assert ( assert (
doc(m.copy_fixed_tensor) doc(m.copy_fixed_tensor)
== "copy_fixed_tensor() -> numpy.ndarray[numpy.float64[3, 5, 2]]" == "copy_fixed_tensor() -> numpy.typing.NDArray[numpy.float64[3, 5, 2]]"
) )
assert ( assert (
doc(m.reference_const_tensor) doc(m.reference_const_tensor)
== "reference_const_tensor() -> numpy.ndarray[numpy.float64[?, ?, ?]]" == "reference_const_tensor() -> 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) == ( assert doc(m.round_trip_view_tensor) == (
f"round_trip_view_tensor(arg0: numpy.ndarray[numpy.float64[?, ?, ?], flags.writeable, {order_flag}])" f"round_trip_view_tensor(arg0: numpy.typing.NDArray[numpy.float64[?, ?, ?], flags.writeable, {order_flag}])"
f" -> numpy.ndarray[numpy.float64[?, ?, ?], flags.writeable, {order_flag}]" f" -> numpy.typing.NDArray[numpy.float64[?, ?, ?], flags.writeable, {order_flag}]"
) )
assert doc(m.round_trip_const_view_tensor) == ( assert doc(m.round_trip_const_view_tensor) == (
f"round_trip_const_view_tensor(arg0: numpy.ndarray[numpy.float64[?, ?, ?], {order_flag}])" f"round_trip_const_view_tensor(arg0: numpy.typing.NDArray[numpy.float64[?, ?, ?], {order_flag}])"
" -> numpy.ndarray[numpy.float64[?, ?, ?]]" " -> numpy.typing.NDArray[numpy.float64[?, ?, ?]]"
) )

View File

@ -320,13 +320,13 @@ def test_overload_resolution(msg):
msg(excinfo.value) msg(excinfo.value)
== """ == """
overloaded(): incompatible function arguments. The following argument types are supported: overloaded(): incompatible function arguments. The following argument types are supported:
1. (arg0: numpy.ndarray[numpy.float64]) -> str 1. (arg0: numpy.typing.NDArray[numpy.float64]) -> str
2. (arg0: numpy.ndarray[numpy.float32]) -> str 2. (arg0: numpy.typing.NDArray[numpy.float32]) -> str
3. (arg0: numpy.ndarray[numpy.int32]) -> str 3. (arg0: numpy.typing.NDArray[numpy.int32]) -> str
4. (arg0: numpy.ndarray[numpy.uint16]) -> str 4. (arg0: numpy.typing.NDArray[numpy.uint16]) -> str
5. (arg0: numpy.ndarray[numpy.int64]) -> str 5. (arg0: numpy.typing.NDArray[numpy.int64]) -> str
6. (arg0: numpy.ndarray[numpy.complex128]) -> str 6. (arg0: numpy.typing.NDArray[numpy.complex128]) -> str
7. (arg0: numpy.ndarray[numpy.complex64]) -> str 7. (arg0: numpy.typing.NDArray[numpy.complex64]) -> str
Invoked with: 'not an array' Invoked with: 'not an array'
""" """
@ -342,8 +342,8 @@ def test_overload_resolution(msg):
assert m.overloaded3(np.array([1], dtype="intc")) == "int" assert m.overloaded3(np.array([1], dtype="intc")) == "int"
expected_exc = """ expected_exc = """
overloaded3(): incompatible function arguments. The following argument types are supported: overloaded3(): incompatible function arguments. The following argument types are supported:
1. (arg0: numpy.ndarray[numpy.int32]) -> str 1. (arg0: numpy.typing.NDArray[numpy.int32]) -> str
2. (arg0: numpy.ndarray[numpy.float64]) -> str 2. (arg0: numpy.typing.NDArray[numpy.float64]) -> str
Invoked with: """ Invoked with: """
@ -527,7 +527,7 @@ def test_index_using_ellipsis():
], ],
) )
def test_format_descriptors_for_floating_point_types(test_func): def test_format_descriptors_for_floating_point_types(test_func):
assert "numpy.ndarray[numpy.float" in test_func.__doc__ assert "numpy.typing.NDArray[numpy.float" in test_func.__doc__
@pytest.mark.parametrize("forcecast", [False, True]) @pytest.mark.parametrize("forcecast", [False, True])

View File

@ -351,7 +351,7 @@ def test_complex_array():
def test_signature(doc): def test_signature(doc):
assert ( assert (
doc(m.create_rec_nested) doc(m.create_rec_nested)
== "create_rec_nested(arg0: int) -> numpy.ndarray[NestedStruct]" == "create_rec_nested(arg0: int) -> numpy.typing.NDArray[NestedStruct]"
) )

View File

@ -150,7 +150,7 @@ def test_docs(doc):
assert ( assert (
doc(m.vectorized_func) doc(m.vectorized_func)
== """ == """
vectorized_func(arg0: numpy.ndarray[numpy.int32], arg1: numpy.ndarray[numpy.float32], arg2: numpy.ndarray[numpy.float64]) -> object vectorized_func(arg0: numpy.typing.NDArray[numpy.int32], arg1: numpy.typing.NDArray[numpy.float32], arg2: numpy.typing.NDArray[numpy.float64]) -> object
""" """
) )
@ -212,12 +212,12 @@ def test_passthrough_arguments(doc):
+ ", ".join( + ", ".join(
[ [
"arg0: float", "arg0: float",
"arg1: numpy.ndarray[numpy.float64]", "arg1: numpy.typing.NDArray[numpy.float64]",
"arg2: numpy.ndarray[numpy.float64]", "arg2: numpy.typing.NDArray[numpy.float64]",
"arg3: numpy.ndarray[numpy.int32]", "arg3: numpy.typing.NDArray[numpy.int32]",
"arg4: int", "arg4: int",
"arg5: m.numpy_vectorize.NonPODClass", "arg5: m.numpy_vectorize.NonPODClass",
"arg6: numpy.ndarray[numpy.float64]", "arg6: numpy.typing.NDArray[numpy.float64]",
] ]
) )
+ ") -> object" + ") -> object"