Skip to content

Commit

Permalink
Add some more mypy checks (#8193)
Browse files Browse the repository at this point in the history
* Add some more mypy checks

This disallows redundant casts

FWIW I thought we had gone through many of the tests and forced them to have typed defs, maybe with the intention of turning on some elements of strict mode. But then I don't think we have any strictness checks at the moment, and I see many functions in the tests which don't have typing. Am I mis-rembering?? Or maybe we started but didn't get far enough (it is a big project...)

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
max-sixty and pre-commit-ci[bot] committed Sep 17, 2023
1 parent fb9c9b3 commit b08a9d8
Show file tree
Hide file tree
Showing 7 changed files with 10 additions and 14 deletions.
6 changes: 2 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ exclude_lines = ["pragma: no cover", "if TYPE_CHECKING"]
exclude = 'xarray/util/generate_.*\.py'
files = "xarray"
show_error_codes = true
show_error_context = true
warn_redundant_casts = true
warn_unused_ignores = true

# Most of the numerical computing stack doesn't have type annotations yet.
Expand Down Expand Up @@ -116,10 +118,6 @@ module = [
"numpy.exceptions.*", # remove once support for `numpy<2.0` has been dropped
]

[[tool.mypy.overrides]]
ignore_errors = true
module = []

[tool.ruff]
builtins = ["ellipsis"]
exclude = [
Expand Down
4 changes: 2 additions & 2 deletions xarray/core/dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -2564,7 +2564,7 @@ def expand_dims(
raise ValueError("dims should not contain duplicate values.")
dim = dict.fromkeys(dim, 1)
elif dim is not None and not isinstance(dim, Mapping):
dim = {cast(Hashable, dim): 1}
dim = {dim: 1}

dim = either_dict_or_kwargs(dim, dim_kwargs, "expand_dims")
ds = self._to_temp_dataset().expand_dims(dim, axis)
Expand Down Expand Up @@ -4362,7 +4362,7 @@ def from_series(cls, series: pd.Series, sparse: bool = False) -> DataArray:
temp_name = "__temporary_name"
df = pd.DataFrame({temp_name: series})
ds = Dataset.from_dataframe(df, sparse=sparse)
result = cast(DataArray, ds[temp_name])
result = ds[temp_name]
result.name = series.name
return result

Expand Down
4 changes: 2 additions & 2 deletions xarray/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

# remove once numpy 2.0 is the oldest supported version
try:
from numpy.exceptions import RankWarning
from numpy.exceptions import RankWarning # type: ignore[attr-defined,unused-ignore]
except ImportError:
from numpy import RankWarning

Expand Down Expand Up @@ -447,7 +447,7 @@ def __contains__(self, key: Hashable) -> bool:

def __getitem__(self, key: Hashable) -> DataArray:
if key not in self._dataset._coord_names:
return cast("DataArray", self._dataset[key])
return self._dataset[key]
raise KeyError(key)

def __repr__(self) -> str:
Expand Down
2 changes: 1 addition & 1 deletion xarray/core/nputils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

# remove once numpy 2.0 is the oldest supported version
try:
from numpy.exceptions import RankWarning
from numpy.exceptions import RankWarning # type: ignore[attr-defined,unused-ignore]
except ImportError:
from numpy import RankWarning

Expand Down
4 changes: 1 addition & 3 deletions xarray/plot/dataarray_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -1052,9 +1052,7 @@ def newplotfunc(
else:
hueplt_norm_values: list[np.ndarray | None]
if hueplt_norm.data is not None:
hueplt_norm_values = list(
cast("DataArray", hueplt_norm.data).to_numpy()
)
hueplt_norm_values = list(hueplt_norm.data.to_numpy())
else:
hueplt_norm_values = [hueplt_norm.data]

Expand Down
2 changes: 1 addition & 1 deletion xarray/tests/test_dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

# remove once numpy 2.0 is the oldest supported version
try:
from numpy.exceptions import RankWarning
from numpy.exceptions import RankWarning # type: ignore[attr-defined,unused-ignore]
except ImportError:
from numpy import RankWarning

Expand Down
2 changes: 1 addition & 1 deletion xarray/tests/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

# remove once numpy 2.0 is the oldest supported version
try:
from numpy.exceptions import RankWarning
from numpy.exceptions import RankWarning # type: ignore[attr-defined,unused-ignore]
except ImportError:
from numpy import RankWarning

Expand Down

0 comments on commit b08a9d8

Please sign in to comment.