Skip to content

Commit acd0c2e

Browse files
committed
#156 Rename enum and related codes (FType → Role)
1 parent fdae924 commit acd0c2e

File tree

3 files changed

+56
-56
lines changed

3 files changed

+56
-56
lines changed

tests/test_typing.py

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414
Attr,
1515
Coord,
1616
Data,
17-
FType,
1817
Name,
18+
Role,
1919
get_dims,
2020
get_dtype,
21-
get_ftype,
2221
get_name,
22+
get_role,
2323
)
2424

2525

@@ -54,24 +54,6 @@
5454
(Union[Ann[Data[Any, float], "data"], Ann[Any, "any"]], np.dtype("f8")),
5555
]
5656

57-
testdata_ftype = [
58-
(Attr[Any], FType.ATTR),
59-
(Data[Any, Any], FType.DATA),
60-
(Coord[Any, Any], FType.COORD),
61-
(Name[Any], FType.NAME),
62-
(Any, FType.OTHER),
63-
(Ann[Attr[Any], "attr"], FType.ATTR),
64-
(Ann[Data[Any, Any], "data"], FType.DATA),
65-
(Ann[Coord[Any, Any], "coord"], FType.COORD),
66-
(Ann[Name[Any], "name"], FType.NAME),
67-
(Ann[Any, "other"], FType.OTHER),
68-
(Union[Ann[Attr[Any], "attr"], Ann[Any, "any"]], FType.ATTR),
69-
(Union[Ann[Data[Any, Any], "data"], Ann[Any, "any"]], FType.DATA),
70-
(Union[Ann[Coord[Any, Any], "coord"], Ann[Any, "any"]], FType.COORD),
71-
(Union[Ann[Name[Any], "name"], Ann[Any, "any"]], FType.NAME),
72-
(Union[Ann[Any, "other"], Ann[Any, "any"]], FType.OTHER),
73-
]
74-
7557
testdata_name = [
7658
(Attr[Any], None),
7759
(Data[Any, Any], None),
@@ -90,6 +72,24 @@
9072
(Union[Ann[Any, "other"], Ann[Any, "any"]], None),
9173
]
9274

75+
testdata_role = [
76+
(Attr[Any], Role.ATTR),
77+
(Data[Any, Any], Role.DATA),
78+
(Coord[Any, Any], Role.COORD),
79+
(Name[Any], Role.NAME),
80+
(Any, Role.OTHER),
81+
(Ann[Attr[Any], "attr"], Role.ATTR),
82+
(Ann[Data[Any, Any], "data"], Role.DATA),
83+
(Ann[Coord[Any, Any], "coord"], Role.COORD),
84+
(Ann[Name[Any], "name"], Role.NAME),
85+
(Ann[Any, "other"], Role.OTHER),
86+
(Union[Ann[Attr[Any], "attr"], Ann[Any, "any"]], Role.ATTR),
87+
(Union[Ann[Data[Any, Any], "data"], Ann[Any, "any"]], Role.DATA),
88+
(Union[Ann[Coord[Any, Any], "coord"], Ann[Any, "any"]], Role.COORD),
89+
(Union[Ann[Name[Any], "name"], Ann[Any, "any"]], Role.NAME),
90+
(Union[Ann[Any, "other"], Ann[Any, "any"]], Role.OTHER),
91+
]
92+
9393

9494
# test functions
9595
@mark.parametrize("tp, dims", testdata_dims)
@@ -102,11 +102,11 @@ def test_get_dtype(tp: Any, dtype: Any) -> None:
102102
assert get_dtype(tp) == dtype
103103

104104

105-
@mark.parametrize("tp, ftype", testdata_ftype)
106-
def test_get_ftype(tp: Any, ftype: Any) -> None:
107-
assert get_ftype(tp) == ftype
108-
109-
110105
@mark.parametrize("tp, name", testdata_name)
111106
def test_get_name(tp: Any, name: Any) -> None:
112107
assert get_name(tp) == name
108+
109+
110+
@mark.parametrize("tp, role", testdata_role)
111+
def test_get_role(tp: Any, role: Any) -> None:
112+
assert get_role(tp) == role

xarray_dataclasses/datamodel.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,16 @@
1717
from .typing import (
1818
AnyDType,
1919
AnyField,
20-
DataClass,
2120
AnyXarray,
21+
DataClass,
2222
Dims,
23-
FType,
23+
Role,
2424
get_annotated,
2525
get_dataclass,
2626
get_dims,
2727
get_dtype,
28-
get_ftype,
2928
get_name,
29+
get_role,
3030
)
3131

3232

@@ -209,29 +209,29 @@ def eval_dataclass(dataclass: AnyDataClass[PInit]) -> None:
209209

210210
def get_entry(field: AnyField, value: Any) -> Optional[AnyEntry]:
211211
"""Create an entry from a field and its value."""
212-
ftype = get_ftype(field.type)
212+
role = get_role(field.type)
213213
name = get_name(field.type, field.name)
214214

215-
if ftype is FType.ATTR or ftype is FType.NAME:
215+
if role is Role.ATTR or role is Role.NAME:
216216
return AttrEntry(
217217
name=name,
218-
tag=ftype.value,
218+
tag=role.value,
219219
value=value,
220220
type=get_annotated(field.type),
221221
)
222222

223-
if ftype is FType.COORD or ftype is FType.DATA:
223+
if role is Role.COORD or role is Role.DATA:
224224
try:
225225
return DataEntry(
226226
name=name,
227-
tag=ftype.value,
227+
tag=role.value,
228228
base=get_dataclass(field.type),
229229
value=value,
230230
)
231231
except TypeError:
232232
return DataEntry(
233233
name=name,
234-
tag=ftype.value,
234+
tag=role.value,
235235
dims=get_dims(field.type),
236236
dtype=get_dtype(field.type),
237237
value=value,

xarray_dataclasses/typing.py

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ class Labeled(Generic[TDims]):
8787

8888

8989
# type hints (public)
90-
class FType(Enum):
90+
class Role(Enum):
9191
"""Annotations for typing dataclass fields."""
9292

9393
ATTR = "attr"
@@ -107,14 +107,14 @@ class FType(Enum):
107107

108108
@classmethod
109109
def annotates(cls, tp: Any) -> bool:
110-
"""Check if any ftype annotates a type hint."""
110+
"""Check if any role annotates a type hint."""
111111
if get_origin(tp) is not Annotated:
112112
return False
113113

114114
return any(isinstance(arg, cls) for arg in get_args(tp))
115115

116116

117-
Attr = Annotated[T, FType.ATTR]
117+
Attr = Annotated[T, Role.ATTR]
118118
"""Type hint for attribute fields (``Attr[T]``).
119119
120120
Example:
@@ -137,7 +137,7 @@ class Image(AsDataArray):
137137
138138
"""
139139

140-
Coord = Annotated[Union[Labeled[TDims], Collection[TDType], TDType], FType.COORD]
140+
Coord = Annotated[Union[Labeled[TDims], Collection[TDType], TDType], Role.COORD]
141141
"""Type hint for coordinate fields (``Coord[TDims, TDType]``).
142142
143143
Example:
@@ -156,7 +156,7 @@ class Image(AsDataArray):
156156
157157
"""
158158

159-
Coordof = Annotated[Union[TDataClass, Any], FType.COORD]
159+
Coordof = Annotated[Union[TDataClass, Any], Role.COORD]
160160
"""Type hint for coordinate fields (``Coordof[TDataClass]``).
161161
162162
Unlike ``Coord``, it specifies a dataclass that defines a DataArray class.
@@ -188,7 +188,7 @@ class Image(AsDataArray):
188188
189189
"""
190190

191-
Data = Annotated[Union[Labeled[TDims], Collection[TDType], TDType], FType.DATA]
191+
Data = Annotated[Union[Labeled[TDims], Collection[TDType], TDType], Role.DATA]
192192
"""Type hint for data fields (``Coordof[TDims, TDType]``).
193193
194194
Example:
@@ -209,7 +209,7 @@ class ColorImage(AsDataset):
209209
210210
"""
211211

212-
Dataof = Annotated[Union[TDataClass, Any], FType.DATA]
212+
Dataof = Annotated[Union[TDataClass, Any], Role.DATA]
213213
"""Type hint for data fields (``Coordof[TDataClass]``).
214214
215215
Unlike ``Data``, it specifies a dataclass that defines a DataArray class.
@@ -236,7 +236,7 @@ class ColorImage(AsDataset):
236236
237237
"""
238238

239-
Name = Annotated[THashable, FType.NAME]
239+
Name = Annotated[THashable, Role.NAME]
240240
"""Type hint for name fields (``Name[THashable]``).
241241
242242
Example:
@@ -272,19 +272,19 @@ def find_annotated(tp: Any) -> Iterable[Any]:
272272

273273

274274
def get_annotated(tp: Any) -> Any:
275-
"""Extract the first ftype-annotated type."""
276-
for annotated in filter(FType.annotates, find_annotated(tp)):
275+
"""Extract the first role-annotated type."""
276+
for annotated in filter(Role.annotates, find_annotated(tp)):
277277
return deannotate(annotated)
278278

279-
raise TypeError("Could not find any ftype-annotated type.")
279+
raise TypeError("Could not find any role-annotated type.")
280280

281281

282282
def get_annotations(tp: Any) -> Tuple[Any, ...]:
283-
"""Extract annotations of the first ftype-annotated type."""
284-
for annotated in filter(FType.annotates, find_annotated(tp)):
283+
"""Extract annotations of the first role-annotated type."""
284+
for annotated in filter(Role.annotates, find_annotated(tp)):
285285
return get_args(annotated)[1:]
286286

287-
raise TypeError("Could not find any ftype-annotated type.")
287+
raise TypeError("Could not find any role-annotated type.")
288288

289289

290290
def get_dataclass(tp: Any) -> Type[DataClass[Any]]:
@@ -341,14 +341,6 @@ def get_dtype(tp: Any) -> Optional[AnyDType]:
341341
return np.dtype(dtype)
342342

343343

344-
def get_ftype(tp: Any, default: FType = FType.OTHER) -> FType:
345-
"""Extract an ftype if found or return given default."""
346-
try:
347-
return get_annotations(tp)[0]
348-
except TypeError:
349-
return default
350-
351-
352344
def get_name(tp: Any, default: Hashable = None) -> Hashable:
353345
"""Extract a name if found or return given default."""
354346
try:
@@ -361,3 +353,11 @@ def get_name(tp: Any, default: Hashable = None) -> Hashable:
361353
return annotation
362354

363355
return default
356+
357+
358+
def get_role(tp: Any, default: Role = Role.OTHER) -> Role:
359+
"""Extract a role if found or return given default."""
360+
try:
361+
return get_annotations(tp)[0]
362+
except TypeError:
363+
return default

0 commit comments

Comments
 (0)