Improve consistency of array and array_t with regard to other pytypes
* `array_t(const object &)` now throws on error
* `array_t::ensure()` is intended for casters —- old constructor is
deprecated
* `array` and `array_t` get default constructors (empty array)
* `array` gets a converting constructor
* `py::isinstance<array_T<T>>()` checks the type (but not flags)
There is only one special thing which must remain: `array_t` gets
its own `type_caster` specialization which uses `ensure` instead
of a simple check.
diff --git a/tests/test_numpy_array.cpp b/tests/test_numpy_array.cpp
index df6377e..14c4c29 100644
--- a/tests/test_numpy_array.cpp
+++ b/tests/test_numpy_array.cpp
@@ -126,4 +126,28 @@
);
sm.def("function_taking_uint64", [](uint64_t) { });
+
+ sm.def("isinstance_untyped", [](py::object yes, py::object no) {
+ return py::isinstance<py::array>(yes) && !py::isinstance<py::array>(no);
+ });
+
+ sm.def("isinstance_typed", [](py::object o) {
+ return py::isinstance<py::array_t<double>>(o) && !py::isinstance<py::array_t<int>>(o);
+ });
+
+ sm.def("default_constructors", []() {
+ return py::dict(
+ "array"_a=py::array(),
+ "array_t<int32>"_a=py::array_t<std::int32_t>(),
+ "array_t<double>"_a=py::array_t<double>()
+ );
+ });
+
+ sm.def("converting_constructors", [](py::object o) {
+ return py::dict(
+ "array"_a=py::array(o),
+ "array_t<int32>"_a=py::array_t<std::int32_t>(o),
+ "array_t<double>"_a=py::array_t<double>(o)
+ );
+ });
});
diff --git a/tests/test_numpy_array.py b/tests/test_numpy_array.py
index 40682ef..cec0054 100644
--- a/tests/test_numpy_array.py
+++ b/tests/test_numpy_array.py
@@ -245,3 +245,30 @@
from pybind11_tests.array import function_taking_uint64
function_taking_uint64(123)
function_taking_uint64(np.uint64(123))
+
+
+@pytest.requires_numpy
+def test_isinstance():
+ from pybind11_tests.array import isinstance_untyped, isinstance_typed
+
+ assert isinstance_untyped(np.array([1, 2, 3]), "not an array")
+ assert isinstance_typed(np.array([1.0, 2.0, 3.0]))
+
+
+@pytest.requires_numpy
+def test_constructors():
+ from pybind11_tests.array import default_constructors, converting_constructors
+
+ defaults = default_constructors()
+ for a in defaults.values():
+ assert a.size == 0
+ assert defaults["array"].dtype == np.array([]).dtype
+ assert defaults["array_t<int32>"].dtype == np.int32
+ assert defaults["array_t<double>"].dtype == np.float64
+
+ results = converting_constructors([1, 2, 3])
+ for a in results.values():
+ np.testing.assert_array_equal(a, [1, 2, 3])
+ assert results["array"].dtype == np.int_
+ assert results["array_t<int32>"].dtype == np.int32
+ assert results["array_t<double>"].dtype == np.float64