Skip to content

Commit 2e2b576

Browse files
committed
Goodbye DataType (and update the tests)
1 parent fee3283 commit 2e2b576

File tree

4 files changed

+5
-129
lines changed

4 files changed

+5
-129
lines changed

src/array.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ impl<T, D> PyArray<T, D> {
168168
/// pyo3::Python::with_gil(|py| {
169169
/// let array = numpy::PyArray::from_vec(py, vec![1, 2, 3i32]);
170170
/// let dtype = array.dtype();
171-
/// assert_eq!(dtype.get_datatype().unwrap(), numpy::DataType::Int32);
171+
/// assert!(dtype.is_equiv_to(numpy::PyArrayDescr::of::<i32>(py)));
172172
/// });
173173
/// ```
174174
pub fn dtype(&self) -> &crate::PyArrayDescr {

src/dtype.rs

Lines changed: 2 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pub use num_complex::{Complex32, Complex64};
2121
/// .unwrap()
2222
/// .downcast()
2323
/// .unwrap();
24-
/// assert_eq!(dtype.get_datatype().unwrap(), numpy::DataType::Float64);
24+
/// assert!(dtype.is_equiv_to(numpy::PyArrayDescr::of::<f64>(py)));
2525
/// });
2626
/// ```
2727
pub struct PyArrayDescr(PyAny);
@@ -68,11 +68,6 @@ impl PyArrayDescr {
6868
unsafe { PyType::from_type_ptr(self.py(), dtype_type_ptr) }
6969
}
7070

71-
/// Returns the data type as `DataType` enum.
72-
pub fn get_datatype(&self) -> Option<DataType> {
73-
DataType::from_typenum(self.get_typenum())
74-
}
75-
7671
/// Shortcut for creating a descriptor of 'object' type.
7772
pub fn object(py: Python) -> &Self {
7873
Self::from_npy_type(py, NPY_TYPES::NPY_OBJECT)
@@ -103,125 +98,6 @@ impl PyArrayDescr {
10398
}
10499
}
105100

106-
/// Represents NumPy data type.
107-
///
108-
/// This is an incomplete counterpart of
109-
/// [Enumerated Types](https://numpy.org/doc/stable/reference/c-api/dtype.html#enumerated-types)
110-
/// in numpy C-API.
111-
#[derive(Clone, Debug, Eq, PartialEq)]
112-
pub enum DataType {
113-
Bool,
114-
Int8,
115-
Int16,
116-
Int32,
117-
Int64,
118-
Uint8,
119-
Uint16,
120-
Uint32,
121-
Uint64,
122-
Float32,
123-
Float64,
124-
Complex32,
125-
Complex64,
126-
Object,
127-
}
128-
129-
impl DataType {
130-
/// Convert `self` into an
131-
/// [enumerated type](https://numpy.org/doc/stable/reference/c-api/dtype.html#enumerated-types).
132-
pub fn into_typenum(self) -> c_int {
133-
self.into_npy_type() as _
134-
}
135-
136-
/// Construct the data type from an
137-
/// [enumerated type](https://numpy.org/doc/stable/reference/c-api/dtype.html#enumerated-types).
138-
pub fn from_typenum(typenum: c_int) -> Option<Self> {
139-
Some(match typenum {
140-
x if x == NPY_TYPES::NPY_BOOL as c_int => DataType::Bool,
141-
x if x == NPY_TYPES::NPY_BYTE as c_int => DataType::Int8,
142-
x if x == NPY_TYPES::NPY_SHORT as c_int => DataType::Int16,
143-
x if x == NPY_TYPES::NPY_INT as c_int => Self::integer::<c_int>()?,
144-
x if x == NPY_TYPES::NPY_LONG as c_int => Self::integer::<c_long>()?,
145-
x if x == NPY_TYPES::NPY_LONGLONG as c_int => Self::integer::<c_longlong>()?,
146-
x if x == NPY_TYPES::NPY_UBYTE as c_int => DataType::Uint8,
147-
x if x == NPY_TYPES::NPY_USHORT as c_int => DataType::Uint16,
148-
x if x == NPY_TYPES::NPY_UINT as c_int => Self::integer::<c_uint>()?,
149-
x if x == NPY_TYPES::NPY_ULONG as c_int => Self::integer::<c_ulong>()?,
150-
x if x == NPY_TYPES::NPY_ULONGLONG as c_int => Self::integer::<c_ulonglong>()?,
151-
x if x == NPY_TYPES::NPY_FLOAT as c_int => DataType::Float32,
152-
x if x == NPY_TYPES::NPY_DOUBLE as c_int => DataType::Float64,
153-
x if x == NPY_TYPES::NPY_CFLOAT as c_int => DataType::Complex32,
154-
x if x == NPY_TYPES::NPY_CDOUBLE as c_int => DataType::Complex64,
155-
x if x == NPY_TYPES::NPY_OBJECT as c_int => DataType::Object,
156-
_ => return None,
157-
})
158-
}
159-
160-
#[inline]
161-
fn integer<T: Bounded + Zero + Sized + PartialEq>() -> Option<Self> {
162-
let is_unsigned = T::min_value() == T::zero();
163-
let bit_width = size_of::<T>() << 3;
164-
Some(match (is_unsigned, bit_width) {
165-
(false, 8) => Self::Int8,
166-
(false, 16) => Self::Int16,
167-
(false, 32) => Self::Int32,
168-
(false, 64) => Self::Int64,
169-
(true, 8) => Self::Uint8,
170-
(true, 16) => Self::Uint16,
171-
(true, 32) => Self::Uint32,
172-
(true, 64) => Self::Uint64,
173-
_ => return None,
174-
})
175-
}
176-
177-
fn into_npy_type(self) -> NPY_TYPES {
178-
fn npy_int_type_lookup<T, T0, T1, T2>(npy_types: [NPY_TYPES; 3]) -> NPY_TYPES {
179-
// `npy_common.h` defines the integer aliases. In order, it checks:
180-
// NPY_BITSOF_LONG, NPY_BITSOF_LONGLONG, NPY_BITSOF_INT, NPY_BITSOF_SHORT, NPY_BITSOF_CHAR
181-
// and assigns the alias to the first matching size, so we should check in this order.
182-
match size_of::<T>() {
183-
x if x == size_of::<T0>() => npy_types[0],
184-
x if x == size_of::<T1>() => npy_types[1],
185-
x if x == size_of::<T2>() => npy_types[2],
186-
_ => panic!("Unable to match integer type descriptor: {:?}", npy_types),
187-
}
188-
}
189-
190-
match self {
191-
DataType::Bool => NPY_TYPES::NPY_BOOL,
192-
DataType::Int8 => NPY_TYPES::NPY_BYTE,
193-
DataType::Int16 => NPY_TYPES::NPY_SHORT,
194-
DataType::Int32 => npy_int_type_lookup::<i32, c_long, c_int, c_short>([
195-
NPY_TYPES::NPY_LONG,
196-
NPY_TYPES::NPY_INT,
197-
NPY_TYPES::NPY_SHORT,
198-
]),
199-
DataType::Int64 => npy_int_type_lookup::<i64, c_long, c_longlong, c_int>([
200-
NPY_TYPES::NPY_LONG,
201-
NPY_TYPES::NPY_LONGLONG,
202-
NPY_TYPES::NPY_INT,
203-
]),
204-
DataType::Uint8 => NPY_TYPES::NPY_UBYTE,
205-
DataType::Uint16 => NPY_TYPES::NPY_USHORT,
206-
DataType::Uint32 => npy_int_type_lookup::<u32, c_ulong, c_uint, c_ushort>([
207-
NPY_TYPES::NPY_ULONG,
208-
NPY_TYPES::NPY_UINT,
209-
NPY_TYPES::NPY_USHORT,
210-
]),
211-
DataType::Uint64 => npy_int_type_lookup::<u64, c_ulong, c_ulonglong, c_uint>([
212-
NPY_TYPES::NPY_ULONG,
213-
NPY_TYPES::NPY_ULONGLONG,
214-
NPY_TYPES::NPY_UINT,
215-
]),
216-
DataType::Float32 => NPY_TYPES::NPY_FLOAT,
217-
DataType::Float64 => NPY_TYPES::NPY_DOUBLE,
218-
DataType::Complex32 => NPY_TYPES::NPY_CFLOAT,
219-
DataType::Complex64 => NPY_TYPES::NPY_CDOUBLE,
220-
DataType::Object => NPY_TYPES::NPY_OBJECT,
221-
}
222-
}
223-
}
224-
225101
/// Represents that a type can be an element of `PyArray`.
226102
///
227103
/// Currently, only integer/float/complex types are supported.
@@ -243,7 +119,7 @@ impl DataType {
243119
/// on Python's heap using PyO3's [Py](pyo3::Py) type.
244120
///
245121
/// ```
246-
/// use numpy::{ndarray::Array2, DataType, Element, PyArray, PyArrayDescr, ToPyArray};
122+
/// use numpy::{ndarray::Array2, Element, PyArray, PyArrayDescr, ToPyArray};
247123
/// use pyo3::{pyclass, Py, Python};
248124
///
249125
/// #[pyclass]

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ pub use crate::array::{
4646
PyArray6, PyArrayDyn,
4747
};
4848
pub use crate::convert::{IntoPyArray, NpyIndex, ToNpyDims, ToPyArray};
49-
pub use crate::dtype::{Complex32, Complex64, DataType, Element, PyArrayDescr};
49+
pub use crate::dtype::{Complex32, Complex64, Element, PyArrayDescr};
5050
pub use crate::error::{DimensionalityError, FromVecError, NotContiguousError, TypeError};
5151
pub use crate::npyffi::{PY_ARRAY_API, PY_UFUNC_API};
5252
pub use crate::npyiter::{

tests/array.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ fn dtype_from_py() {
249249
.downcast()
250250
.unwrap();
251251
assert_eq!(&format!("{:?}", dtype), "dtype('uint32')");
252-
assert_eq!(dtype.get_datatype().unwrap(), numpy::DataType::Uint32);
252+
assert!(dtype.is_equiv_to(numpy::PyArrayDescr::of::<u32>(py)));
253253
})
254254
}
255255

0 commit comments

Comments
 (0)