Skip to content

Commit bd71550

Browse files
authored
Merge pull request #10 from xarray-contrib/add_precommit
Add precommit
2 parents d2d4b6c + b9d716a commit bd71550

File tree

13 files changed

+1024
-963
lines changed

13 files changed

+1024
-963
lines changed

.github/workflows/tests.yml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,6 @@ jobs:
2323
python-version: ${{ matrix.python }}
2424
- name: Install project dependencies
2525
run: pip install -e ".[dev]"
26-
- name: Test code's formatting (Black)
27-
run: black --check docs tests src/xarray_dataclasses
28-
- name: Test code's typing (Pyright)
29-
run: pyright docs tests src/xarray_dataclasses
3026
- name: Test code's execution (pytest)
3127
run: pytest -v tests
3228
- name: Test docs' building (Sphinx)

.pre-commit-config.yaml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# See https://pre-commit.com for more information
2+
# See https://pre-commit.com/hooks.html for more hooks
3+
repos:
4+
- repo: https://github.com/pre-commit/pre-commit-hooks
5+
rev: v5.0.0
6+
hooks:
7+
- id: trailing-whitespace
8+
- id: end-of-file-fixer
9+
- id: check-added-large-files
10+
- repo: https://github.com/astral-sh/ruff-pre-commit
11+
rev: v0.12.5
12+
hooks:
13+
- id: ruff-check
14+
args: [--fix] # optional, to autofix lint errors
15+
- id: ruff-format
16+
- repo: https://github.com/fsouza/mirrors-pyright
17+
rev: v1.1.403
18+
hooks:
19+
- id: pyright
20+
- repo: https://github.com/abravalheri/validate-pyproject
21+
rev: v0.24.1
22+
hooks:
23+
- id: validate-pyproject
24+
name: "python · Validate pyproject.toml"
25+
additional_dependencies: ["validate-pyproject-schema-store[all]"]

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Important Note
2-
This repository was a fork from [here](https://github.com/astropenguin/xarray-dataclasses). We are grateful for the
3-
work of the developer on this repo. That being said, sadly the state on main has been the case that the code was
4-
deleted and there has been no development for a while. Therefore, we intially decided to fork the repository and
2+
This repository was a fork from [here](https://github.com/astropenguin/xarray-dataclasses). We are grateful for the
3+
work of the developer on this repo. That being said, sadly the state on main has been the case that the code was
4+
deleted and there has been no development for a while. Therefore, we intially decided to fork the repository and
55
continue development here, where the community is better able to contribute to and maintain the project. We now changed
66
it into a standalone repository.
77

@@ -392,7 +392,7 @@ pixi run pyright
392392
```
393393

394394
### Creating documentation
395-
We also have a [documentation workflow] (Add link). However, if you want to locally create the documentation
395+
We also have a [documentation workflow] (Add link). However, if you want to locally create the documentation
396396
run the following:
397397

398398
```shell

docs/_static/logo-dark.svg

Lines changed: 1 addition & 1 deletion
Loading

docs/_static/logo-light.svg

Lines changed: 1 addition & 1 deletion
Loading

pixi.lock

Lines changed: 934 additions & 892 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,14 @@ Documentation = "https://xarray-contrib.github.io/xarray-dataclasses/"
3838

3939
[project.optional-dependencies]
4040
dev = [
41-
"black>=24.4",
4241
"build>=1.0.0",
43-
"flake8>=7.0.0",
4442
"ipython>=8.12",
4543
"myst-parser>=3.0",
4644
"pydata-sphinx-theme>=0.14",
47-
"pyright>=1.1",
4845
"pytest>=8.2",
4946
"sphinx>=7.1",
5047
"twine>=4.0.0",
48+
"pre-commit>=4.2.0,<5"
5149
]
5250

5351
[tool.hatch.build]
@@ -72,16 +70,16 @@ dev = { features = ["dev"], solve-group = "default" }
7270

7371
[tool.pixi.feature.dev.tasks]
7472
tests = "pytest"
75-
flake8 = "flake8 docs tests src/xarray_dataclasses"
76-
black = "black docs tests src/xarray_dataclasses"
7773
doc_build = { cmd = "sphinx-apidoc -efT -o docs/_apidoc src/xarray_dataclasses && sphinx-build -a docs docs/_build" }
78-
pyright = "pyright docs tests src/xarray_dataclasses"
7974

8075
[tool.pyright]
81-
reportImportCycles = "warning"
82-
reportUnknownArgumentType = "warning"
83-
reportUnknownMemberType = "warning"
84-
reportUnknownVariableType = "warning"
76+
reportMissingImports = "none"
77+
reportImportCycles = "none"
78+
reportUnknownArgumentType = "none"
79+
reportUnknownMemberType = "none"
80+
reportUnknownVariableType = "none"
81+
reportUnknownParameterType = "none"
82+
reportUntypedFunctionDecorator = "none"
8583
typeCheckingMode = "strict"
8684

8785
[tool.pixi.pypi-dependencies]

src/xarray_dataclasses/__init__.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,15 @@
2727
from . import dataoptions
2828
from . import typing
2929
from .__about__ import __version__
30-
from .dataarray import *
31-
from .dataset import *
32-
from .datamodel import *
33-
from .dataoptions import *
34-
from .typing import *
30+
from .dataarray import AsDataArray, asdataarray
31+
from .dataset import AsDataset, asdataset
32+
from .datamodel import DataModel
33+
from .dataoptions import DataOptions
34+
from .typing import (
35+
Attr,
36+
Coord,
37+
Coordof,
38+
Data,
39+
Dataof,
40+
Name,
41+
)

src/xarray_dataclasses/datamodel.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from typing import (
99
Any,
1010
Dict,
11+
get_type_hints,
1112
Hashable,
1213
List,
1314
Literal,
@@ -22,7 +23,7 @@
2223
# dependencies
2324
import numpy as np
2425
import xarray as xr
25-
from typing_extensions import ParamSpec, get_type_hints
26+
from typing_extensions import ParamSpec
2627

2728

2829
# submodules
@@ -133,7 +134,7 @@ def __post_init__(self) -> None:
133134
if model.names:
134135
setattr(self, "name", model.names[0].value)
135136

136-
def __call__(self, reference: Optional[AnyXarray] = None) -> xr.DataArray:
137+
def __call__(self, reference: Optional[AnyXarray] = None) -> xr.DataArray: # pyright: ignore[reportUnknownParameterType]
137138
"""Create a DataArray object according to the entry."""
138139
from .dataarray import asdataarray
139140

@@ -187,12 +188,12 @@ def from_dataclass(cls, dataclass: AnyDataClass[PInit]) -> "DataModel":
187188
model = cls()
188189
eval_dataclass(dataclass)
189190

190-
for field in dataclass.__dataclass_fields__.values():
191-
value = getattr(dataclass, field.name, MISSING)
192-
entry = get_entry(field, value)
191+
for field_value in dataclass.__dataclass_fields__.values():
192+
value = getattr(dataclass, field_value.name, MISSING)
193+
entry = get_entry(field_value, value)
193194

194195
if entry is not None:
195-
model.entries[field.name] = entry
196+
model.entries[field_value.name] = entry
196197

197198
return model
198199

@@ -203,10 +204,10 @@ def eval_dataclass(dataclass: AnyDataClass[PInit]) -> None:
203204
if not is_dataclass(dataclass):
204205
raise TypeError("Not a dataclass or its object.")
205206

206-
fields = dataclass.__dataclass_fields__.values()
207+
field_values = dataclass.__dataclass_fields__.values()
207208

208209
# do nothing if field types are already evaluated
209-
if not any(isinstance(field.type, str) for field in fields):
210+
if not any(isinstance(field_value.type, str) for field_value in field_values):
210211
return
211212

212213
# otherwise, replace field types with evaluated types
@@ -215,8 +216,8 @@ def eval_dataclass(dataclass: AnyDataClass[PInit]) -> None:
215216

216217
types = get_type_hints(dataclass, include_extras=True)
217218

218-
for field in fields:
219-
field.type = types[field.name]
219+
for field_value in field_values:
220+
field_value.type = types[field_value.name]
220221

221222

222223
def get_entry(field: AnyField, value: Any) -> Optional[AnyEntry]:
@@ -250,11 +251,11 @@ def get_entry(field: AnyField, value: Any) -> Optional[AnyEntry]:
250251
)
251252

252253

253-
def get_typedarray(
254+
def get_typedarray( # pyright: ignore[reportUnknownParameterType]
254255
data: Any,
255256
dims: Dims,
256-
dtype: Optional[AnyDType],
257-
reference: Optional[AnyXarray] = None,
257+
dtype: Optional[AnyDType], # pyright: ignore[reportUnknownParameterType]
258+
reference: Optional[AnyXarray] = None, # pyright: ignore[reportUnknownParameterType]
258259
) -> xr.DataArray:
259260
"""Create a DataArray object with given dims and dtype.
260261

src/xarray_dataclasses/typing.py

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,15 @@
2323
from enum import Enum
2424
from itertools import chain
2525
from typing import (
26+
Annotated,
2627
Any,
2728
ClassVar,
2829
Collection,
2930
Dict,
3031
Generic,
32+
get_args,
33+
get_origin,
34+
get_type_hints,
3135
Hashable,
3236
Iterable,
3337
Literal,
@@ -44,14 +48,7 @@
4448
# dependencies
4549
import numpy as np
4650
import xarray as xr
47-
from typing_extensions import (
48-
Annotated,
49-
ParamSpec,
50-
TypeAlias,
51-
get_args,
52-
get_origin,
53-
get_type_hints,
54-
)
51+
from typing_extensions import ParamSpec, TypeAlias
5552

5653

5754
# type hints (private)
@@ -62,10 +59,10 @@
6259
TDType = TypeVar("TDType", covariant=True)
6360
THashable = TypeVar("THashable", bound=Hashable)
6461

65-
AnyArray: TypeAlias = "np.ndarray[Any, Any]"
66-
AnyDType: TypeAlias = "np.dtype[Any]"
67-
AnyField: TypeAlias = "Field[Any]"
68-
AnyXarray: TypeAlias = "xr.DataArray | xr.Dataset"
62+
AnyArray: TypeAlias = np.ndarray[Any, Any]
63+
AnyDType: TypeAlias = np.dtype[Any]
64+
AnyField: TypeAlias = Field[Any]
65+
AnyXarray: TypeAlias = Union[xr.DataArray, xr.Dataset]
6966
Dims = Tuple[str, ...]
7067
Order = Literal["C", "F"]
7168
Shape = Union[Sequence[int], int]
@@ -325,7 +322,7 @@ def get_dims(tp: Any) -> Dims:
325322
return tuple(str(get_args(arg)[0]) for arg in args)
326323

327324

328-
def get_dtype(tp: Any) -> Optional[AnyDType]:
325+
def get_dtype(tp: Any) -> Optional[AnyDType]: # pyright: ignore[reportUnknownParameterType]
329326
"""Extract a NumPy data type (dtype)."""
330327
try:
331328
dtype = get_args(get_args(get_annotated(tp))[1])[0]

0 commit comments

Comments
 (0)