Skip to content

Commit baabf08

Browse files
d-v-bjhamman
andauthored
Fix/windows int32 issue (#3151)
* fix int32 inference on windows * docstring * pre-commit * changelog --------- Co-authored-by: Joe Hamman <joe@earthmover.io>
1 parent bbdefac commit baabf08

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

changes/3151.bugfix.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fixed an issue preventing correct parsing of NumPy ``int32`` dtypes when constructed via
2+
``np.dtype('i')``.

src/zarr/core/dtype/npy/int.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,26 @@ class Int32(BaseInt[np.dtypes.Int32DType, np.int32], HasEndianness):
420420
_zarr_v3_name: ClassVar[Literal["int32"]] = "int32"
421421
_zarr_v2_names: ClassVar[tuple[Literal[">i4"], Literal["<i4"]]] = (">i4", "<i4")
422422

423+
@classmethod
424+
def _check_native_dtype(cls: type[Self], dtype: TBaseDType) -> TypeGuard[np.dtypes.Int32DType]:
425+
"""
426+
A type guard that checks if the input is assignable to the type of ``cls.dtype_class``
427+
428+
This method is overridden for this particular data type because of a windows-specific issue where
429+
np.dtype('i') is an instance of ``np.dtypes.IntDType``, not an instance of ``np.dtypes.Int32DType``.
430+
431+
Parameters
432+
----------
433+
dtype : TDType
434+
The dtype to check.
435+
436+
Returns
437+
-------
438+
Bool
439+
True if the dtype matches, False otherwise.
440+
"""
441+
return super()._check_native_dtype(dtype) or dtype == np.dtypes.Int32DType()
442+
423443
@classmethod
424444
def from_native_dtype(cls: type[Self], dtype: TBaseDType) -> Self:
425445
if cls._check_native_dtype(dtype):

tests/test_dtype/test_npy/test_int.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,10 @@ class TestInt16(BaseTestZDType):
7575
class TestInt32(BaseTestZDType):
7676
test_cls = Int32
7777
scalar_type = np.int32
78-
valid_dtype = (np.dtype(">i4"), np.dtype("<i4"))
78+
# The behavior of some tests associated with this class variable are
79+
# order-dependent -- np.dtype('i') correctly fails certain tests only if it's not
80+
# in the last position of the tuple. I have no idea how this is possible!
81+
valid_dtype = (np.dtype("i"), np.dtype(">i4"), np.dtype("<i4"))
7982
invalid_dtype = (
8083
np.dtype(np.int8),
8184
np.dtype(np.uint16),

0 commit comments

Comments
 (0)