Skip to content

Commit e65d3ad

Browse files
committed
#135 Preferentially use data options if they are given in asdata* functions
1 parent 71d0ba7 commit e65d3ad

File tree

2 files changed

+52
-22
lines changed

2 files changed

+52
-22
lines changed

xarray_dataclasses/dataarray.py

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,6 @@
2929
from .typing import DataClass, DataClassFields, DataType, Order, Shape, Sizes
3030

3131

32-
# constants
33-
DEFAULT_OPTIONS = DataOptions(xr.DataArray)
34-
35-
3632
# type hints
3733
P = ParamSpec("P")
3834
TDataArray = TypeVar("TDataArray", bound=xr.DataArray)
@@ -53,7 +49,25 @@ def __init__(self, *args: P.args, **kwargs: P.kwargs) -> None:
5349
def asdataarray(
5450
dataclass: OptionedClass[P, TDataArray],
5551
reference: Optional[DataType] = None,
56-
dataoptions: DataOptions[Any] = DEFAULT_OPTIONS,
52+
dataoptions: None = None,
53+
) -> TDataArray:
54+
...
55+
56+
57+
@overload
58+
def asdataarray(
59+
dataclass: DataClass[P],
60+
reference: Optional[DataType] = None,
61+
dataoptions: None = None,
62+
) -> xr.DataArray:
63+
...
64+
65+
66+
@overload
67+
def asdataarray(
68+
dataclass: OptionedClass[P, Any],
69+
reference: Optional[DataType] = None,
70+
dataoptions: Optional[DataOptions[TDataArray]] = None,
5771
) -> TDataArray:
5872
...
5973

@@ -62,15 +76,15 @@ def asdataarray(
6276
def asdataarray(
6377
dataclass: DataClass[P],
6478
reference: Optional[DataType] = None,
65-
dataoptions: DataOptions[TDataArray] = DEFAULT_OPTIONS,
79+
dataoptions: Optional[DataOptions[TDataArray]] = None,
6680
) -> TDataArray:
6781
...
6882

6983

7084
def asdataarray(
7185
dataclass: Any,
7286
reference: Optional[DataType] = None,
73-
dataoptions: DataOptions[Any] = DEFAULT_OPTIONS,
87+
dataoptions: Any = None,
7488
) -> Any:
7589
"""Create a DataArray object from a dataclass object.
7690
@@ -83,10 +97,11 @@ def asdataarray(
8397
DataArray object created from the dataclass object.
8498
8599
"""
86-
try:
87-
dataoptions = dataclass.__dataoptions__
88-
except AttributeError:
89-
pass
100+
if dataoptions is None:
101+
try:
102+
dataoptions = dataclass.__dataoptions__
103+
except AttributeError:
104+
dataoptions = DataOptions(xr.DataArray)
90105

91106
model = DataModel.from_dataclass(dataclass)
92107
item = next(iter(model.data.values()))

xarray_dataclasses/dataset.py

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@
2020
from .typing import DataClass, DataClassFields, DataType, Order, Shape, Sizes
2121

2222

23-
# constants
24-
DEFAULT_OPTIONS = DataOptions(xr.Dataset)
25-
26-
2723
# type hints
2824
P = ParamSpec("P")
2925
TDataset = TypeVar("TDataset", bound=xr.Dataset)
@@ -44,7 +40,25 @@ def __init__(self, *args: P.args, **kwargs: P.kwargs) -> None:
4440
def asdataset(
4541
dataclass: OptionedClass[P, TDataset],
4642
reference: Optional[DataType] = None,
47-
dataoptions: DataOptions[Any] = DEFAULT_OPTIONS,
43+
dataoptions: None = None,
44+
) -> TDataset:
45+
...
46+
47+
48+
@overload
49+
def asdataset(
50+
dataclass: DataClass[P],
51+
reference: Optional[DataType] = None,
52+
dataoptions: None = None,
53+
) -> xr.Dataset:
54+
...
55+
56+
57+
@overload
58+
def asdataset(
59+
dataclass: OptionedClass[P, Any],
60+
reference: Optional[DataType] = None,
61+
dataoptions: Optional[DataOptions[TDataset]] = None,
4862
) -> TDataset:
4963
...
5064

@@ -53,15 +67,15 @@ def asdataset(
5367
def asdataset(
5468
dataclass: DataClass[P],
5569
reference: Optional[DataType] = None,
56-
dataoptions: DataOptions[TDataset] = DEFAULT_OPTIONS,
70+
dataoptions: Optional[DataOptions[TDataset]] = None,
5771
) -> TDataset:
5872
...
5973

6074

6175
def asdataset(
6276
dataclass: Any,
6377
reference: Optional[DataType] = None,
64-
dataoptions: DataOptions[Any] = DEFAULT_OPTIONS,
78+
dataoptions: Any = None,
6579
) -> Any:
6680
"""Create a Dataset object from a dataclass object.
6781
@@ -74,10 +88,11 @@ def asdataset(
7488
Dataset object created from the dataclass object.
7589
7690
"""
77-
try:
78-
dataoptions = dataclass.__dataoptions__
79-
except AttributeError:
80-
pass
91+
if dataoptions is None:
92+
try:
93+
dataoptions = dataclass.__dataoptions__
94+
except AttributeError:
95+
dataoptions = DataOptions(xr.Dataset)
8196

8297
model = DataModel.from_dataclass(dataclass)
8398
dataset = dataoptions.factory()

0 commit comments

Comments
 (0)