Skip to content

Commit 7c1bb2a

Browse files
committed
#143 Rename type variables to be readable
1 parent eee9460 commit 7c1bb2a

File tree

4 files changed

+47
-47
lines changed

4 files changed

+47
-47
lines changed

xarray_dataclasses/dataarray.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@
3030

3131

3232
# type hints
33-
P = ParamSpec("P")
33+
PInit = ParamSpec("PInit")
3434
TDataArray = TypeVar("TDataArray", bound=xr.DataArray)
3535

3636

37-
class OptionedClass(Protocol[P, TDataArray]):
37+
class OptionedClass(Protocol[PInit, TDataArray]):
3838
"""Type hint for dataclass objects with options."""
3939

40-
def __init__(self, *args: P.args, **kwargs: P.kwargs) -> None:
40+
def __init__(self, *args: PInit.args, **kwargs: PInit.kwargs) -> None:
4141
...
4242

4343
__dataclass_fields__: ClassVar[DataClassFields]
@@ -47,7 +47,7 @@ def __init__(self, *args: P.args, **kwargs: P.kwargs) -> None:
4747
# runtime functions
4848
@overload
4949
def asdataarray(
50-
dataclass: OptionedClass[P, TDataArray],
50+
dataclass: OptionedClass[PInit, TDataArray],
5151
reference: Optional[DataType] = None,
5252
dataoptions: None = None,
5353
) -> TDataArray:
@@ -56,7 +56,7 @@ def asdataarray(
5656

5757
@overload
5858
def asdataarray(
59-
dataclass: DataClass[P],
59+
dataclass: DataClass[PInit],
6060
reference: Optional[DataType] = None,
6161
dataoptions: None = None,
6262
) -> xr.DataArray:
@@ -130,16 +130,16 @@ def __init__(self, func: Any) -> None:
130130
def __get__(
131131
self,
132132
obj: Any,
133-
cls: Type[OptionedClass[P, TDataArray]],
134-
) -> Callable[P, TDataArray]:
133+
cls: Type[OptionedClass[PInit, TDataArray]],
134+
) -> Callable[PInit, TDataArray]:
135135
...
136136

137137
@overload
138138
def __get__(
139139
self,
140140
obj: Any,
141-
cls: Type[DataClass[P]],
142-
) -> Callable[P, xr.DataArray]:
141+
cls: Type[DataClass[PInit]],
142+
) -> Callable[PInit, xr.DataArray]:
143143
...
144144

145145
def __get__(self, obj: Any, cls: Any) -> Any:
@@ -166,7 +166,7 @@ def new(cls: Any, *args: Any, **kwargs: Any) -> Any:
166166
@overload
167167
@classmethod
168168
def shaped(
169-
cls: Type[OptionedClass[P, TDataArray]],
169+
cls: Type[OptionedClass[PInit, TDataArray]],
170170
func: Callable[[Shape], np.ndarray],
171171
shape: Union[Shape, Sizes],
172172
**kwargs: Any,
@@ -176,7 +176,7 @@ def shaped(
176176
@overload
177177
@classmethod
178178
def shaped(
179-
cls: Type[DataClass[P]],
179+
cls: Type[DataClass[PInit]],
180180
func: Callable[[Shape], np.ndarray],
181181
shape: Union[Shape, Sizes],
182182
**kwargs: Any,
@@ -212,7 +212,7 @@ def shaped(
212212
@overload
213213
@classmethod
214214
def empty(
215-
cls: Type[OptionedClass[P, TDataArray]],
215+
cls: Type[OptionedClass[PInit, TDataArray]],
216216
shape: Union[Shape, Sizes],
217217
order: Order = "C",
218218
**kwargs: Any,
@@ -222,7 +222,7 @@ def empty(
222222
@overload
223223
@classmethod
224224
def empty(
225-
cls: Type[DataClass[P]],
225+
cls: Type[DataClass[PInit]],
226226
shape: Union[Shape, Sizes],
227227
order: Order = "C",
228228
**kwargs: Any,
@@ -254,7 +254,7 @@ def empty(
254254
@overload
255255
@classmethod
256256
def zeros(
257-
cls: Type[OptionedClass[P, TDataArray]],
257+
cls: Type[OptionedClass[PInit, TDataArray]],
258258
shape: Union[Shape, Sizes],
259259
order: Order = "C",
260260
**kwargs: Any,
@@ -264,7 +264,7 @@ def zeros(
264264
@overload
265265
@classmethod
266266
def zeros(
267-
cls: Type[DataClass[P]],
267+
cls: Type[DataClass[PInit]],
268268
shape: Union[Shape, Sizes],
269269
order: Order = "C",
270270
**kwargs: Any,
@@ -296,7 +296,7 @@ def zeros(
296296
@overload
297297
@classmethod
298298
def ones(
299-
cls: Type[OptionedClass[P, TDataArray]],
299+
cls: Type[OptionedClass[PInit, TDataArray]],
300300
shape: Union[Shape, Sizes],
301301
order: Order = "C",
302302
**kwargs: Any,
@@ -306,7 +306,7 @@ def ones(
306306
@overload
307307
@classmethod
308308
def ones(
309-
cls: Type[DataClass[P]],
309+
cls: Type[DataClass[PInit]],
310310
shape: Union[Shape, Sizes],
311311
order: Order = "C",
312312
**kwargs: Any,
@@ -338,7 +338,7 @@ def ones(
338338
@overload
339339
@classmethod
340340
def full(
341-
cls: Type[OptionedClass[P, TDataArray]],
341+
cls: Type[OptionedClass[PInit, TDataArray]],
342342
shape: Union[Shape, Sizes],
343343
fill_value: Any,
344344
order: Order = "C",
@@ -349,7 +349,7 @@ def full(
349349
@overload
350350
@classmethod
351351
def full(
352-
cls: Type[DataClass[P]],
352+
cls: Type[DataClass[PInit]],
353353
shape: Union[Shape, Sizes],
354354
fill_value: Any,
355355
order: Order = "C",

xarray_dataclasses/datamodel.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727

2828

2929
# type hints
30-
P = ParamSpec("P")
31-
AnyDataClass = Union[Type[DataClass[P]], DataClass[P]]
30+
PInit = ParamSpec("PInit")
31+
AnyDataClass = Union[Type[DataClass[PInit]], DataClass[PInit]]
3232
AnyEntry = Union["AttrEntry", "DataEntry"]
3333

3434

@@ -167,7 +167,7 @@ def names(self) -> List[AttrEntry]:
167167
return [v for v in self.entries.values() if v.tag == "name"]
168168

169169
@classmethod
170-
def from_dataclass(cls, dataclass: AnyDataClass[P]) -> "DataModel":
170+
def from_dataclass(cls, dataclass: AnyDataClass[PInit]) -> "DataModel":
171171
"""Create a data model from a dataclass or its object."""
172172
model = cls()
173173
eval_dataclass(dataclass)
@@ -183,7 +183,7 @@ def from_dataclass(cls, dataclass: AnyDataClass[P]) -> "DataModel":
183183

184184

185185
# runtime functions
186-
def eval_dataclass(dataclass: AnyDataClass[P]) -> None:
186+
def eval_dataclass(dataclass: AnyDataClass[PInit]) -> None:
187187
"""Evaluate field types of a dataclass."""
188188
if not is_dataclass(dataclass):
189189
raise TypeError("Not a dataclass or its object.")

xarray_dataclasses/dataset.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@
2121

2222

2323
# type hints
24-
P = ParamSpec("P")
24+
PInit = ParamSpec("PInit")
2525
TDataset = TypeVar("TDataset", bound=xr.Dataset)
2626

2727

28-
class OptionedClass(Protocol[P, TDataset]):
28+
class OptionedClass(Protocol[PInit, TDataset]):
2929
"""Type hint for dataclass objects with options."""
3030

31-
def __init__(self, *args: P.args, **kwargs: P.kwargs) -> None:
31+
def __init__(self, *args: PInit.args, **kwargs: PInit.kwargs) -> None:
3232
...
3333

3434
__dataclass_fields__: ClassVar[DataClassFields]
@@ -38,7 +38,7 @@ def __init__(self, *args: P.args, **kwargs: P.kwargs) -> None:
3838
# runtime functions and classes
3939
@overload
4040
def asdataset(
41-
dataclass: OptionedClass[P, TDataset],
41+
dataclass: OptionedClass[PInit, TDataset],
4242
reference: Optional[DataType] = None,
4343
dataoptions: None = None,
4444
) -> TDataset:
@@ -47,7 +47,7 @@ def asdataset(
4747

4848
@overload
4949
def asdataset(
50-
dataclass: DataClass[P],
50+
dataclass: DataClass[PInit],
5151
reference: Optional[DataType] = None,
5252
dataoptions: None = None,
5353
) -> xr.Dataset:
@@ -121,16 +121,16 @@ def __init__(self, func: Callable[..., Any]) -> None:
121121
def __get__(
122122
self,
123123
obj: Any,
124-
cls: Type[OptionedClass[P, TDataset]],
125-
) -> Callable[P, TDataset]:
124+
cls: Type[OptionedClass[PInit, TDataset]],
125+
) -> Callable[PInit, TDataset]:
126126
...
127127

128128
@overload
129129
def __get__(
130130
self,
131131
obj: Any,
132-
cls: Type[DataClass[P]],
133-
) -> Callable[P, xr.Dataset]:
132+
cls: Type[DataClass[PInit]],
133+
) -> Callable[PInit, xr.Dataset]:
134134
...
135135

136136
def __get__(self, obj: Any, cls: Any) -> Any:
@@ -157,7 +157,7 @@ def new(cls: Any, *args: Any, **kwargs: Any) -> Any:
157157
@overload
158158
@classmethod
159159
def shaped(
160-
cls: Type[OptionedClass[P, TDataset]],
160+
cls: Type[OptionedClass[PInit, TDataset]],
161161
func: Callable[[Shape], np.ndarray],
162162
sizes: Sizes,
163163
**kwargs: Any,
@@ -167,7 +167,7 @@ def shaped(
167167
@overload
168168
@classmethod
169169
def shaped(
170-
cls: Type[DataClass[P]],
170+
cls: Type[DataClass[PInit]],
171171
func: Callable[[Shape], np.ndarray],
172172
sizes: Sizes,
173173
**kwargs: Any,
@@ -204,7 +204,7 @@ def shaped(
204204
@overload
205205
@classmethod
206206
def empty(
207-
cls: Type[OptionedClass[P, TDataset]],
207+
cls: Type[OptionedClass[PInit, TDataset]],
208208
sizes: Sizes,
209209
order: Order = "C",
210210
**kwargs: Any,
@@ -214,7 +214,7 @@ def empty(
214214
@overload
215215
@classmethod
216216
def empty(
217-
cls: Type[DataClass[P]],
217+
cls: Type[DataClass[PInit]],
218218
sizes: Sizes,
219219
order: Order = "C",
220220
**kwargs: Any,
@@ -246,7 +246,7 @@ def empty(
246246
@overload
247247
@classmethod
248248
def zeros(
249-
cls: Type[OptionedClass[P, TDataset]],
249+
cls: Type[OptionedClass[PInit, TDataset]],
250250
sizes: Sizes,
251251
order: Order = "C",
252252
**kwargs: Any,
@@ -256,7 +256,7 @@ def zeros(
256256
@overload
257257
@classmethod
258258
def zeros(
259-
cls: Type[DataClass[P]],
259+
cls: Type[DataClass[PInit]],
260260
sizes: Sizes,
261261
order: Order = "C",
262262
**kwargs: Any,
@@ -288,7 +288,7 @@ def zeros(
288288
@overload
289289
@classmethod
290290
def ones(
291-
cls: Type[OptionedClass[P, TDataset]],
291+
cls: Type[OptionedClass[PInit, TDataset]],
292292
sizes: Sizes,
293293
order: Order = "C",
294294
**kwargs: Any,
@@ -298,7 +298,7 @@ def ones(
298298
@overload
299299
@classmethod
300300
def ones(
301-
cls: Type[DataClass[P]],
301+
cls: Type[DataClass[PInit]],
302302
sizes: Sizes,
303303
order: Order = "C",
304304
**kwargs: Any,
@@ -330,7 +330,7 @@ def ones(
330330
@overload
331331
@classmethod
332332
def full(
333-
cls: Type[OptionedClass[P, TDataset]],
333+
cls: Type[OptionedClass[PInit, TDataset]],
334334
sizes: Sizes,
335335
fill_value: Any,
336336
order: Order = "C",
@@ -341,7 +341,7 @@ def full(
341341
@overload
342342
@classmethod
343343
def full(
344-
cls: Type[DataClass[P]],
344+
cls: Type[DataClass[PInit]],
345345
sizes: Sizes,
346346
fill_value: Any,
347347
order: Order = "C",

xarray_dataclasses/typing.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@
4949

5050

5151
# type hints
52-
P = ParamSpec("P")
53-
T = TypeVar("T")
52+
PInit = ParamSpec("PInit")
53+
TAttr = TypeVar("TAttr")
5454
TDataClass = TypeVar("TDataClass", bound="DataClass[Any]")
5555
TDims = TypeVar("TDims", covariant=True)
5656
TDtype = TypeVar("TDtype", covariant=True)
@@ -77,10 +77,10 @@ class Collection(Labeled[TDims], Collection[TDtype], Protocol):
7777
pass
7878

7979

80-
class DataClass(Protocol[P]):
80+
class DataClass(Protocol[PInit]):
8181
"""Type hint for dataclass objects."""
8282

83-
def __init__(self, *args: P.args, **kwargs: P.kwargs) -> None:
83+
def __init__(self, *args: PInit.args, **kwargs: PInit.kwargs) -> None:
8484
...
8585

8686
__dataclass_fields__: ClassVar[DataClassFields]
@@ -108,7 +108,7 @@ def annotates(self, hint: Any) -> bool:
108108

109109

110110
# public type hints
111-
Attr = Annotated[T, FieldType.ATTR]
111+
Attr = Annotated[TAttr, FieldType.ATTR]
112112
"""Type hint to define attribute fields (``Attr[T]``).
113113
114114
Example:

0 commit comments

Comments
 (0)