xarray-annotated 0.5.2__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- xarray_annotated-0.5.2/LICENSE +21 -0
- xarray_annotated-0.5.2/PKG-INFO +156 -0
- xarray_annotated-0.5.2/README.md +129 -0
- xarray_annotated-0.5.2/pyproject.toml +93 -0
- xarray_annotated-0.5.2/pyproject.toml.orig +81 -0
- xarray_annotated-0.5.2/src/xarray_annotated/__init__.py +50 -0
- xarray_annotated-0.5.2/src/xarray_annotated/_annotations.py +119 -0
- xarray_annotated-0.5.2/src/xarray_annotated/_config.py +101 -0
- xarray_annotated-0.5.2/src/xarray_annotated/_reader.py +110 -0
- xarray_annotated-0.5.2/src/xarray_annotated/_writer.py +99 -0
- xarray_annotated-0.5.2/src/xarray_annotated/py.typed +0 -0
- xarray_annotated-0.5.2/src/xarray_annotated/schema/__init__.py +62 -0
- xarray_annotated-0.5.2/src/xarray_annotated/schema/_annotations.py +254 -0
- xarray_annotated-0.5.2/src/xarray_annotated/schema/_check.py +232 -0
- xarray_annotated-0.5.2/src/xarray_annotated/schema/_config.py +147 -0
- xarray_annotated-0.5.2/src/xarray_annotated/schema/_decorator.py +124 -0
- xarray_annotated-0.5.2/src/xarray_annotated/temporal/__init__.py +60 -0
- xarray_annotated-0.5.2/src/xarray_annotated/temporal/_annotations.py +154 -0
- xarray_annotated-0.5.2/src/xarray_annotated/temporal/_check.py +326 -0
- xarray_annotated-0.5.2/src/xarray_annotated/temporal/_config.py +182 -0
- xarray_annotated-0.5.2/src/xarray_annotated/temporal/_decorator.py +133 -0
- xarray_annotated-0.5.2/src/xarray_annotated/units/__init__.py +53 -0
- xarray_annotated-0.5.2/src/xarray_annotated/units/_annotations.py +169 -0
- xarray_annotated-0.5.2/src/xarray_annotated/units/_check.py +592 -0
- xarray_annotated-0.5.2/src/xarray_annotated/units/_config.py +237 -0
- xarray_annotated-0.5.2/src/xarray_annotated/units/_decorator.py +203 -0
- xarray_annotated-0.5.2/src/xarray_annotated/units/_registry.py +94 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Joe Marsh Rossney
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: xarray-annotated
|
|
3
|
+
Version: 0.5.2
|
|
4
|
+
Summary: Run-time validation of xarray DataArray properties (units, dims/coords/dtype) declared via typing.Annotated
|
|
5
|
+
Keywords: xarray,pint,units,validation,annotations
|
|
6
|
+
Author: jmarshrossney
|
|
7
|
+
Author-email: jmarshrossney <17361029+jmarshrossney@users.noreply.github.com>
|
|
8
|
+
License-Expression: MIT
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Classifier: Development Status :: 3 - Alpha
|
|
11
|
+
Classifier: Intended Audience :: Science/Research
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
15
|
+
Classifier: Topic :: Scientific/Engineering
|
|
16
|
+
Requires-Dist: pandas>=2.2
|
|
17
|
+
Requires-Dist: pint>=0.25.3
|
|
18
|
+
Requires-Dist: pint-xarray>=0.6.1
|
|
19
|
+
Requires-Dist: xarray>=2026.2.0
|
|
20
|
+
Requires-Dist: cf-xarray>=0.11.3 ; extra == 'cf'
|
|
21
|
+
Requires-Python: >=3.12
|
|
22
|
+
Project-URL: Homepage, https://jmarshrossney.github.io/xarray-annotated
|
|
23
|
+
Project-URL: Repository, https://github.com/jmarshrossney/xarray-annotated
|
|
24
|
+
Project-URL: Documentation, https://jmarshrossney.github.io/xarray-annotated
|
|
25
|
+
Provides-Extra: cf
|
|
26
|
+
Description-Content-Type: text/markdown
|
|
27
|
+
|
|
28
|
+
# xarray-annotated
|
|
29
|
+
|
|
30
|
+
[](https://pypi.org/project/xarray-annotated/)
|
|
31
|
+
[](https://pypi.org/project/xarray-annotated/)
|
|
32
|
+
[](https://github.com/jmarshrossney/xarray-annotated/actions/workflows/ci.yml)
|
|
33
|
+
[](https://jmarshrossney.github.io/xarray-annotated)
|
|
34
|
+
[](https://github.com/jmarshrossney/xarray-annotated/blob/main/LICENSE)
|
|
35
|
+
|
|
36
|
+
`xarray-annotated` enables run-time validation of `xarray.DataArray` properties declared in function signatures via [`typing.Annotated`](https://docs.python.org/3/library/typing.html#typing.Annotated).
|
|
37
|
+
|
|
38
|
+
The main idea is that annotations serve as a single source of truth for
|
|
39
|
+
|
|
40
|
+
- structural schema (dims, coords, dtype),
|
|
41
|
+
- physical units,
|
|
42
|
+
- frequency (and phase) of a time axis,
|
|
43
|
+
|
|
44
|
+
from which
|
|
45
|
+
|
|
46
|
+
- static checkers and run-time validation can derive expected properties,
|
|
47
|
+
- documentation can be generated automatically,
|
|
48
|
+
- automated tools (coding agents) receive steering.
|
|
49
|
+
|
|
50
|
+
For validating and automatically converting physical units we lean on the excellent [pint](https://pint.readthedocs.io/en/stable/) and [pint-xarray](https://pint-xarray.readthedocs.io).
|
|
51
|
+
([cf-xarray](https://cf-xarray.readthedocs.io) is an optional dependency for CF/UDUNITS unit strings.)
|
|
52
|
+
|
|
53
|
+
For full user documentation please visit **[https://jmarshrossney.github.io/xarray-annotated/](https://jmarshrossney.github.io/xarray-annotated/)**.
|
|
54
|
+
|
|
55
|
+
## Installation
|
|
56
|
+
|
|
57
|
+
Either using `uv` (recommended) or `pip`:
|
|
58
|
+
|
|
59
|
+
```sh
|
|
60
|
+
uv add xarray-annotated
|
|
61
|
+
# or
|
|
62
|
+
pip install xarray-annotated
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
CF-convention / UDUNITS unit strings (e.g. `"umol m-2 s-1"`) need the optional `cf` extra, which pulls in [cf-xarray](https://cf-xarray.readthedocs.io).
|
|
66
|
+
|
|
67
|
+
```sh
|
|
68
|
+
uv add "xarray-annotated[cf]"
|
|
69
|
+
# or
|
|
70
|
+
pip install "xarray-annotated[cf]"
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
## A short example
|
|
74
|
+
|
|
75
|
+
Here is a function that converts barometric pressure to altitude.
|
|
76
|
+
It assumes a unit (pascals) and says so in the docstring but doesn't check or enforce it.
|
|
77
|
+
|
|
78
|
+
```python
|
|
79
|
+
import xarray as xr
|
|
80
|
+
|
|
81
|
+
def altitude(p: xr.DataArray) -> xr.DataArray:
|
|
82
|
+
"""Barometric altitude. Assumes p is in Pa."""
|
|
83
|
+
return 44330.0 * (1.0 - (p / 101325.0) ** 0.1903)
|
|
84
|
+
|
|
85
|
+
p = xr.DataArray([1013.0, 1000.0], dims=["time"], attrs={"units": "hPa"})
|
|
86
|
+
altitude(p)
|
|
87
|
+
```
|
|
88
|
+
```
|
|
89
|
+
<xarray.DataArray (time: 2)> Size: 16B
|
|
90
|
+
array([25876.55998663, 25921.86206292])
|
|
91
|
+
Dimensions without coordinates: time
|
|
92
|
+
Attributes:
|
|
93
|
+
units: hPa
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
The input units are wrong (hPa vs Pa) but the onus is on the user to check, and in reality we all know that such bugs can go unnoticed for a terrifyingly long time.
|
|
97
|
+
The returned altitude is numerically wrong and carries a completely bogus hPa units in `attrs`.
|
|
98
|
+
|
|
99
|
+
Here we use `xarray-annotated` to declare expected properties in the signature and enforce them at run-time with minimal boilerplate (just decorators).
|
|
100
|
+
|
|
101
|
+
```python
|
|
102
|
+
from typing import Annotated
|
|
103
|
+
import xarray as xr
|
|
104
|
+
from xarray_annotated.schema import declare_schema, Dims
|
|
105
|
+
from xarray_annotated.units import declare_units, Unit
|
|
106
|
+
|
|
107
|
+
@declare_units
|
|
108
|
+
@declare_schema
|
|
109
|
+
def altitude(
|
|
110
|
+
p: Annotated[xr.DataArray, Dims("time"), Unit("Pa")],
|
|
111
|
+
) -> Annotated[xr.DataArray, Dims("time"), Unit("m")]:
|
|
112
|
+
"""Barometric altitude."""
|
|
113
|
+
return 44330.0 * (1.0 - (p / 101325.0) ** 0.1903)
|
|
114
|
+
|
|
115
|
+
p = xr.DataArray([1013.0, 1000.0], dims=["time"], attrs={"units": "hPa"})
|
|
116
|
+
altitude(p)
|
|
117
|
+
```
|
|
118
|
+
```
|
|
119
|
+
<xarray.DataArray (time: 2)> Size: 16B
|
|
120
|
+
array([ 2.08162886, 110.90398059])
|
|
121
|
+
Dimensions without coordinates: time
|
|
122
|
+
Attributes:
|
|
123
|
+
units: m
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
Now the outputs are physical and carry the correct units attribute.
|
|
127
|
+
The `hPa` input was converted to `Pa` before the body ran, and the result was stamped with the unit the signature promised.
|
|
128
|
+
The docstring no longer needs to say "assumes Pa", because the signature does --- and unlike the docstring, it is actually enforced.
|
|
129
|
+
|
|
130
|
+
If we had handed the function an array whose units were _dimensionally_ wrong, or with an extra dimension, this would have failed gracefully:
|
|
131
|
+
|
|
132
|
+
```python
|
|
133
|
+
altitude(xr.DataArray([1.0, 2.0], dims=["time"], attrs={"units": "kg"}))
|
|
134
|
+
# DimensionalityError: Cannot convert from 'kilogram' ([mass])
|
|
135
|
+
# to 'pascal' ([mass] / [length] / [time] ** 2)
|
|
136
|
+
|
|
137
|
+
altitude(xr.DataArray([[1013.0, 1000.0]], dims=["run", "time"], attrs={"units": "hPa"}))
|
|
138
|
+
# SchemaError: [altitude] 'p' dims mismatch: expected ('time',) in any order, got ('run', 'time')
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
## Contributing
|
|
142
|
+
|
|
143
|
+
```sh
|
|
144
|
+
uv sync # install the package + dev dependencies into .venv
|
|
145
|
+
just # lint, typecheck, test, build docs
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
See the `justfile` for individual targets (`just lint`, `just test`, `just test-cov`, ...).
|
|
149
|
+
|
|
150
|
+
A `.pre-commit-config.yaml` is included to run the same linting (ruff) and type-checking (pyright) steps on every commit. Install the hooks with:
|
|
151
|
+
|
|
152
|
+
```sh
|
|
153
|
+
uv run pre-commit install
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
Contributions are welcome --- open an issue or pull request on GitHub.
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
# xarray-annotated
|
|
2
|
+
|
|
3
|
+
[](https://pypi.org/project/xarray-annotated/)
|
|
4
|
+
[](https://pypi.org/project/xarray-annotated/)
|
|
5
|
+
[](https://github.com/jmarshrossney/xarray-annotated/actions/workflows/ci.yml)
|
|
6
|
+
[](https://jmarshrossney.github.io/xarray-annotated)
|
|
7
|
+
[](https://github.com/jmarshrossney/xarray-annotated/blob/main/LICENSE)
|
|
8
|
+
|
|
9
|
+
`xarray-annotated` enables run-time validation of `xarray.DataArray` properties declared in function signatures via [`typing.Annotated`](https://docs.python.org/3/library/typing.html#typing.Annotated).
|
|
10
|
+
|
|
11
|
+
The main idea is that annotations serve as a single source of truth for
|
|
12
|
+
|
|
13
|
+
- structural schema (dims, coords, dtype),
|
|
14
|
+
- physical units,
|
|
15
|
+
- frequency (and phase) of a time axis,
|
|
16
|
+
|
|
17
|
+
from which
|
|
18
|
+
|
|
19
|
+
- static checkers and run-time validation can derive expected properties,
|
|
20
|
+
- documentation can be generated automatically,
|
|
21
|
+
- automated tools (coding agents) receive steering.
|
|
22
|
+
|
|
23
|
+
For validating and automatically converting physical units we lean on the excellent [pint](https://pint.readthedocs.io/en/stable/) and [pint-xarray](https://pint-xarray.readthedocs.io).
|
|
24
|
+
([cf-xarray](https://cf-xarray.readthedocs.io) is an optional dependency for CF/UDUNITS unit strings.)
|
|
25
|
+
|
|
26
|
+
For full user documentation please visit **[https://jmarshrossney.github.io/xarray-annotated/](https://jmarshrossney.github.io/xarray-annotated/)**.
|
|
27
|
+
|
|
28
|
+
## Installation
|
|
29
|
+
|
|
30
|
+
Either using `uv` (recommended) or `pip`:
|
|
31
|
+
|
|
32
|
+
```sh
|
|
33
|
+
uv add xarray-annotated
|
|
34
|
+
# or
|
|
35
|
+
pip install xarray-annotated
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
CF-convention / UDUNITS unit strings (e.g. `"umol m-2 s-1"`) need the optional `cf` extra, which pulls in [cf-xarray](https://cf-xarray.readthedocs.io).
|
|
39
|
+
|
|
40
|
+
```sh
|
|
41
|
+
uv add "xarray-annotated[cf]"
|
|
42
|
+
# or
|
|
43
|
+
pip install "xarray-annotated[cf]"
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
## A short example
|
|
47
|
+
|
|
48
|
+
Here is a function that converts barometric pressure to altitude.
|
|
49
|
+
It assumes a unit (pascals) and says so in the docstring but doesn't check or enforce it.
|
|
50
|
+
|
|
51
|
+
```python
|
|
52
|
+
import xarray as xr
|
|
53
|
+
|
|
54
|
+
def altitude(p: xr.DataArray) -> xr.DataArray:
|
|
55
|
+
"""Barometric altitude. Assumes p is in Pa."""
|
|
56
|
+
return 44330.0 * (1.0 - (p / 101325.0) ** 0.1903)
|
|
57
|
+
|
|
58
|
+
p = xr.DataArray([1013.0, 1000.0], dims=["time"], attrs={"units": "hPa"})
|
|
59
|
+
altitude(p)
|
|
60
|
+
```
|
|
61
|
+
```
|
|
62
|
+
<xarray.DataArray (time: 2)> Size: 16B
|
|
63
|
+
array([25876.55998663, 25921.86206292])
|
|
64
|
+
Dimensions without coordinates: time
|
|
65
|
+
Attributes:
|
|
66
|
+
units: hPa
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
The input units are wrong (hPa vs Pa) but the onus is on the user to check, and in reality we all know that such bugs can go unnoticed for a terrifyingly long time.
|
|
70
|
+
The returned altitude is numerically wrong and carries a completely bogus hPa units in `attrs`.
|
|
71
|
+
|
|
72
|
+
Here we use `xarray-annotated` to declare expected properties in the signature and enforce them at run-time with minimal boilerplate (just decorators).
|
|
73
|
+
|
|
74
|
+
```python
|
|
75
|
+
from typing import Annotated
|
|
76
|
+
import xarray as xr
|
|
77
|
+
from xarray_annotated.schema import declare_schema, Dims
|
|
78
|
+
from xarray_annotated.units import declare_units, Unit
|
|
79
|
+
|
|
80
|
+
@declare_units
|
|
81
|
+
@declare_schema
|
|
82
|
+
def altitude(
|
|
83
|
+
p: Annotated[xr.DataArray, Dims("time"), Unit("Pa")],
|
|
84
|
+
) -> Annotated[xr.DataArray, Dims("time"), Unit("m")]:
|
|
85
|
+
"""Barometric altitude."""
|
|
86
|
+
return 44330.0 * (1.0 - (p / 101325.0) ** 0.1903)
|
|
87
|
+
|
|
88
|
+
p = xr.DataArray([1013.0, 1000.0], dims=["time"], attrs={"units": "hPa"})
|
|
89
|
+
altitude(p)
|
|
90
|
+
```
|
|
91
|
+
```
|
|
92
|
+
<xarray.DataArray (time: 2)> Size: 16B
|
|
93
|
+
array([ 2.08162886, 110.90398059])
|
|
94
|
+
Dimensions without coordinates: time
|
|
95
|
+
Attributes:
|
|
96
|
+
units: m
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
Now the outputs are physical and carry the correct units attribute.
|
|
100
|
+
The `hPa` input was converted to `Pa` before the body ran, and the result was stamped with the unit the signature promised.
|
|
101
|
+
The docstring no longer needs to say "assumes Pa", because the signature does --- and unlike the docstring, it is actually enforced.
|
|
102
|
+
|
|
103
|
+
If we had handed the function an array whose units were _dimensionally_ wrong, or with an extra dimension, this would have failed gracefully:
|
|
104
|
+
|
|
105
|
+
```python
|
|
106
|
+
altitude(xr.DataArray([1.0, 2.0], dims=["time"], attrs={"units": "kg"}))
|
|
107
|
+
# DimensionalityError: Cannot convert from 'kilogram' ([mass])
|
|
108
|
+
# to 'pascal' ([mass] / [length] / [time] ** 2)
|
|
109
|
+
|
|
110
|
+
altitude(xr.DataArray([[1013.0, 1000.0]], dims=["run", "time"], attrs={"units": "hPa"}))
|
|
111
|
+
# SchemaError: [altitude] 'p' dims mismatch: expected ('time',) in any order, got ('run', 'time')
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
## Contributing
|
|
115
|
+
|
|
116
|
+
```sh
|
|
117
|
+
uv sync # install the package + dev dependencies into .venv
|
|
118
|
+
just # lint, typecheck, test, build docs
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
See the `justfile` for individual targets (`just lint`, `just test`, `just test-cov`, ...).
|
|
122
|
+
|
|
123
|
+
A `.pre-commit-config.yaml` is included to run the same linting (ruff) and type-checking (pyright) steps on every commit. Install the hooks with:
|
|
124
|
+
|
|
125
|
+
```sh
|
|
126
|
+
uv run pre-commit install
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
Contributions are welcome --- open an issue or pull request on GitHub.
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["uv_build>=0.11.17,<0.12.0"]
|
|
3
|
+
build-backend = "uv_build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "xarray-annotated"
|
|
7
|
+
version = "0.5.2"
|
|
8
|
+
description = "Run-time validation of xarray DataArray properties (units, dims/coords/dtype) declared via typing.Annotated"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = "MIT"
|
|
11
|
+
license-files = ["LICENSE"]
|
|
12
|
+
requires-python = ">=3.12"
|
|
13
|
+
dependencies = [
|
|
14
|
+
"pandas>=2.2",
|
|
15
|
+
"pint>=0.25.3",
|
|
16
|
+
"pint-xarray>=0.6.1",
|
|
17
|
+
"xarray>=2026.2.0",
|
|
18
|
+
]
|
|
19
|
+
keywords = [
|
|
20
|
+
"xarray",
|
|
21
|
+
"pint",
|
|
22
|
+
"units",
|
|
23
|
+
"validation",
|
|
24
|
+
"annotations",
|
|
25
|
+
]
|
|
26
|
+
classifiers = [
|
|
27
|
+
"Development Status :: 3 - Alpha",
|
|
28
|
+
"Intended Audience :: Science/Research",
|
|
29
|
+
"Programming Language :: Python :: 3.12",
|
|
30
|
+
"Programming Language :: Python :: 3.13",
|
|
31
|
+
"Programming Language :: Python :: 3.14",
|
|
32
|
+
"Topic :: Scientific/Engineering",
|
|
33
|
+
]
|
|
34
|
+
|
|
35
|
+
[[project.authors]]
|
|
36
|
+
name = "jmarshrossney"
|
|
37
|
+
email = "17361029+jmarshrossney@users.noreply.github.com"
|
|
38
|
+
|
|
39
|
+
[project.optional-dependencies]
|
|
40
|
+
cf = ["cf-xarray>=0.11.3"]
|
|
41
|
+
|
|
42
|
+
[project.urls]
|
|
43
|
+
Homepage = "https://jmarshrossney.github.io/xarray-annotated"
|
|
44
|
+
Repository = "https://github.com/jmarshrossney/xarray-annotated"
|
|
45
|
+
Documentation = "https://jmarshrossney.github.io/xarray-annotated"
|
|
46
|
+
|
|
47
|
+
[dependency-groups]
|
|
48
|
+
dev = [
|
|
49
|
+
"xarray-annotated[cf]",
|
|
50
|
+
"cftime>=1.6",
|
|
51
|
+
"coverage>=7.13.5",
|
|
52
|
+
"pyright>=1.1.409",
|
|
53
|
+
"pytest>=9.0.2",
|
|
54
|
+
"pytest-cov>=7.1.0",
|
|
55
|
+
"ruff>=0.15.4",
|
|
56
|
+
"rust-just>=1.46.0",
|
|
57
|
+
"pre-commit>=4.6.0",
|
|
58
|
+
{ include-group = "docs" },
|
|
59
|
+
]
|
|
60
|
+
docs = [
|
|
61
|
+
"marimo>=0.23.11",
|
|
62
|
+
"marimo-md-export>=0.7.0",
|
|
63
|
+
"mkdocstrings-python>=2.0.5",
|
|
64
|
+
"zensical>=0.0.46",
|
|
65
|
+
]
|
|
66
|
+
|
|
67
|
+
[tool.pytest]
|
|
68
|
+
testpaths = ["tests"]
|
|
69
|
+
|
|
70
|
+
[tool.pyright]
|
|
71
|
+
venvPath = "."
|
|
72
|
+
venv = ".venv"
|
|
73
|
+
include = [
|
|
74
|
+
"src/",
|
|
75
|
+
"tests/",
|
|
76
|
+
]
|
|
77
|
+
|
|
78
|
+
[tool.ruff]
|
|
79
|
+
line-length = 88
|
|
80
|
+
indent-width = 4
|
|
81
|
+
target-version = "py312"
|
|
82
|
+
|
|
83
|
+
[tool.ruff.lint.per-file-ignores]
|
|
84
|
+
"examples/notebook.py" = [
|
|
85
|
+
"F821",
|
|
86
|
+
"F722",
|
|
87
|
+
]
|
|
88
|
+
|
|
89
|
+
[tool.uv]
|
|
90
|
+
exclude-newer = "1 week"
|
|
91
|
+
|
|
92
|
+
[tool.uv.exclude-newer-package]
|
|
93
|
+
marimo-md-export = false
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["uv_build>=0.11.17,<0.12.0"]
|
|
3
|
+
build-backend = "uv_build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "xarray-annotated"
|
|
7
|
+
version = "0.5.2"
|
|
8
|
+
description = "Run-time validation of xarray DataArray properties (units, dims/coords/dtype) declared via typing.Annotated"
|
|
9
|
+
authors = [
|
|
10
|
+
{ name = "jmarshrossney", email = "17361029+jmarshrossney@users.noreply.github.com" }
|
|
11
|
+
]
|
|
12
|
+
readme = "README.md"
|
|
13
|
+
license = "MIT"
|
|
14
|
+
license-files = ["LICENSE"]
|
|
15
|
+
requires-python = ">=3.12"
|
|
16
|
+
dependencies = [
|
|
17
|
+
"pandas>=2.2",
|
|
18
|
+
"pint>=0.25.3",
|
|
19
|
+
"pint-xarray>=0.6.1",
|
|
20
|
+
"xarray>=2026.2.0",
|
|
21
|
+
]
|
|
22
|
+
keywords = ["xarray", "pint", "units", "validation", "annotations"]
|
|
23
|
+
classifiers = [
|
|
24
|
+
"Development Status :: 3 - Alpha",
|
|
25
|
+
"Intended Audience :: Science/Research",
|
|
26
|
+
"Programming Language :: Python :: 3.12",
|
|
27
|
+
"Programming Language :: Python :: 3.13",
|
|
28
|
+
"Programming Language :: Python :: 3.14",
|
|
29
|
+
"Topic :: Scientific/Engineering",
|
|
30
|
+
]
|
|
31
|
+
|
|
32
|
+
[project.optional-dependencies]
|
|
33
|
+
cf = ["cf-xarray>=0.11.3"]
|
|
34
|
+
|
|
35
|
+
[project.urls]
|
|
36
|
+
Homepage = "https://jmarshrossney.github.io/xarray-annotated"
|
|
37
|
+
Repository = "https://github.com/jmarshrossney/xarray-annotated"
|
|
38
|
+
Documentation = "https://jmarshrossney.github.io/xarray-annotated"
|
|
39
|
+
|
|
40
|
+
[dependency-groups]
|
|
41
|
+
dev = [
|
|
42
|
+
"xarray-annotated[cf]",
|
|
43
|
+
"cftime>=1.6",
|
|
44
|
+
"coverage>=7.13.5",
|
|
45
|
+
"pyright>=1.1.409",
|
|
46
|
+
"pytest>=9.0.2",
|
|
47
|
+
"pytest-cov>=7.1.0",
|
|
48
|
+
"ruff>=0.15.4",
|
|
49
|
+
"rust-just>=1.46.0",
|
|
50
|
+
"pre-commit>=4.6.0",
|
|
51
|
+
{include-group = "docs"},
|
|
52
|
+
]
|
|
53
|
+
docs = [
|
|
54
|
+
"marimo>=0.23.11",
|
|
55
|
+
"marimo-md-export>=0.7.0",
|
|
56
|
+
"mkdocstrings-python>=2.0.5",
|
|
57
|
+
"zensical>=0.0.46",
|
|
58
|
+
]
|
|
59
|
+
|
|
60
|
+
[tool.pytest]
|
|
61
|
+
testpaths = ["tests"]
|
|
62
|
+
|
|
63
|
+
[tool.pyright]
|
|
64
|
+
venvPath = "."
|
|
65
|
+
venv = ".venv"
|
|
66
|
+
include = ["src/", "tests/"]
|
|
67
|
+
|
|
68
|
+
[tool.ruff]
|
|
69
|
+
line-length = 88
|
|
70
|
+
indent-width = 4
|
|
71
|
+
target-version = "py312"
|
|
72
|
+
|
|
73
|
+
[tool.ruff.lint.per-file-ignores]
|
|
74
|
+
# marimo threads `Annotated` in as a cell argument, so ruff can't resolve it to
|
|
75
|
+
# `typing.Annotated` and misreads the unit strings in the metadata as forward
|
|
76
|
+
# references (F821 "undefined name" / F722 "syntax error in forward annotation").
|
|
77
|
+
"examples/notebook.py" = ["F821", "F722"]
|
|
78
|
+
|
|
79
|
+
[tool.uv]
|
|
80
|
+
exclude-newer = "1 week"
|
|
81
|
+
exclude-newer-package = { marimo-md-export = false }
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"""xarray-annotated: validate xarray DataArray properties declared via ``typing.Annotated``.
|
|
2
|
+
|
|
3
|
+
Annotation is the unifying technology: a declared property is read off an
|
|
4
|
+
``Annotated[DataArray, ...]`` hint and validated at runtime. Each kind of
|
|
5
|
+
property is a domain subpackage, imported explicitly:
|
|
6
|
+
|
|
7
|
+
* ``xarray_annotated.units`` — physical units (pint / CF), the mature domain.
|
|
8
|
+
* ``xarray_annotated.schema`` — structural properties (dims, coords, dtype);
|
|
9
|
+
validate-only (never mutates).
|
|
10
|
+
* ``xarray_annotated.temporal`` — the frequency (and phase) of a time axis;
|
|
11
|
+
validate-only.
|
|
12
|
+
|
|
13
|
+
The top level is deliberately thin — nothing domain-specific is re-exported
|
|
14
|
+
here, so the domains never collide in a shared namespace::
|
|
15
|
+
|
|
16
|
+
from xarray_annotated import schema, temporal, units
|
|
17
|
+
from xarray_annotated.units import declare_units, check_units
|
|
18
|
+
from xarray_annotated.schema import declare_schema, Dims, Dtype
|
|
19
|
+
from xarray_annotated.temporal import declare_freq, Freq
|
|
20
|
+
|
|
21
|
+
The only names surfaced at the top level are domain-*agnostic* helpers that
|
|
22
|
+
belong to no single domain: the shared declaration *writer* ``annotate`` and its
|
|
23
|
+
inverse *reader* ``declarations_from_signature`` (which reads every facet declared
|
|
24
|
+
on a signature into one uniform ``Declared`` value), plus the ``Annotated``
|
|
25
|
+
introspection kernel (``unwrap_annotated``, ``walk_signature``).
|
|
26
|
+
``walk_signature`` is the shared driver behind every domain's reader, so a
|
|
27
|
+
third-party facet author can use it to build their own ``*_from_signature``
|
|
28
|
+
reader. No domain-specific name is re-exported here.
|
|
29
|
+
"""
|
|
30
|
+
|
|
31
|
+
from importlib.metadata import version as _version
|
|
32
|
+
|
|
33
|
+
from . import schema, temporal, units
|
|
34
|
+
from ._annotations import unwrap_annotated, walk_signature
|
|
35
|
+
from ._reader import Declared, declarations_from_signature
|
|
36
|
+
from ._writer import annotate
|
|
37
|
+
|
|
38
|
+
__version__ = _version("xarray-annotated")
|
|
39
|
+
|
|
40
|
+
__all__ = [
|
|
41
|
+
"Declared",
|
|
42
|
+
"__version__",
|
|
43
|
+
"annotate",
|
|
44
|
+
"declarations_from_signature",
|
|
45
|
+
"schema",
|
|
46
|
+
"temporal",
|
|
47
|
+
"units",
|
|
48
|
+
"unwrap_annotated",
|
|
49
|
+
"walk_signature",
|
|
50
|
+
]
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
"""Shared `typing.Annotated` introspection for xarray-annotated.
|
|
2
|
+
|
|
3
|
+
Pure `typing` machinery with no dependency on any domain's registry or config —
|
|
4
|
+
the "declaration side" common to every domain. A domain supplies an *extractor*
|
|
5
|
+
that turns one `Annotated[...]` hint into its own declaration payload (a unit
|
|
6
|
+
string, a list of schema markers, …); `walk_signature` drives that extractor over
|
|
7
|
+
a whole function signature, handling the `TypedDict` / dataclass / single-value
|
|
8
|
+
return shapes once, for everyone.
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
import dataclasses
|
|
12
|
+
import types
|
|
13
|
+
from collections.abc import Callable
|
|
14
|
+
from typing import (
|
|
15
|
+
Annotated,
|
|
16
|
+
Any,
|
|
17
|
+
TypeVar,
|
|
18
|
+
Union,
|
|
19
|
+
get_args,
|
|
20
|
+
get_origin,
|
|
21
|
+
get_type_hints,
|
|
22
|
+
is_typeddict,
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
import xarray as xr
|
|
26
|
+
|
|
27
|
+
_T = TypeVar("_T")
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def unwrap_annotated(hint: Any) -> Any:
|
|
31
|
+
"""Return the underlying type of an `Annotated` hint, else the hint itself.
|
|
32
|
+
|
|
33
|
+
`Annotated[DataArray, ...]` → `DataArray`; a non-`Annotated` hint is returned
|
|
34
|
+
unchanged. Lets type comparisons (e.g. `t is DataArray`) see through the
|
|
35
|
+
metadata that declarations attach to parameters.
|
|
36
|
+
|
|
37
|
+
Args:
|
|
38
|
+
hint: A type hint, possibly `Annotated`.
|
|
39
|
+
|
|
40
|
+
Returns:
|
|
41
|
+
The base type if `hint` is `Annotated`; otherwise `hint` unchanged.
|
|
42
|
+
"""
|
|
43
|
+
return get_args(hint)[0] if get_origin(hint) is Annotated else hint
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def _is_dataarray_type(tp: Any) -> bool:
|
|
47
|
+
"""Return whether `tp` is `DataArray` (possibly wrapped in a Union).
|
|
48
|
+
|
|
49
|
+
Accepts a bare `DataArray` as well as `DataArray | None` /
|
|
50
|
+
`Optional[DataArray]` (and any other union that includes `DataArray`), so an
|
|
51
|
+
optional DataArray parameter can still carry a declaration. Anything else
|
|
52
|
+
(scalars, `str`, `bool`, `Dataset`, …) is not a declaration-bearing type.
|
|
53
|
+
|
|
54
|
+
Args:
|
|
55
|
+
tp: A type annotation to inspect.
|
|
56
|
+
|
|
57
|
+
Returns:
|
|
58
|
+
`True` if `tp` is or contains `DataArray`.
|
|
59
|
+
"""
|
|
60
|
+
if tp is xr.DataArray:
|
|
61
|
+
return True
|
|
62
|
+
if get_origin(tp) in (Union, types.UnionType):
|
|
63
|
+
return any(_is_dataarray_type(arg) for arg in get_args(tp))
|
|
64
|
+
return False
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def walk_signature(
|
|
68
|
+
func: object,
|
|
69
|
+
extract: Callable[[Any], _T | None],
|
|
70
|
+
) -> tuple[dict[str, _T], dict[str, _T] | _T | None]:
|
|
71
|
+
"""Extract per-parameter and return declarations from a function's hints.
|
|
72
|
+
|
|
73
|
+
Reads `get_type_hints(func, include_extras=True)` and maps each hint through
|
|
74
|
+
`extract`, which returns a domain's declaration payload for that hint or
|
|
75
|
+
`None` when the hint carries no declaration. The `is not None` filter is the
|
|
76
|
+
single "no declaration" rule shared by every domain.
|
|
77
|
+
|
|
78
|
+
Args:
|
|
79
|
+
func: A callable whose parameters/return may carry `Annotated` metadata.
|
|
80
|
+
extract: Turns one hint into a declaration payload, or `None`. For units
|
|
81
|
+
this is `annotated_unit` (payload `str`); for schema `annotated_schema`
|
|
82
|
+
(payload `list[marker]`).
|
|
83
|
+
|
|
84
|
+
Returns:
|
|
85
|
+
An `(inputs, output)` pair:
|
|
86
|
+
|
|
87
|
+
* `inputs` — `dict[str, T]` mapping each declared parameter to its payload.
|
|
88
|
+
* `output` — a `dict[str, T]` (per-field) if the return hint is a
|
|
89
|
+
`TypedDict` or dataclass; a single `T` if the return is one declared
|
|
90
|
+
`Annotated[DataArray, ...]`; `None` otherwise.
|
|
91
|
+
"""
|
|
92
|
+
hints = get_type_hints(func, include_extras=True)
|
|
93
|
+
ret = hints.pop("return", None)
|
|
94
|
+
|
|
95
|
+
inputs = {
|
|
96
|
+
name: payload
|
|
97
|
+
for name, hint in hints.items()
|
|
98
|
+
if (payload := extract(hint)) is not None
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
output: dict[str, _T] | _T | None
|
|
102
|
+
if is_typeddict(ret):
|
|
103
|
+
ret_hints = get_type_hints(ret, include_extras=True)
|
|
104
|
+
output = {
|
|
105
|
+
name: payload
|
|
106
|
+
for name, hint in ret_hints.items()
|
|
107
|
+
if (payload := extract(hint)) is not None
|
|
108
|
+
}
|
|
109
|
+
elif dataclasses.is_dataclass(ret):
|
|
110
|
+
ret_hints = get_type_hints(ret, include_extras=True)
|
|
111
|
+
output = {
|
|
112
|
+
f.name: payload
|
|
113
|
+
for f in dataclasses.fields(ret)
|
|
114
|
+
if (payload := extract(ret_hints.get(f.name))) is not None
|
|
115
|
+
}
|
|
116
|
+
else:
|
|
117
|
+
output = extract(ret)
|
|
118
|
+
|
|
119
|
+
return inputs, output
|