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

Commitb08641a

Browse files
committed
fix: more covariance
1 parentadd59e7 commitb08641a

File tree

5 files changed

+46
-36
lines changed

5 files changed

+46
-36
lines changed

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
SupportsIndex,
1616
SupportsInt,
1717
TypeGuard,
18+
TypeVar,
1819
)
1920

2021
importnumpyasnp
@@ -66,10 +67,19 @@
6667
NumpyEndiannessStr=Literal[">","<","="]
6768
NUMPY_ENDIANNESS_STR:Final=">","<","="
6869

69-
typeTFloatDType=np.dtypes.Float16DType|np.dtypes.Float32DType|np.dtypes.Float64DType
70-
typeTFloatScalar=np.float16|np.float32|np.float64
71-
typeTComplexDType=np.dtypes.Complex64DType|np.dtypes.Complex128DType
72-
typeTComplexScalar=np.complex64|np.complex128
70+
TFloatDType_co=TypeVar(
71+
"TFloatDType_co",
72+
bound=np.dtypes.Float16DType|np.dtypes.Float32DType|np.dtypes.Float64DType,
73+
covariant=True,
74+
)
75+
TFloatScalar_co=TypeVar(
76+
"TFloatScalar_co",bound=np.float16|np.float32|np.float64,covariant=True
77+
)
78+
79+
TComplexDType_co=TypeVar(
80+
"TComplexDType_co",bound=np.dtypes.Complex64DType|np.dtypes.Complex128DType,covariant=True
81+
)
82+
TComplexScalar_co=TypeVar("TComplexScalar_co",bound=np.complex64|np.complex128,covariant=True)
7383

7484

7585
defendianness_from_numpy_str(endianness:NumpyEndiannessStr)->EndiannessStr:

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

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
)
2323
fromzarr.core.dtype.npy.commonimport (
2424
ComplexLike,
25-
TComplexDType,
26-
TComplexScalar,
25+
TComplexDType_co,
26+
TComplexScalar_co,
2727
check_json_complex_float_v2,
2828
check_json_complex_float_v3,
2929
complex_float_from_json_v2,
@@ -40,9 +40,7 @@
4040

4141

4242
@dataclass(frozen=True)
43-
classBaseComplex[DType:TComplexDType,Scalar:TComplexScalar](
44-
ZDType[DType,Scalar],HasEndianness,HasItemSize
45-
):
43+
classBaseComplex(ZDType[TComplexDType_co,TComplexScalar_co],HasEndianness,HasItemSize):
4644
"""
4745
A base class for Zarr data types that wrap NumPy complex float data types.
4846
"""
@@ -76,13 +74,13 @@ def from_native_dtype(cls, dtype: TBaseDType) -> Self:
7674
f"Invalid data type:{dtype}. Expected an instance of{cls.dtype_cls}"
7775
)
7876

79-
defto_native_dtype(self)->DType:
77+
defto_native_dtype(self)->TComplexDType_co:
8078
"""
8179
Convert this class to a NumPy complex dtype with the appropriate byte order.
8280
8381
Returns
8482
-------
85-
DType
83+
TComplexDType_co
8684
A NumPy data type object representing the complex data type with the specified byte order.
8785
"""
8886

@@ -238,7 +236,7 @@ def _check_scalar(self, data: object) -> TypeGuard[ComplexLike]:
238236
"""
239237
returnisinstance(data,ComplexLike)
240238

241-
def_cast_scalar_unchecked(self,data:ComplexLike)->Scalar:
239+
def_cast_scalar_unchecked(self,data:ComplexLike)->TComplexScalar_co:
242240
"""
243241
Cast the provided scalar data to the native scalar type of this class.
244242
@@ -249,7 +247,7 @@ def _cast_scalar_unchecked(self, data: ComplexLike) -> Scalar:
249247
250248
Returns
251249
-------
252-
Scalar
250+
TComplexScalar_co
253251
The casted data as a numpy complex scalar.
254252
255253
Notes
@@ -259,7 +257,7 @@ def _cast_scalar_unchecked(self, data: ComplexLike) -> Scalar:
259257
"""
260258
returnself.to_native_dtype().type(data)# type: ignore[return-value]
261259

262-
defcast_scalar(self,data:object)->Scalar:
260+
defcast_scalar(self,data:object)->TComplexScalar_co:
263261
"""
264262
Attempt to cast a given object to a numpy complex scalar.
265263
@@ -270,7 +268,7 @@ def cast_scalar(self, data: object) -> Scalar:
270268
271269
Returns
272270
-------
273-
Scalar
271+
TComplexScalar_co
274272
The data cast as a numpy complex scalar.
275273
276274
Raises
@@ -286,7 +284,7 @@ def cast_scalar(self, data: object) -> Scalar:
286284
)
287285
raiseTypeError(msg)
288286

289-
defdefault_scalar(self)->Scalar:
287+
defdefault_scalar(self)->TComplexScalar_co:
290288
"""
291289
Get the default value, which is 0 cast to this dtype
292290
@@ -297,7 +295,7 @@ def default_scalar(self) -> Scalar:
297295
"""
298296
returnself._cast_scalar_unchecked(0)
299297

300-
deffrom_json_scalar(self,data:JSON,*,zarr_format:ZarrFormat)->Scalar:
298+
deffrom_json_scalar(self,data:JSON,*,zarr_format:ZarrFormat)->TComplexScalar_co:
301299
"""
302300
Read a JSON-serializable value as a numpy float.
303301

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

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
)
1616
fromzarr.core.dtype.npy.commonimport (
1717
FloatLike,
18-
TFloatDType,
19-
TFloatScalar,
18+
TFloatDType_co,
19+
TFloatScalar_co,
2020
check_json_float_v2,
2121
check_json_float_v3,
2222
check_json_floatish_str,
@@ -34,9 +34,7 @@
3434

3535

3636
@dataclass(frozen=True)
37-
classBaseFloat[DType:TFloatDType,Scalar:TFloatScalar](
38-
ZDType[DType,Scalar],HasEndianness,HasItemSize
39-
):
37+
classBaseFloat(ZDType[TFloatDType_co,TFloatScalar_co],HasEndianness,HasItemSize):
4038
"""
4139
A base class for Zarr data types that wrap NumPy float data types.
4240
"""
@@ -65,13 +63,13 @@ def from_native_dtype(cls, dtype: TBaseDType) -> Self:
6563
f"Invalid data type:{dtype}. Expected an instance of{cls.dtype_cls}"
6664
)
6765

68-
defto_native_dtype(self)->DType:
66+
defto_native_dtype(self)->TFloatDType_co:
6967
"""
7068
Convert the wrapped data type to a NumPy data type.
7169
7270
Returns
7371
-------
74-
DType
72+
TFloatDType_co
7573
The NumPy data type.
7674
"""
7775
byte_order=endianness_to_numpy_str(self.endianness)
@@ -206,7 +204,7 @@ def _check_scalar(self, data: object) -> TypeGuard[FloatLike]:
206204
"""
207205
returnisinstance(data,FloatLike)
208206

209-
def_cast_scalar_unchecked(self,data:FloatLike)->Scalar:
207+
def_cast_scalar_unchecked(self,data:FloatLike)->TFloatScalar_co:
210208
"""
211209
Cast a scalar value to a NumPy float scalar.
212210
@@ -217,12 +215,12 @@ def _cast_scalar_unchecked(self, data: FloatLike) -> Scalar:
217215
218216
Returns
219217
-------
220-
Scalar
218+
TFloatScalar_co
221219
The NumPy float scalar.
222220
"""
223221
returnself.to_native_dtype().type(data)# type: ignore[return-value]
224222

225-
defcast_scalar(self,data:object)->Scalar:
223+
defcast_scalar(self,data:object)->TFloatScalar_co:
226224
"""
227225
Cast a scalar value to a NumPy float scalar.
228226
@@ -233,7 +231,7 @@ def cast_scalar(self, data: object) -> Scalar:
233231
234232
Returns
235233
-------
236-
Scalar
234+
TFloatScalar_co
237235
The NumPy float scalar.
238236
"""
239237
ifself._check_scalar(data):
@@ -244,18 +242,18 @@ def cast_scalar(self, data: object) -> Scalar:
244242
)
245243
raiseTypeError(msg)
246244

247-
defdefault_scalar(self)->Scalar:
245+
defdefault_scalar(self)->TFloatScalar_co:
248246
"""
249247
Get the default value, which is 0 cast to this zdtype.
250248
251249
Returns
252250
-------
253-
Scalar
251+
TFloatScalar_co
254252
The default value.
255253
"""
256254
returnself._cast_scalar_unchecked(0)
257255

258-
deffrom_json_scalar(self,data:JSON,*,zarr_format:ZarrFormat)->Scalar:
256+
deffrom_json_scalar(self,data:JSON,*,zarr_format:ZarrFormat)->TFloatScalar_co:
259257
"""
260258
Read a JSON-serializable value as a NumPy float scalar.
261259
@@ -268,7 +266,7 @@ def from_json_scalar(self, data: JSON, *, zarr_format: ZarrFormat) -> Scalar:
268266
269267
Returns
270268
-------
271-
Scalar
269+
TFloatScalar_co
272270
The NumPy float scalar.
273271
"""
274272
ifzarr_format==2:

‎tests/test_dtype/test_npy/test_complex.py‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
importnumpyasnp
66

77
fromtests.test_dtype.test_wrapperimportBaseTestZDType
8-
fromzarr.core.dtype.npy.commonimportTComplexDType,TComplexScalar
98
fromzarr.core.dtype.npy.compleximportComplex64,Complex128
109

1110

12-
class_BaseTestFloat[DType:TComplexDType,Scalar:TComplexScalar](BaseTestZDType[DType,Scalar]):
11+
class_BaseTestFloat[
12+
DType:np.dtypes.Complex64DType|np.dtypes.Complex128DType,
13+
Scalar:np.complex64|np.complex128,
14+
](BaseTestZDType[DType,Scalar]):
1315
defscalar_equals(self,scalar1:object,scalar2:object)->bool:
1416
ifnp.isnan(scalar1)andnp.isnan(scalar2):# type: ignore[call-overload]
1517
returnTrue

‎tests/test_dtype/test_npy/test_float.py‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
importnumpyasnp
44

55
fromtests.test_dtype.test_wrapperimportBaseTestZDType
6-
fromzarr.core.dtype.npy.commonimportTFloatDType,TFloatScalar
76
fromzarr.core.dtype.npy.floatimportFloat16,Float32,Float64
87

98

10-
class_BaseTestFloat[DType:TFloatDType,Scalar:TFloatScalar](BaseTestZDType[DType,Scalar]):
9+
class_BaseTestFloat[
10+
DType:np.dtypes.Float16DType|np.dtypes.Float32DType|np.dtypes.Float64DType,
11+
Scalar:np.float16|np.float32|np.float64,
12+
](BaseTestZDType[DType,Scalar]):
1113
defscalar_equals(self,scalar1:object,scalar2:object)->bool:
1214
ifnp.isnan(scalar1)andnp.isnan(scalar2):# type: ignore[call-overload]
1315
returnTrue

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp