Skip to content

Commit 6baceca

Browse files
authored
Repo checker (#9450)
* Remove default mypy option * Implement mypy ignore-without-code option * Enable mypy redundant-expr * Fix erroneous tuple types * Remove ruff target-version, redundant with project * Use extend selections for ruff * Fix B009 and B010 with ruff * Fix test parametrization * Fix FutureWarning * Make zips strict unless it is causing errors In which case set them to explicit False * Add a commit message for pre-commit autoupdate
1 parent cc74d3a commit 6baceca

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+421
-313
lines changed

.pre-commit-config.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# https://pre-commit.com/
22
ci:
33
autoupdate_schedule: monthly
4+
autoupdate_commit_msg: 'Update pre-commit hooks'
45
exclude: 'xarray/datatree_.*'
56
repos:
67
- repo: https://github.com/pre-commit/pre-commit-hooks

asv_bench/benchmarks/dataset_io.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -724,7 +724,7 @@ class PerformanceBackend(xr.backends.BackendEntrypoint):
724724
def open_dataset(
725725
self,
726726
filename_or_obj: str | os.PathLike | None,
727-
drop_variables: tuple[str] = None,
727+
drop_variables: tuple[str, ...] = None,
728728
*,
729729
mask_and_scale=True,
730730
decode_times=True,

asv_bench/benchmarks/groupby.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ def setup(self, use_cftime, use_flox):
174174
# GH9426 - deep-copying CFTime object arrays is weirdly slow
175175
asda = xr.DataArray(time)
176176
labeled_time = []
177-
for year, month in zip(asda.dt.year, asda.dt.month):
177+
for year, month in zip(asda.dt.year, asda.dt.month, strict=True):
178178
labeled_time.append(cftime.datetime(year, month, 1))
179179

180180
self.da = xr.DataArray(

asv_bench/benchmarks/rolling.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def time_rolling_long(self, func, pandas, use_bottleneck):
6464
def time_rolling_np(self, window_, min_periods, use_bottleneck):
6565
with xr.set_options(use_bottleneck=use_bottleneck):
6666
self.ds.rolling(x=window_, center=False, min_periods=min_periods).reduce(
67-
getattr(np, "nansum")
67+
np.nansum
6868
).load()
6969

7070
@parameterized(

doc/user-guide/testing.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ different type:
193193

194194
.. ipython:: python
195195
196-
def sparse_random_arrays(shape: tuple[int]) -> sparse._coo.core.COO:
196+
def sparse_random_arrays(shape: tuple[int, ...]) -> sparse._coo.core.COO:
197197
"""Strategy which generates random sparse.COO arrays"""
198198
if shape is None:
199199
shape = npst.array_shapes()

properties/test_pandas_roundtrip.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def test_roundtrip_dataarray(data, arr) -> None:
8080
tuple
8181
)
8282
)
83-
coords = {name: np.arange(n) for (name, n) in zip(names, arr.shape)}
83+
coords = {name: np.arange(n) for (name, n) in zip(names, arr.shape, strict=True)}
8484
original = xr.DataArray(arr, dims=names, coords=coords)
8585
roundtripped = xr.DataArray(original.to_pandas())
8686
xr.testing.assert_identical(original, roundtripped)

pyproject.toml

+3-5
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,13 @@ source = ["xarray"]
8484
exclude_lines = ["pragma: no cover", "if TYPE_CHECKING"]
8585

8686
[tool.mypy]
87-
enable_error_code = "redundant-self"
87+
enable_error_code = ["ignore-without-code", "redundant-self", "redundant-expr"]
8888
exclude = [
8989
'build',
9090
'xarray/util/generate_.*\.py',
9191
'xarray/datatree_/doc/.*\.py',
9292
]
9393
files = "xarray"
94-
show_error_codes = true
9594
show_error_context = true
9695
warn_redundant_casts = true
9796
warn_unused_configs = true
@@ -240,7 +239,6 @@ extend-exclude = [
240239
"doc",
241240
"_typed_ops.pyi",
242241
]
243-
target-version = "py310"
244242

245243
[tool.ruff.lint]
246244
# E402: module level import not at top of file
@@ -249,13 +247,13 @@ target-version = "py310"
249247
extend-safe-fixes = [
250248
"TID252", # absolute imports
251249
]
252-
ignore = [
250+
extend-ignore = [
253251
"E402",
254252
"E501",
255253
"E731",
256254
"UP007",
257255
]
258-
select = [
256+
extend-select = [
259257
"F", # Pyflakes
260258
"E", # Pycodestyle
261259
"W",

xarray/backends/api.py

+10-6
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
try:
5252
from dask.delayed import Delayed
5353
except ImportError:
54-
Delayed = None # type: ignore
54+
Delayed = None # type: ignore[assignment, misc]
5555
from io import BufferedIOBase
5656

5757
from xarray.backends.common import BackendEntrypoint
@@ -1113,7 +1113,7 @@ def open_mfdataset(
11131113
list(combined_ids_paths.keys()),
11141114
list(combined_ids_paths.values()),
11151115
)
1116-
elif combine == "by_coords" and concat_dim is not None:
1116+
elif concat_dim is not None:
11171117
raise ValueError(
11181118
"When combine='by_coords', passing a value for `concat_dim` has no "
11191119
"effect. To manually combine along a specific dimension you should "
@@ -1432,7 +1432,7 @@ def to_netcdf(
14321432
store.sync()
14331433
return target.getvalue()
14341434
finally:
1435-
if not multifile and compute:
1435+
if not multifile and compute: # type: ignore[redundant-expr]
14361436
store.close()
14371437

14381438
if not compute:
@@ -1585,8 +1585,9 @@ def save_mfdataset(
15851585
multifile=True,
15861586
**kwargs,
15871587
)
1588-
for ds, path, group in zip(datasets, paths, groups)
1589-
]
1588+
for ds, path, group in zip(datasets, paths, groups, strict=True)
1589+
],
1590+
strict=True,
15901591
)
15911592

15921593
try:
@@ -1600,7 +1601,10 @@ def save_mfdataset(
16001601
import dask
16011602

16021603
return dask.delayed(
1603-
[dask.delayed(_finalize_store)(w, s) for w, s in zip(writes, stores)]
1604+
[
1605+
dask.delayed(_finalize_store)(w, s)
1606+
for w, s in zip(writes, stores, strict=True)
1607+
]
16041608
)
16051609

16061610

xarray/backends/common.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ def set_dimensions(self, variables, unlimited_dims=None):
431431
for v in unlimited_dims: # put unlimited_dims first
432432
dims[v] = None
433433
for v in variables.values():
434-
dims.update(dict(zip(v.dims, v.shape)))
434+
dims.update(dict(zip(v.dims, v.shape, strict=True)))
435435

436436
for dim, length in dims.items():
437437
if dim in existing_dims and length != existing_dims[dim]:

xarray/backends/file_manager.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ def __getstate__(self):
276276
def __setstate__(self, state) -> None:
277277
"""Restore from a pickle."""
278278
opener, args, mode, kwargs, lock, manager_id = state
279-
self.__init__( # type: ignore
279+
self.__init__( # type: ignore[misc]
280280
opener, *args, mode=mode, kwargs=kwargs, lock=lock, manager_id=manager_id
281281
)
282282

xarray/backends/h5netcdf_.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,9 @@ def open_store_variable(self, name, var):
208208
"shuffle": var.shuffle,
209209
}
210210
if var.chunks:
211-
encoding["preferred_chunks"] = dict(zip(var.dimensions, var.chunks))
211+
encoding["preferred_chunks"] = dict(
212+
zip(var.dimensions, var.chunks, strict=True)
213+
)
212214
# Convert h5py-style compression options to NetCDF4-Python
213215
# style, if possible
214216
if var.compression == "gzip":

xarray/backends/netCDF4_.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,9 @@ def _extract_nc4_variable_encoding(
278278
chunksizes = encoding["chunksizes"]
279279
chunks_too_big = any(
280280
c > d and dim not in unlimited_dims
281-
for c, d, dim in zip(chunksizes, variable.shape, variable.dims)
281+
for c, d, dim in zip(
282+
chunksizes, variable.shape, variable.dims, strict=False
283+
)
282284
)
283285
has_original_shape = "original_shape" in encoding
284286
changed_shape = (
@@ -446,7 +448,9 @@ def open_store_variable(self, name: str, var):
446448
else:
447449
encoding["contiguous"] = False
448450
encoding["chunksizes"] = tuple(chunking)
449-
encoding["preferred_chunks"] = dict(zip(var.dimensions, chunking))
451+
encoding["preferred_chunks"] = dict(
452+
zip(var.dimensions, chunking, strict=True)
453+
)
450454
# TODO: figure out how to round-trip "endian-ness" without raising
451455
# warnings from netCDF4
452456
# encoding['endian'] = var.endian()

xarray/backends/plugins.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ def get_backend(engine: str | type[BackendEntrypoint]) -> BackendEntrypoint:
199199
"https://docs.xarray.dev/en/stable/getting-started-guide/installing.html"
200200
)
201201
backend = engines[engine]
202-
elif isinstance(engine, type) and issubclass(engine, BackendEntrypoint):
202+
elif issubclass(engine, BackendEntrypoint):
203203
backend = engine()
204204
else:
205205
raise TypeError(

xarray/backends/zarr.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ def _determine_zarr_chunks(enc_chunks, var_chunks, ndim, name, safe_chunks):
186186
# TODO: incorporate synchronizer to allow writes from multiple dask
187187
# threads
188188
if var_chunks and enc_chunks_tuple:
189-
for zchunk, dchunks in zip(enc_chunks_tuple, var_chunks):
189+
for zchunk, dchunks in zip(enc_chunks_tuple, var_chunks, strict=True):
190190
for dchunk in dchunks[:-1]:
191191
if dchunk % zchunk:
192192
base_error = (
@@ -548,13 +548,13 @@ def open_store_variable(self, name, zarr_array=None):
548548

549549
encoding = {
550550
"chunks": zarr_array.chunks,
551-
"preferred_chunks": dict(zip(dimensions, zarr_array.chunks)),
551+
"preferred_chunks": dict(zip(dimensions, zarr_array.chunks, strict=True)),
552552
"compressor": zarr_array.compressor,
553553
"filters": zarr_array.filters,
554554
}
555555
# _FillValue needs to be in attributes, not encoding, so it will get
556556
# picked up by decode_cf
557-
if getattr(zarr_array, "fill_value") is not None:
557+
if zarr_array.fill_value is not None:
558558
attributes["_FillValue"] = zarr_array.fill_value
559559

560560
return Variable(dimensions, data, attributes, encoding)
@@ -576,7 +576,7 @@ def get_dimensions(self):
576576
dimensions = {}
577577
for k, v in self.zarr_group.arrays():
578578
dim_names, _ = _get_zarr_dims_and_attrs(v, DIMENSION_KEY, try_nczarr)
579-
for d, s in zip(dim_names, v.shape):
579+
for d, s in zip(dim_names, v.shape, strict=True):
580580
if d in dimensions and dimensions[d] != s:
581581
raise ValueError(
582582
f"found conflicting lengths for dimension {d} "

xarray/coding/calendar_ops.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ def convert_calendar(
198198
_convert_to_new_calendar_with_new_day_of_year(
199199
date, newdoy, calendar, use_cftime
200200
)
201-
for date, newdoy in zip(time.variable._data.array, new_doy)
201+
for date, newdoy in zip(time.variable._data.array, new_doy, strict=True)
202202
],
203203
dims=(dim,),
204204
name=dim,

xarray/coding/times.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ def _unpack_time_units_and_ref_date(units: str) -> tuple[str, pd.Timestamp]:
204204

205205

206206
def _decode_cf_datetime_dtype(
207-
data, units: str, calendar: str, use_cftime: bool | None
207+
data, units: str, calendar: str | None, use_cftime: bool | None
208208
) -> np.dtype:
209209
# Verify that at least the first and last date can be decoded
210210
# successfully. Otherwise, tracebacks end up swallowed by
@@ -704,7 +704,7 @@ def _cast_to_dtype_if_safe(num: np.ndarray, dtype: np.dtype) -> np.ndarray:
704704

705705

706706
def encode_cf_datetime(
707-
dates: T_DuckArray, # type: ignore
707+
dates: T_DuckArray, # type: ignore[misc]
708708
units: str | None = None,
709709
calendar: str | None = None,
710710
dtype: np.dtype | None = None,
@@ -726,7 +726,7 @@ def encode_cf_datetime(
726726

727727

728728
def _eagerly_encode_cf_datetime(
729-
dates: T_DuckArray, # type: ignore
729+
dates: T_DuckArray, # type: ignore[misc]
730730
units: str | None = None,
731731
calendar: str | None = None,
732732
dtype: np.dtype | None = None,
@@ -809,7 +809,7 @@ def _eagerly_encode_cf_datetime(
809809

810810

811811
def _encode_cf_datetime_within_map_blocks(
812-
dates: T_DuckArray, # type: ignore
812+
dates: T_DuckArray, # type: ignore[misc]
813813
units: str,
814814
calendar: str,
815815
dtype: np.dtype,
@@ -859,7 +859,7 @@ def _lazily_encode_cf_datetime(
859859

860860

861861
def encode_cf_timedelta(
862-
timedeltas: T_DuckArray, # type: ignore
862+
timedeltas: T_DuckArray, # type: ignore[misc]
863863
units: str | None = None,
864864
dtype: np.dtype | None = None,
865865
) -> tuple[T_DuckArray, str]:
@@ -871,7 +871,7 @@ def encode_cf_timedelta(
871871

872872

873873
def _eagerly_encode_cf_timedelta(
874-
timedeltas: T_DuckArray, # type: ignore
874+
timedeltas: T_DuckArray, # type: ignore[misc]
875875
units: str | None = None,
876876
dtype: np.dtype | None = None,
877877
allow_units_modification: bool = True,
@@ -923,7 +923,7 @@ def _eagerly_encode_cf_timedelta(
923923

924924

925925
def _encode_cf_timedelta_within_map_blocks(
926-
timedeltas: T_DuckArray, # type:ignore
926+
timedeltas: T_DuckArray, # type: ignore[misc]
927927
units: str,
928928
dtype: np.dtype,
929929
) -> T_DuckArray:

xarray/core/alignment.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -405,14 +405,15 @@ def align_indexes(self) -> None:
405405
zip(
406406
[joined_index] + matching_indexes,
407407
[joined_index_vars] + matching_index_vars,
408+
strict=True,
408409
)
409410
)
410411
need_reindex = self._need_reindex(dims, cmp_indexes)
411412
else:
412413
if len(matching_indexes) > 1:
413414
need_reindex = self._need_reindex(
414415
dims,
415-
list(zip(matching_indexes, matching_index_vars)),
416+
list(zip(matching_indexes, matching_index_vars, strict=True)),
416417
)
417418
else:
418419
need_reindex = False
@@ -557,7 +558,7 @@ def reindex_all(self) -> None:
557558
self.results = tuple(
558559
self._reindex_one(obj, matching_indexes)
559560
for obj, matching_indexes in zip(
560-
self.objects, self.objects_matching_indexes
561+
self.objects, self.objects_matching_indexes, strict=True
561562
)
562563
)
563564

@@ -952,7 +953,7 @@ def is_alignable(obj):
952953
fill_value=fill_value,
953954
)
954955

955-
for position, key, aligned_obj in zip(positions, keys, aligned):
956+
for position, key, aligned_obj in zip(positions, keys, aligned, strict=True):
956957
if key is no_key:
957958
out[position] = aligned_obj
958959
else:

xarray/core/combine.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,8 @@ def _infer_concat_order_from_coords(datasets):
139139
# Append positions along extra dimension to structure which
140140
# encodes the multi-dimensional concatenation order
141141
tile_ids = [
142-
tile_id + (position,) for tile_id, position in zip(tile_ids, order)
142+
tile_id + (position,)
143+
for tile_id, position in zip(tile_ids, order, strict=True)
143144
]
144145

145146
if len(datasets) > 1 and not concat_dims:
@@ -148,7 +149,7 @@ def _infer_concat_order_from_coords(datasets):
148149
"order the datasets for concatenation"
149150
)
150151

151-
combined_ids = dict(zip(tile_ids, datasets))
152+
combined_ids = dict(zip(tile_ids, datasets, strict=True))
152153

153154
return combined_ids, concat_dims
154155

@@ -349,7 +350,7 @@ def _nested_combine(
349350
combined_ids = _infer_concat_order_from_positions(datasets)
350351
else:
351352
# Already sorted so just use the ids already passed
352-
combined_ids = dict(zip(ids, datasets))
353+
combined_ids = dict(zip(ids, datasets, strict=True))
353354

354355
# Check that the inferred shape is combinable
355356
_check_shape_tile_ids(combined_ids)

xarray/core/common.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ def sizes(self: Any) -> Mapping[Hashable, int]:
254254
--------
255255
Dataset.sizes
256256
"""
257-
return Frozen(dict(zip(self.dims, self.shape)))
257+
return Frozen(dict(zip(self.dims, self.shape, strict=True)))
258258

259259

260260
class AttrAccessMixin:

0 commit comments

Comments
 (0)