-
Notifications
You must be signed in to change notification settings - Fork 129
Enforce strict mypy type checking and additional ruff rules #492
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
Open
FabianHofmann
wants to merge
29
commits into
master
Choose a base branch
from
refac/type-annotation
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
bf12877
Add aggregate_time support to convert_and_aggregate
FabianHofmann 5543e1a
add missing docstrings
FabianHofmann e0c511c
Add mypy type checking with strict settings and fix all type errors a…
FabianHofmann 9228ca7
Enable ruff rules B, SIM, RET, C4, TC, NPY, G, PTH, RUF100 and fix al…
FabianHofmann 820e7e2
Include test directory in mypy checking and fix all test type errors
FabianHofmann b1edf3f
Remove examples/ from mypy exclude (no .py files to check, notebooks …
FabianHofmann c5a7a8d
Include doc/ in mypy checking and fix type errors in conf.py and char…
FabianHofmann 8153e27
Bump mypy python_version to 3.11, enabling proper xarray Self types a…
FabianHofmann 16cd01c
Fix mypy errors: remove unused type: ignore comments, fix type annota…
FabianHofmann 6591a14
Enable ruff DOC/D417 rules for docstring-signature alignment and fix …
FabianHofmann 80d6cdb
Address PR review: add 'legacy' default, drop False, extract _aggrega…
FabianHofmann 5a2fba9
update release notes
FabianHofmann 7fe656b
Merge branch 'feat/aggregate_time' into refac/type-annotation
FabianHofmann 0f1d464
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] dfdcc97
Add docstrings to era5/convert modules and replace str with Literal t…
FabianHofmann fd68cd3
Enforce D103 (missing docstring in public function) via ruff
FabianHofmann 2d0497c
Add docstrings to all dataset modules (sarah, ncep, cordex, gebco) an…
FabianHofmann 2486e59
Merge branch 'master' into refac/type-annotation
FabianHofmann 67f7366
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] aef7b64
Ignore D103 in test/ and examples/, fix duplicate _aggregate_time def…
FabianHofmann 182b93e
Remove commented-out D103 ignore line
FabianHofmann 0c807ce
Fix docstring lint errors: D205, D401, D100, D101, D105, DOC201
FabianHofmann 80f744a
Merge branch 'master' into refac/type-annotation
FabianHofmann 55d5345
Merge branch 'master' into refac/type-annotation
FabianHofmann 54601f8
address review comments
FabianHofmann ab6ea3a
Fail loudly in cordex/ncep when path env var is missing
FabianHofmann 161c81d
Restore non-obvious comments, citations, and docstring context
FabianHofmann 4f57261
Fix mypy errors in convert, cutout, era5 and orientation
FabianHofmann 42a9f68
Fix remaining mypy errors across atlite modules
FabianHofmann File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| # SPDX-FileCopyrightText: Contributors to atlite <https://github.com/pypsa/atlite> | ||
| # | ||
| # SPDX-License-Identifier: MIT | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from pathlib import Path | ||
| from typing import Any, Literal, TypeAlias, TypedDict | ||
|
|
||
| import numpy as np | ||
| import xarray as xr | ||
| from pyproj import CRS | ||
|
|
||
| NDArray: TypeAlias = np.ndarray[Any, np.dtype[np.floating[Any]]] | ||
| NDArrayInt: TypeAlias = np.ndarray[Any, np.dtype[np.signedinteger[Any]]] | ||
| NDArrayBool: TypeAlias = np.ndarray[Any, np.dtype[np.bool_]] | ||
| PathLike: TypeAlias = str | Path | ||
| NumericArray: TypeAlias = NDArray | xr.DataArray | ||
| Number: TypeAlias = int | float | np.number[Any] | ||
| CrsLike: TypeAlias = str | int | CRS | dict[str, Any] | ||
| ConvertResult: TypeAlias = ( | ||
| xr.DataArray | xr.Dataset | tuple[xr.DataArray | xr.Dataset, xr.DataArray] | ||
| ) | ||
|
|
||
| TrackingType: TypeAlias = Literal["horizontal", "tilted_horizontal", "vertical", "dual"] | ||
| ClearskyModel: TypeAlias = Literal["simple", "enhanced"] | ||
| TrigonModel: TypeAlias = Literal["simple", "perez"] | ||
| IrradiationType: TypeAlias = Literal["total", "direct", "diffuse", "ground"] | ||
| HeatPumpSource: TypeAlias = Literal["air", "soil"] | ||
| OrientationName: TypeAlias = Literal["latitude_optimal", "constant", "latitude"] | ||
| DataFormat: TypeAlias = Literal["grib", "netcdf"] | ||
|
|
||
|
|
||
| class ERA5RetrievalParams(TypedDict, total=False): | ||
| product: str | ||
| area: list[float] | ||
| grid: str | ||
| chunks: dict[str, int] | None | ||
| tmpdir: str | Path | None | ||
| lock: Any | None | ||
| data_format: Literal["grib", "netcdf"] | ||
| year: list[str] | ||
| month: list[str] | str | ||
| day: list[str] | str | ||
| time: str | list[str] | ||
| variable: str | list[str] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,29 +1,56 @@ | ||
| # SPDX-FileCopyrightText: Contributors to atlite <https://github.com/pypsa/atlite> | ||
| # | ||
| # SPDX-License-Identifier: MIT | ||
| """ | ||
| Functions for aggregating results. | ||
| """ | ||
| """Functions for aggregating results.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing import TYPE_CHECKING, cast | ||
|
|
||
| import dask | ||
| import xarray as xr | ||
|
|
||
| if TYPE_CHECKING: | ||
| import pandas as pd | ||
| from scipy.sparse import spmatrix | ||
|
|
||
|
|
||
| def aggregate_matrix( | ||
| da: xr.DataArray, | ||
| matrix: spmatrix, | ||
| index: pd.Index, | ||
| ) -> xr.DataArray: | ||
| """ | ||
| Aggregate spatial data with a sparse matrix. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| da : xarray.DataArray | ||
| DataArray with spatial dimensions ``y`` and ``x``. | ||
| matrix : scipy.sparse.spmatrix | ||
| Aggregation matrix mapping flattened spatial cells to ``index``. | ||
| index : pandas.Index | ||
| Index defining the aggregated dimension. | ||
|
|
||
| def aggregate_matrix(da, matrix, index): | ||
| Returns | ||
| ------- | ||
| xarray.DataArray | ||
| Aggregated data indexed by ``index`` and, if present, time. | ||
| """ | ||
| if index.name is None: | ||
| index = index.rename("dim_0") | ||
| if isinstance(da.data, dask.array.core.Array): | ||
| da = da.stack(spatial=("y", "x")) | ||
| da = da.chunk(dict(spatial=-1)) | ||
| return xr.apply_ufunc( | ||
| da = da.chunk({"spatial": -1}) | ||
| result = xr.apply_ufunc( | ||
| lambda da: da * matrix.T, | ||
| da, | ||
| input_core_dims=[["spatial"]], | ||
| output_core_dims=[[index.name]], | ||
| dask="parallelized", | ||
| output_dtypes=[da.dtype], | ||
| dask_gufunc_kwargs=dict(output_sizes={index.name: index.size}), | ||
| dask_gufunc_kwargs={"output_sizes": {index.name: index.size}}, | ||
| ).assign_coords(**{index.name: index}) | ||
| else: | ||
| da = da.stack(spatial=("y", "x")).transpose("spatial", "time") | ||
| return xr.DataArray(matrix * da, [index, da.coords["time"]]) | ||
| return cast("xr.DataArray", result) | ||
| da = da.stack(spatial=("y", "x")).transpose("spatial", "time") | ||
| return xr.DataArray(matrix * da, [index, da.coords["time"]]) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it wise to hardcode that here? Since it's a dict it doesn't give you any benefits, is it?