Skip to content

Commit 7d14d82

Browse files
authored
Merge branch 'main' into issue-3780-ndarray-like-typing
2 parents eca64c0 + 93dd0e4 commit 7d14d82

File tree

4 files changed

+34
-2
lines changed

4 files changed

+34
-2
lines changed

changes/3797.bugfix.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix an issue that prevents the correct parsing of special NumPy ``uint32`` dtypes resulting e.g.
2+
from bit wise operations on ``uint32`` arrays on Windows.

mkdocs.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,6 @@ plugins:
216216
'developers/index.html.md': 'contributing.md'
217217
'developers/roadmap.html.md': 'https://zarr.readthedocs.io/en/v3.0.8/developers/roadmap.html'
218218
'api/zarr/creation.md': 'api/zarr/deprecated/creation.md'
219-
'api/zarr/codecs/numcodecs.md': 'api/zarr/deprecated/creation.md'
220219
'api.md': 'api/zarr/index.md'
221220
'api/zarr/metadata/migrate_v3.md': 'api/zarr/metadata.md'
222221

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1070,6 +1070,28 @@ class UInt32(BaseInt[np.dtypes.UInt32DType, np.uint32], HasEndianness):
10701070
_zarr_v3_name: ClassVar[Literal["uint32"]] = "uint32"
10711071
_zarr_v2_names: ClassVar[tuple[Literal[">u4"], Literal["<u4"]]] = (">u4", "<u4")
10721072

1073+
@classmethod
1074+
def _check_native_dtype(cls: type[Self], dtype: TBaseDType) -> TypeGuard[np.dtypes.UInt32DType]:
1075+
"""
1076+
A type guard that checks if the input is assignable to the type of ``cls.dtype_class``
1077+
1078+
This method is overridden for this particular data type because of a Windows-specific issue
1079+
where ``np.array([1], dtype=np.uint32) & 1`` creates an instance of ``np.dtypes.UIntDType``,
1080+
rather than an instance of ``np.dtypes.UInt32DType``, even though both represent 32-bit
1081+
unsigned integers. (In contrast to ``np.dtype('i')``, ``np.dtype('u')`` raises an error.)
1082+
1083+
Parameters
1084+
----------
1085+
dtype : TDType
1086+
The dtype to check.
1087+
1088+
Returns
1089+
-------
1090+
Bool
1091+
True if the dtype matches, False otherwise.
1092+
"""
1093+
return super()._check_native_dtype(dtype) or dtype == np.dtypes.UInt32DType()
1094+
10731095
@classmethod
10741096
def from_native_dtype(cls, dtype: TBaseDType) -> Self:
10751097
"""

tests/test_dtype/test_npy/test_int.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,16 @@ class TestUInt16(BaseTestZDType):
216216
class TestUInt32(BaseTestZDType):
217217
test_cls = UInt32
218218
scalar_type = np.uint32
219-
valid_dtype = (np.dtype(">u4"), np.dtype("<u4"))
219+
220+
# On Windows, this creates an UIntDType (instead of UInt32DType),
221+
# similar to how np.dtype('i') creates an IntDType instead of Int32DType.
222+
# However, np.dtype('u') raises a TypeError.
223+
uint_dtype = (np.array([1], dtype=np.uint32) & 1).dtype
224+
225+
# The behavior of some tests associated with this class variable are
226+
# order-dependent -- uint_dtype correctly fails certain tests only if it's not
227+
# in the last position of the tuple. I have no idea how this is possible!
228+
valid_dtype = (uint_dtype, np.dtype(">u4"), np.dtype("<u4"))
220229
invalid_dtype = (
221230
np.dtype(np.int8),
222231
np.dtype(np.int16),

0 commit comments

Comments
 (0)