Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit98404bd

Browse files
committed
fix: try covariance handling
1 parent7ab6c47 commit98404bd

File tree

6 files changed

+55
-48
lines changed

6 files changed

+55
-48
lines changed

‎src/zarr/core/dtype/npy/complex.py‎

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222
)
2323
fromzarr.core.dtype.npy.commonimport (
2424
ComplexLike,
25-
TComplexDType_co,
26-
TComplexScalar_co,
2725
check_json_complex_float_v2,
2826
check_json_complex_float_v3,
2927
complex_float_from_json_v2,
@@ -40,7 +38,10 @@
4038

4139

4240
@dataclass(frozen=True)
43-
classBaseComplex(ZDType[TComplexDType_co,TComplexScalar_co],HasEndianness,HasItemSize):
41+
classBaseComplex[
42+
DType:np.dtypes.Complex64DType|np.dtypes.Complex128DType,
43+
Scalar:np.complex64|np.complex128,
44+
](ZDType[DType,Scalar],HasEndianness,HasItemSize):
4445
"""
4546
A base class for Zarr data types that wrap NumPy complex float data types.
4647
"""
@@ -74,13 +75,13 @@ def from_native_dtype(cls, dtype: TBaseDType) -> Self:
7475
f"Invalid data type:{dtype}. Expected an instance of{cls.dtype_cls}"
7576
)
7677

77-
defto_native_dtype(self)->TComplexDType_co:
78+
defto_native_dtype(self)->DType:
7879
"""
7980
Convert this class to a NumPy complex dtype with the appropriate byte order.
8081
8182
Returns
8283
-------
83-
TComplexDType_co
84+
DType
8485
A NumPy data type object representing the complex data type with the specified byte order.
8586
"""
8687

@@ -235,7 +236,7 @@ def _check_scalar(self, data: object) -> TypeGuard[ComplexLike]:
235236
"""
236237
returnisinstance(data,ComplexLike)
237238

238-
def_cast_scalar_unchecked(self,data:ComplexLike)->TComplexScalar_co:
239+
def_cast_scalar_unchecked(self,data:ComplexLike)->Scalar:
239240
"""
240241
Cast the provided scalar data to the native scalar type of this class.
241242
@@ -246,7 +247,7 @@ def _cast_scalar_unchecked(self, data: ComplexLike) -> TComplexScalar_co:
246247
247248
Returns
248249
-------
249-
TComplexScalar_co
250+
Scalar
250251
The casted data as a numpy complex scalar.
251252
252253
Notes
@@ -256,7 +257,7 @@ def _cast_scalar_unchecked(self, data: ComplexLike) -> TComplexScalar_co:
256257
"""
257258
returnself.to_native_dtype().type(data)# type: ignore[return-value]
258259

259-
defcast_scalar(self,data:object)->TComplexScalar_co:
260+
defcast_scalar(self,data:object)->Scalar:
260261
"""
261262
Attempt to cast a given object to a numpy complex scalar.
262263
@@ -267,7 +268,7 @@ def cast_scalar(self, data: object) -> TComplexScalar_co:
267268
268269
Returns
269270
-------
270-
TComplexScalar_co
271+
Scalar
271272
The data cast as a numpy complex scalar.
272273
273274
Raises
@@ -283,7 +284,7 @@ def cast_scalar(self, data: object) -> TComplexScalar_co:
283284
)
284285
raiseTypeError(msg)
285286

286-
defdefault_scalar(self)->TComplexScalar_co:
287+
defdefault_scalar(self)->Scalar:
287288
"""
288289
Get the default value, which is 0 cast to this dtype
289290
@@ -294,7 +295,7 @@ def default_scalar(self) -> TComplexScalar_co:
294295
"""
295296
returnself._cast_scalar_unchecked(0)
296297

297-
deffrom_json_scalar(self,data:JSON,*,zarr_format:ZarrFormat)->TComplexScalar_co:
298+
deffrom_json_scalar(self,data:JSON,*,zarr_format:ZarrFormat)->Scalar:
298299
"""
299300
Read a JSON-serializable value as a numpy float.
300301

‎src/zarr/core/dtype/npy/float.py‎

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
)
1616
fromzarr.core.dtype.npy.commonimport (
1717
FloatLike,
18-
TFloatDType_co,
19-
TFloatScalar_co,
2018
check_json_float_v2,
2119
check_json_float_v3,
2220
check_json_floatish_str,
@@ -34,7 +32,10 @@
3432

3533

3634
@dataclass(frozen=True)
37-
classBaseFloat(ZDType[TFloatDType_co,TFloatScalar_co],HasEndianness,HasItemSize):
35+
classBaseFloat[
36+
DType:np.dtypes.Float16DType|np.dtypes.Float32DType|np.dtypes.Float64DType,
37+
Scalar:np.float16|np.float32|np.float64,
38+
](ZDType[DType,Scalar],HasEndianness,HasItemSize):
3839
"""
3940
A base class for Zarr data types that wrap NumPy float data types.
4041
"""
@@ -63,13 +64,13 @@ def from_native_dtype(cls, dtype: TBaseDType) -> Self:
6364
f"Invalid data type:{dtype}. Expected an instance of{cls.dtype_cls}"
6465
)
6566

66-
defto_native_dtype(self)->TFloatDType_co:
67+
defto_native_dtype(self)->DType:
6768
"""
6869
Convert the wrapped data type to a NumPy data type.
6970
7071
Returns
7172
-------
72-
TFloatDType_co
73+
DType
7374
The NumPy data type.
7475
"""
7576
byte_order=endianness_to_numpy_str(self.endianness)
@@ -203,7 +204,7 @@ def _check_scalar(self, data: object) -> TypeGuard[FloatLike]:
203204
"""
204205
returnisinstance(data,FloatLike)
205206

206-
def_cast_scalar_unchecked(self,data:FloatLike)->TFloatScalar_co:
207+
def_cast_scalar_unchecked(self,data:FloatLike)->Scalar:
207208
"""
208209
Cast a scalar value to a NumPy float scalar.
209210
@@ -214,12 +215,12 @@ def _cast_scalar_unchecked(self, data: FloatLike) -> TFloatScalar_co:
214215
215216
Returns
216217
-------
217-
TFloatScalar_co
218+
Scalar
218219
The NumPy float scalar.
219220
"""
220221
returnself.to_native_dtype().type(data)# type: ignore[return-value]
221222

222-
defcast_scalar(self,data:object)->TFloatScalar_co:
223+
defcast_scalar(self,data:object)->Scalar:
223224
"""
224225
Cast a scalar value to a NumPy float scalar.
225226
@@ -230,7 +231,7 @@ def cast_scalar(self, data: object) -> TFloatScalar_co:
230231
231232
Returns
232233
-------
233-
TFloatScalar_co
234+
Scalar
234235
The NumPy float scalar.
235236
"""
236237
ifself._check_scalar(data):
@@ -241,18 +242,18 @@ def cast_scalar(self, data: object) -> TFloatScalar_co:
241242
)
242243
raiseTypeError(msg)
243244

244-
defdefault_scalar(self)->TFloatScalar_co:
245+
defdefault_scalar(self)->Scalar:
245246
"""
246247
Get the default value, which is 0 cast to this zdtype.
247248
248249
Returns
249250
-------
250-
TFloatScalar_co
251+
Scalar
251252
The default value.
252253
"""
253254
returnself._cast_scalar_unchecked(0)
254255

255-
deffrom_json_scalar(self,data:JSON,*,zarr_format:ZarrFormat)->TFloatScalar_co:
256+
deffrom_json_scalar(self,data:JSON,*,zarr_format:ZarrFormat)->Scalar:
256257
"""
257258
Read a JSON-serializable value as a NumPy float scalar.
258259
@@ -265,7 +266,7 @@ def from_json_scalar(self, data: JSON, *, zarr_format: ZarrFormat) -> TFloatScal
265266
266267
Returns
267268
-------
268-
TFloatScalar_co
269+
Scalar
269270
The NumPy float scalar.
270271
"""
271272
ifzarr_format==2:

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

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,10 @@
5454

5555

5656
@dataclass(frozen=True)
57-
classBaseInt(ZDType[TIntDType_co,TIntScalar_co],HasItemSize):
57+
classBaseInt[
58+
DType:_NumpyIntDType,
59+
Scalar:np.int8|np.int16|np.int32|np.int64|np.uint8|np.uint16|np.uint32|np.uint64,
60+
](ZDType[DType,Scalar],HasItemSize):
5861
"""
5962
A base class for integer data types in Zarr.
6063
@@ -129,7 +132,7 @@ def _check_scalar(self, data: object) -> TypeGuard[IntLike]:
129132

130133
returnisinstance(data,IntLike)
131134

132-
def_cast_scalar_unchecked(self,data:IntLike)->TIntScalar_co:
135+
def_cast_scalar_unchecked(self,data:IntLike)->Scalar:
133136
"""
134137
Casts a given scalar value to the native integer scalar type without type checking.
135138
@@ -140,13 +143,13 @@ def _cast_scalar_unchecked(self, data: IntLike) -> TIntScalar_co:
140143
141144
Returns
142145
-------
143-
TIntScalar_co
146+
Scalar
144147
The casted integer scalar of the native dtype.
145148
"""
146149

147150
returnself.to_native_dtype().type(data)# type: ignore[return-value]
148151

149-
defcast_scalar(self,data:object)->TIntScalar_co:
152+
defcast_scalar(self,data:object)->Scalar:
150153
"""
151154
Attempt to cast a given object to a NumPy integer scalar.
152155
@@ -157,7 +160,7 @@ def cast_scalar(self, data: object) -> TIntScalar_co:
157160
158161
Returns
159162
-------
160-
TIntScalar_co
163+
Scalar
161164
The data cast as a NumPy integer scalar.
162165
163166
Raises
@@ -174,18 +177,18 @@ def cast_scalar(self, data: object) -> TIntScalar_co:
174177
)
175178
raiseTypeError(msg)
176179

177-
defdefault_scalar(self)->TIntScalar_co:
180+
defdefault_scalar(self)->Scalar:
178181
"""
179182
Get the default value, which is 0 cast to this dtype.
180183
181184
Returns
182185
-------
183-
TIntScalar_co
186+
Scalar
184187
The default value.
185188
"""
186189
returnself._cast_scalar_unchecked(0)
187190

188-
deffrom_json_scalar(self,data:JSON,*,zarr_format:ZarrFormat)->TIntScalar_co:
191+
deffrom_json_scalar(self,data:JSON,*,zarr_format:ZarrFormat)->Scalar:
189192
"""
190193
Read a JSON-serializable value as a NumPy int scalar.
191194
@@ -198,7 +201,7 @@ def from_json_scalar(self, data: JSON, *, zarr_format: ZarrFormat) -> TIntScalar
198201
199202
Returns
200203
-------
201-
TIntScalar_co
204+
Scalar
202205
The NumPy int scalar.
203206
204207
Raises

‎src/zarr/core/dtype/npy/string.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
endianness_to_numpy_str,
3434
get_endianness_from_numpy_dtype,
3535
)
36-
fromzarr.core.dtype.wrapperimportTDType_co,ZDType
36+
fromzarr.core.dtype.wrapperimportZDType
3737

3838
ifTYPE_CHECKING:
3939
fromzarr.core.commonimportJSON,ZarrFormat
@@ -453,7 +453,7 @@ class VariableLengthUTF8JSON_V2(DTypeConfig_V2[Literal["|O"], Literal["vlen-utf8
453453
# If NumPy 2 is installed, then VariableLengthUTF8 is defined with the NumPy variable length
454454
# string dtype as the native dtype. Otherwise, VariableLengthUTF8 is defined with the NumPy object
455455
# dtype as the native dtype.
456-
classUTF8Base(ZDType[TDType_co,str],HasObjectCodec):
456+
classUTF8Base[DType:TBaseDType](ZDType[DType,str],HasObjectCodec):
457457
"""
458458
A base class for variable-length UTF-8 string data types.
459459

‎src/zarr/core/dtype/npy/time.py‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,10 @@ class DateTime64JSON_V2(DTypeConfig_V2[str, None]):
217217

218218

219219
@dataclass(frozen=True,kw_only=True,slots=True)
220-
classTimeDTypeBase(ZDType[BaseTimeDType_co,BaseTimeScalar_co],HasEndianness,HasItemSize):
220+
classTimeDTypeBase[
221+
DType:np.dtypes.TimeDelta64DType|np.dtypes.DateTime64DType,
222+
Scalar:np.timedelta64|np.datetime64,
223+
](ZDType[DType,Scalar],HasEndianness,HasItemSize):
221224
"""
222225
A base class for data types that represent time via the NumPy TimeDelta64 and DateTime64 data
223226
types.
@@ -275,7 +278,7 @@ def from_native_dtype(cls, dtype: TBaseDType) -> Self:
275278
f"Invalid data type:{dtype}. Expected an instance of{cls.dtype_cls}"
276279
)
277280

278-
defto_native_dtype(self)->BaseTimeDType_co:
281+
defto_native_dtype(self)->DType:
279282
# Numpy does not allow creating datetime64 or timedelta64 via
280283
# np.dtypes.{dtype_name}()
281284
# so we use np.dtype with a formatted string.
@@ -285,7 +288,7 @@ def to_native_dtype(self) -> BaseTimeDType_co:
285288
286289
Returns
287290
-------
288-
BaseTimeDType_co
291+
DType
289292
A NumPy data type object representing the time data type with
290293
the specified unit, scale factor, and byte order.
291294
"""

‎src/zarr/core/dtype/wrapper.py‎

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
fromtypingimport (
2929
TYPE_CHECKING,
3030
ClassVar,
31-
Generic,
3231
Literal,
3332
Self,
3433
TypeGuard,
@@ -44,10 +43,10 @@
4443

4544
# This the upper bound for the scalar types we support. It's numpy scalars + str,
4645
# because the new variable-length string dtype in numpy does not have a corresponding scalar type
47-
TBaseScalar=np.generic|str|bytes
46+
typeTBaseScalar=np.generic|str|bytes
4847
# This is the bound for the dtypes that we support. If we support non-numpy dtypes,
4948
# then this bound will need to be widened.
50-
TBaseDType=np.dtype[np.generic]
49+
typeTBaseDType=np.dtype[np.generic]
5150

5251
# These two type parameters are covariant because we want
5352
# x : ZDType[BaseDType, BaseScalar] = ZDType[SubDType, SubScalar]
@@ -57,7 +56,7 @@
5756

5857

5958
@dataclass(frozen=True,kw_only=True,slots=True)
60-
classZDType(ABC,Generic[TDType_co,TScalar_co]):# noqa: UP046
59+
classZDType[DType:TBaseDType,Scalar:TBaseScalar](ABC):
6160
"""
6261
Abstract base class for wrapping native array data types, e.g. numpy dtypes
6362
@@ -71,11 +70,11 @@ class variable, and it should generally be unique across different data types.
7170
"""
7271

7372
# this class will create a native data type
74-
dtype_cls:ClassVar[type[TDType_co]]
73+
dtype_cls:ClassVar[type[DType]]
7574
_zarr_v3_name:ClassVar[str]
7675

7776
@classmethod
78-
def_check_native_dtype(cls:type[Self],dtype:TBaseDType)->TypeGuard[TDType_co]:
77+
def_check_native_dtype(cls:type[Self],dtype:TBaseDType)->TypeGuard[DType]:
7978
"""
8079
Check that a native data type matches the dtype_cls class attribute.
8180
@@ -120,7 +119,7 @@ def from_native_dtype(cls: type[Self], dtype: TBaseDType) -> Self:
120119
raiseNotImplementedError# pragma: no cover
121120

122121
@abstractmethod
123-
defto_native_dtype(self:Self)->TDType_co:
122+
defto_native_dtype(self:Self)->DType:
124123
"""
125124
Return an instance of the wrapped data type. This operation inverts ``from_native_dtype``.
126125
@@ -206,7 +205,7 @@ def _check_scalar(self, data: object) -> bool:
206205
raiseNotImplementedError# pragma: no cover
207206

208207
@abstractmethod
209-
defcast_scalar(self,data:object)->TScalar_co:
208+
defcast_scalar(self,data:object)->Scalar:
210209
"""
211210
Cast a python object to the wrapped scalar type.
212211
@@ -226,7 +225,7 @@ def cast_scalar(self, data: object) -> TScalar_co:
226225
raiseNotImplementedError# pragma: no cover
227226

228227
@abstractmethod
229-
defdefault_scalar(self)->TScalar_co:
228+
defdefault_scalar(self)->Scalar:
230229
"""
231230
Get the default scalar value for the wrapped data type.
232231
@@ -242,7 +241,7 @@ def default_scalar(self) -> TScalar_co:
242241
raiseNotImplementedError# pragma: no cover
243242

244243
@abstractmethod
245-
deffrom_json_scalar(self:Self,data:JSON,*,zarr_format:ZarrFormat)->TScalar_co:
244+
deffrom_json_scalar(self:Self,data:JSON,*,zarr_format:ZarrFormat)->Scalar:
246245
"""
247246
Read a JSON-serializable value as a scalar.
248247

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp