File tree Expand file tree Collapse file tree 2 files changed +46
-8
lines changed Expand file tree Collapse file tree 2 files changed +46
-8
lines changed Original file line number Diff line number Diff line change 8
8
9
9
10
10
# submodules
11
- from xarray_dataclasses .typing import ArrayLike , get_dims , get_dtype
11
+ from xarray_dataclasses .typing import (
12
+ ArrayLike ,
13
+ Attr ,
14
+ Coord ,
15
+ Data ,
16
+ Name ,
17
+ get_dims ,
18
+ get_dtype ,
19
+ get_field_type ,
20
+ )
12
21
13
22
14
23
# type hints
41
50
(ArrayLike [Any , int ], "int" ),
42
51
]
43
52
53
+ testdata_field_type = [
54
+ (Attr [Any ], "attr" ),
55
+ (Coord [Any , Any ], "coord" ),
56
+ (Data [Any , Any ], "data" ),
57
+ (Name [Any ], "name" ),
58
+ ]
59
+
44
60
45
61
# test functions
46
62
@mark .parametrize ("type_, dims" , testdata_dims )
@@ -51,3 +67,8 @@ def test_get_dims(type_: Any, dims: Any) -> None:
51
67
@mark .parametrize ("type_, dtype" , testdata_dtype )
52
68
def test_get_dtype (type_ : Any , dtype : Any ) -> None :
53
69
assert get_dtype (type_ ) == dtype
70
+
71
+
72
+ @mark .parametrize ("type_, field_type" , testdata_field_type )
73
+ def test_get_field_type (type_ : Any , field_type : Any ) -> None :
74
+ assert get_field_type (type_ ).value == field_type
Original file line number Diff line number Diff line change 19
19
20
20
# standard library
21
21
from dataclasses import Field
22
- from enum import auto , Enum
22
+ from enum import Enum
23
23
from typing import (
24
24
Any ,
25
25
ClassVar ,
52
52
class FieldType (Enum ):
53
53
"""Annotation of xarray-related field hints."""
54
54
55
- ATTR = auto ()
55
+ ATTR = "attr"
56
56
"""Annotation of attribute field hints."""
57
57
58
- COORD = auto ()
58
+ COORD = "coord"
59
59
"""Annotation of coordinate field hints."""
60
60
61
- COORDOF = auto ()
61
+ COORDOF = "coordof"
62
62
"""Annotation of coordinate field hints."""
63
63
64
- DATA = auto ()
64
+ DATA = "data"
65
65
"""Annotation of data (variable) field hints."""
66
66
67
- DATAOF = auto ()
67
+ DATAOF = "dataof"
68
68
"""Annotation of data (variable) field hints."""
69
69
70
- NAME = auto ()
70
+ NAME = "name"
71
71
"""Annotation of name field hints."""
72
72
73
73
def annotates (self , hint : Any ) -> bool :
@@ -318,6 +318,23 @@ def get_dtype(type_: Any) -> Dtype:
318
318
raise ValueError (f"Could not convert { type_ !r} to dtype." )
319
319
320
320
321
+ def get_field_type (type_ : Any ) -> FieldType :
322
+ """Parse a type and return a field type if it exists."""
323
+ if FieldType .ATTR .annotates (type_ ):
324
+ return FieldType .ATTR
325
+
326
+ if FieldType .COORD .annotates (type_ ):
327
+ return FieldType .COORD
328
+
329
+ if FieldType .DATA .annotates (type_ ):
330
+ return FieldType .DATA
331
+
332
+ if FieldType .NAME .annotates (type_ ):
333
+ return FieldType .NAME
334
+
335
+ raise TypeError (f"Could not find any field type in { type_ !r} ." )
336
+
337
+
321
338
def get_inner (hint : Any , * indexes : int ) -> Any :
322
339
"""Return an inner type hint by indexes."""
323
340
if not indexes :
You can’t perform that action at this time.
0 commit comments