Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update static type check #153

Merged
merged 5 commits into from
Apr 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
"settings": {
"files.insertFinalNewline": true,
"files.trimTrailingWhitespace": true,
"python.analysis.typeCheckingMode": "strict",
"python.formatting.provider": "black",
"python.languageServer": "Pylance",
"[python]": {
Expand Down
8 changes: 3 additions & 5 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,20 @@ jobs:
env:
POETRY_VIRTUALENVS_CREATE: false
strategy:
fail-fast: false
matrix:
python: ["3.7", "3.8", "3.9", "3.10"]
steps:
- uses: actions/checkout@v2
- uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python }}
- uses: actions/setup-node@v2
with:
node-version: "lts/gallium"
- name: Install project dependencies
run: pip install poetry && poetry install && npm install
run: pip install poetry && poetry install
- name: Test code's formatting (Black)
run: black --check docs tests xarray_dataclasses
- name: Test code's typing (Pyright)
run: npx pyright docs tests xarray_dataclasses
run: pyright docs tests xarray_dataclasses
- name: Test code's execution (pytest)
run: pytest -v tests
- name: Test docs' building (Sphinx)
Expand Down
33 changes: 0 additions & 33 deletions package-lock.json

This file was deleted.

5 changes: 0 additions & 5 deletions package.json

This file was deleted.

127 changes: 94 additions & 33 deletions poetry.lock

Large diffs are not rendered by default.

13 changes: 12 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ documentation = "https://astropenguin.github.io/xarray-dataclasses/"
python = ">=3.7.1, <3.11"
morecopy = "^0.2"
more-itertools = "^8.12"
numpy = "^1.15"
numpy = [
{ version = ">=1.15, <1.22", python = ">=3.7.1, <3.8" },
{ version = "^1.15", python = ">=3.8, <3.11" },
]
typing-extensions = "^3.10"
xarray = [
{ version = ">=0.18, <0.21", python = ">=3.7.1, <3.8" },
Expand All @@ -28,9 +31,17 @@ ipython = [
]
myst-parser = "^0.17"
pydata-sphinx-theme = "^0.8"
pyright = "^1.1"
pytest = "^7.1"
sphinx = "^4.5"

[tool.pyright]
reportImportCycles = "warning"
reportUnknownArgumentType = "warning"
reportUnknownMemberType = "warning"
reportUnknownVariableType = "warning"
typeCheckingMode = "strict"

[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"
8 changes: 0 additions & 8 deletions pyrightconfig.json

This file was deleted.

12 changes: 0 additions & 12 deletions xarray_dataclasses/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,6 @@
__version__ = "1.1.0"


# for Python 3.7 and 3.8
def _make_field_generic():
from dataclasses import Field
from typing import Sequence

GenericAlias = type(Sequence[int])
Field.__class_getitem__ = classmethod(GenericAlias)


_make_field_generic()


# submodules
from . import dataarray
from . import dataset
Expand Down
8 changes: 4 additions & 4 deletions xarray_dataclasses/dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
# submodules
from .datamodel import DataModel
from .dataoptions import DataOptions
from .typing import DataClass, DataClassFields, DataType, Order, Shape, Sizes
from .typing import AnyArray, DataClass, DataClassFields, DataType, Order, Shape, Sizes


# type hints
Expand Down Expand Up @@ -168,7 +168,7 @@ def new(cls: Any, *args: Any, **kwargs: Any) -> Any:
@classmethod
def shaped(
cls: Type[OptionedClass[PInit, TDataArray]],
func: Callable[[Shape], np.ndarray],
func: Callable[[Shape], AnyArray],
shape: Union[Shape, Sizes],
**kwargs: Any,
) -> TDataArray:
Expand All @@ -178,7 +178,7 @@ def shaped(
@classmethod
def shaped(
cls: Type[DataClass[PInit]],
func: Callable[[Shape], np.ndarray],
func: Callable[[Shape], AnyArray],
shape: Union[Shape, Sizes],
**kwargs: Any,
) -> xr.DataArray:
Expand All @@ -187,7 +187,7 @@ def shaped(
@classmethod
def shaped(
cls: Any,
func: Callable[[Shape], np.ndarray],
func: Callable[[Shape], AnyArray],
shape: Union[Shape, Sizes],
**kwargs: Any,
) -> Any:
Expand Down
5 changes: 3 additions & 2 deletions xarray_dataclasses/datamodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@


# standard library
from dataclasses import Field, dataclass, field, is_dataclass
from dataclasses import dataclass, field, is_dataclass
from typing import Any, Dict, Hashable, List, Optional, Tuple, Type, Union, cast


Expand All @@ -15,6 +15,7 @@

# submodules
from .typing import (
AnyField,
DataClass,
DataType,
Dims,
Expand Down Expand Up @@ -204,7 +205,7 @@ def eval_dataclass(dataclass: AnyDataClass[PInit]) -> None:
field.type = types[field.name]


def get_entry(field: Field[Any], value: Any) -> AnyEntry:
def get_entry(field: AnyField, value: Any) -> AnyEntry:
"""Create an entry from a field and its value."""
field_type = get_field_type(field.type)
repr_type = get_repr_type(field.type)
Expand Down
10 changes: 5 additions & 5 deletions xarray_dataclasses/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
# submodules
from .datamodel import DataModel
from .dataoptions import DataOptions
from .typing import DataClass, DataClassFields, DataType, Order, Shape, Sizes
from .typing import AnyArray, DataClass, DataClassFields, DataType, Order, Shape, Sizes


# type hints
Expand Down Expand Up @@ -159,7 +159,7 @@ def new(cls: Any, *args: Any, **kwargs: Any) -> Any:
@classmethod
def shaped(
cls: Type[OptionedClass[PInit, TDataset]],
func: Callable[[Shape], np.ndarray],
func: Callable[[Shape], AnyArray],
sizes: Sizes,
**kwargs: Any,
) -> TDataset:
Expand All @@ -169,7 +169,7 @@ def shaped(
@classmethod
def shaped(
cls: Type[DataClass[PInit]],
func: Callable[[Shape], np.ndarray],
func: Callable[[Shape], AnyArray],
sizes: Sizes,
**kwargs: Any,
) -> xr.Dataset:
Expand All @@ -178,7 +178,7 @@ def shaped(
@classmethod
def shaped(
cls: Any,
func: Callable[[Shape], np.ndarray],
func: Callable[[Shape], AnyArray],
sizes: Sizes,
**kwargs: Any,
) -> Any:
Expand All @@ -194,7 +194,7 @@ def shaped(

"""
model = DataModel.from_dataclass(cls)
data_vars: Dict[str, np.ndarray] = {}
data_vars: Dict[str, AnyArray] = {}

for key, entry in model.data_vars_items:
shape = tuple(sizes[dim] for dim in entry.dims)
Expand Down
6 changes: 5 additions & 1 deletion xarray_dataclasses/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,15 @@


# dependencies
import numpy as np
import xarray as xr
from more_itertools import collapse
from typing_extensions import (
Annotated,
Literal,
ParamSpec,
Protocol,
TypeAlias,
get_args,
get_origin,
get_type_hints,
Expand All @@ -56,7 +58,9 @@
TDtype = TypeVar("TDtype", covariant=True)
TName = TypeVar("TName", bound=Hashable)

DataClassFields = Dict[str, Field[Any]]
AnyArray: TypeAlias = "np.ndarray[Any, Any]"
AnyField: TypeAlias = "Field[Any]"
DataClassFields = Dict[str, AnyField]
DataType = Union[xr.DataArray, xr.Dataset]
Dims = Tuple[str, ...]
Dtype = Optional[str]
Expand Down