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

Enforce ruff/flake8-comprehensions rules (C4) #9724

Merged
merged 6 commits into from
Nov 7, 2024
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
2 changes: 1 addition & 1 deletion properties/test_index_manipulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ def rename_vars(self, newname, data):
def drop_dims(self, data):
dims = data.draw(
st.lists(
st.sampled_from(sorted(tuple(self.dataset.dims))),
st.sampled_from(sorted(self.dataset.dims)),
min_size=1,
unique=True,
)
Expand Down
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ extend-safe-fixes = [
"TID252", # absolute imports
]
ignore = [
"C40",
"E402",
"E501",
"E731",
Expand All @@ -254,6 +255,7 @@ ignore = [
]
extend-select = [
"B", # flake8-bugbear
"C4", # flake8-comprehensions
"F", # Pyflakes
"E", # Pycodestyle
"W",
Expand Down
4 changes: 2 additions & 2 deletions xarray/backends/zarr.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ def __init__(self, zarr_array):
if (
not _zarr_v3()
and self._array.filters is not None
and any([filt.codec_id == "vlen-utf8" for filt in self._array.filters])
and any(filt.codec_id == "vlen-utf8" for filt in self._array.filters)
):
dtype = coding.strings.create_vlen_dtype(str)
else:
Expand Down Expand Up @@ -649,7 +649,7 @@ def open_store(
use_zarr_fill_value_as_mask=use_zarr_fill_value_as_mask,
zarr_format=zarr_format,
)
group_paths = [node for node in _iter_zarr_groups(zarr_group, parent=group)]
group_paths = list(_iter_zarr_groups(zarr_group, parent=group))
return {
group: cls(
zarr_group.get(group),
Expand Down
2 changes: 1 addition & 1 deletion xarray/core/computation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2252,7 +2252,7 @@ def unify_chunks(*objects: Dataset | DataArray) -> tuple[Dataset | DataArray, ..
if not unify_chunks_args:
return objects

chunkmanager = get_chunked_array_type(*[arg for arg in unify_chunks_args])
chunkmanager = get_chunked_array_type(*list(unify_chunks_args))
_, chunked_data = chunkmanager.unify_chunks(*unify_chunks_args)
chunked_data_iter = iter(chunked_data)
out: list[Dataset | DataArray] = []
Expand Down
8 changes: 4 additions & 4 deletions xarray/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ def _get_func_args(func, param_names):
else:
params = list(func_args)[1:]
if any(
[(p.kind in [p.VAR_POSITIONAL, p.VAR_KEYWORD]) for p in func_args.values()]
(p.kind in [p.VAR_POSITIONAL, p.VAR_KEYWORD]) for p in func_args.values()
):
raise ValueError(
"`param_names` must be provided because `func` takes variable length arguments."
Expand Down Expand Up @@ -1586,7 +1586,7 @@ def __getitem__(
message = f"No variable named {key!r}. Variables on the dataset include {shorten_list_repr(list(self.variables.keys()), max_items=10)}"
# If someone attempts `ds['foo' , 'bar']` instead of `ds[['foo', 'bar']]`
if isinstance(key, tuple):
message += f"\nHint: use a list to select multiple variables, for example `ds[{[d for d in key]}]`"
message += f"\nHint: use a list to select multiple variables, for example `ds[{list(key)}]`"
raise KeyError(message) from e

if utils.iterable_of_hashable(key):
Expand Down Expand Up @@ -1686,7 +1686,7 @@ def _setitem_check(self, key, value):
f"Variables {missing_vars} in new values"
f" not available in original dataset:\n{self}"
)
elif not any([isinstance(value, t) for t in [DataArray, Number, str]]):
elif not any(isinstance(value, t) for t in [DataArray, Number, str]):
raise TypeError(
"Dataset assignment only accepts DataArrays, Datasets, and scalars."
)
Expand Down Expand Up @@ -4078,7 +4078,7 @@ def interp(
)
indexers.update({d: self.variables[d] for d in sdims})

obj = self if assume_sorted else self.sortby([k for k in coords])
obj = self if assume_sorted else self.sortby(list(coords))

def maybe_variable(obj, k):
# workaround to get variable for dimension without coordinate.
Expand Down
4 changes: 1 addition & 3 deletions xarray/core/datatree_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,7 @@ def _check_single_set_return_values(path_to_node: str, obj: Any) -> int | None:
def _check_all_return_values(returned_objects) -> int | None:
"""Walk through all values returned by mapping func over subtrees, raising on any invalid or inconsistent types."""

result_data_objects = [
(path_to_node, r) for path_to_node, r in returned_objects.items()
]
result_data_objects = list(returned_objects.items())

first_path, result = result_data_objects[0]
return_values = _check_single_set_return_values(first_path, result)
Expand Down
21 changes: 9 additions & 12 deletions xarray/core/indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -880,7 +880,7 @@ def _check_dim_compat(variables: Mapping[Any, Variable], all_dims: str = "equal"
either share the same (single) dimension or each have a different dimension.

"""
if any([var.ndim != 1 for var in variables.values()]):
if any(var.ndim != 1 for var in variables.values()):
raise ValueError("PandasMultiIndex only accepts 1-dimensional variables")

dims = {var.dims for var in variables.values()}
Expand Down Expand Up @@ -1208,7 +1208,7 @@ def sel(self, labels, method=None, tolerance=None) -> IndexSelResult:
indexer: int | slice | np.ndarray | Variable | DataArray

# label(s) given for multi-index level(s)
if all([lbl in self.index.names for lbl in labels]):
if all(lbl in self.index.names for lbl in labels):
label_values = {}
for k, v in labels.items():
label_array = normalize_label(v, dtype=self.level_coords_dtype[k])
Expand All @@ -1221,7 +1221,7 @@ def sel(self, labels, method=None, tolerance=None) -> IndexSelResult:
f"available along coordinate {k!r} (multi-index level)"
) from err

has_slice = any([isinstance(v, slice) for v in label_values.values()])
has_slice = any(isinstance(v, slice) for v in label_values.values())

if len(label_values) == self.index.nlevels and not has_slice:
indexer = self.index.get_loc(
Expand Down Expand Up @@ -1268,9 +1268,7 @@ def sel(self, labels, method=None, tolerance=None) -> IndexSelResult:
else:
levels = [self.index.names[i] for i in range(len(label))]
indexer, new_index = self.index.get_loc_level(label, level=levels)
scalar_coord_values.update(
{k: v for k, v in zip(levels, label, strict=True)}
)
scalar_coord_values.update(dict(zip(levels, label, strict=True)))

else:
label_array = normalize_label(label)
Expand Down Expand Up @@ -1371,10 +1369,9 @@ def rename(self, name_dict, dims_dict):
index = self.index.rename(new_names)

new_dim = dims_dict.get(self.dim, self.dim)
new_level_coords_dtype = {
k: v
for k, v in zip(new_names, self.level_coords_dtype.values(), strict=True)
}
new_level_coords_dtype = dict(
zip(new_names, self.level_coords_dtype.values(), strict=True)
)
return self._replace(
index, dim=new_dim, level_coords_dtype=new_level_coords_dtype
)
Expand Down Expand Up @@ -1820,7 +1817,7 @@ def _apply_indexes_fast(indexes: Indexes[Index], args: Mapping[Any, Any], func:
# multi-index arrays
indexes_fast, coords = indexes._indexes, indexes._variables

new_indexes: dict[Hashable, Index] = {k: v for k, v in indexes_fast.items()}
new_indexes: dict[Hashable, Index] = dict(indexes_fast.items())
new_index_variables: dict[Hashable, Variable] = {}
for name, index in indexes_fast.items():
coord = coords[name]
Expand Down Expand Up @@ -1848,7 +1845,7 @@ def _apply_indexes(
args: Mapping[Any, Any],
func: str,
) -> tuple[dict[Hashable, Index], dict[Hashable, Variable]]:
new_indexes: dict[Hashable, Index] = {k: v for k, v in indexes.items()}
new_indexes: dict[Hashable, Index] = dict(indexes.items())
new_index_variables: dict[Hashable, Variable] = {}

for index, index_vars in indexes.group_by_index():
Expand Down
2 changes: 1 addition & 1 deletion xarray/core/rolling.py
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ def _counts(self, keep_attrs: bool | None) -> DataArray:
counts = (
self.obj.notnull(keep_attrs=keep_attrs)
.rolling(
{d: w for d, w in zip(self.dim, self.window, strict=True)},
dict(zip(self.dim, self.window, strict=True)),
center={d: self.center[i] for i, d in enumerate(self.dim)},
)
.construct(rolling_dim, fill_value=False, keep_attrs=keep_attrs)
Expand Down
6 changes: 3 additions & 3 deletions xarray/core/treenode.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ def iter_lineage(self: Tree) -> tuple[Tree, ...]:
DeprecationWarning,
stacklevel=2,
)
return tuple((self, *self.parents))
return (self, *self.parents)

@property
def lineage(self: Tree) -> tuple[Tree, ...]:
Expand Down Expand Up @@ -349,7 +349,7 @@ def ancestors(self: Tree) -> tuple[Tree, ...]:
DeprecationWarning,
stacklevel=2,
)
return tuple((*reversed(self.parents), self))
return (*reversed(self.parents), self)

@property
def root(self: Tree) -> Tree:
Expand Down Expand Up @@ -380,7 +380,7 @@ def leaves(self: Tree) -> tuple[Tree, ...]:

Leaf nodes are defined as nodes which have no children.
"""
return tuple([node for node in self.subtree if node.is_leaf])
return tuple(node for node in self.subtree if node.is_leaf)

@property
def siblings(self: Tree) -> dict[str, Tree]:
Expand Down
8 changes: 3 additions & 5 deletions xarray/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1069,11 +1069,9 @@ def contains_only_chunked_or_numpy(obj) -> bool:
obj = obj._to_temp_dataset()

return all(
[
isinstance(var._data, ExplicitlyIndexed | np.ndarray)
or is_chunked_array(var._data)
for var in obj._variables.values()
]
isinstance(var._data, ExplicitlyIndexed | np.ndarray)
or is_chunked_array(var._data)
for var in obj._variables.values()
)


Expand Down
2 changes: 1 addition & 1 deletion xarray/core/variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -1547,7 +1547,7 @@ def _unstack_once(
else:
sizes = itertools.product(*[range(s) for s in reordered.shape[:-1]])
tuple_indexes = itertools.product(sizes, codes)
indexes = map(lambda x: list(itertools.chain(*x)), tuple_indexes) # type: ignore[assignment]
indexes = (list(itertools.chain(*x)) for x in tuple_indexes) # type: ignore[assignment]

data = COO(
coords=np.array(list(indexes)).T,
Expand Down
4 changes: 2 additions & 2 deletions xarray/plot/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ def _assert_valid_xy(
valid_xy = (set(darray.dims) | set(darray.coords)) - multiindex_dims

if (xy is not None) and (xy not in valid_xy):
valid_xy_str = "', '".join(sorted(tuple(str(v) for v in valid_xy)))
valid_xy_str = "', '".join(sorted(str(v) for v in valid_xy))
raise ValueError(
f"{name} must be one of None, '{valid_xy_str}'. Received '{xy}' instead."
)
Expand Down Expand Up @@ -1219,7 +1219,7 @@ def _adjust_legend_subtitles(legend):

def _infer_meta_data(ds, x, y, hue, hue_style, add_guide, funcname):
dvars = set(ds.variables.keys())
error_msg = f" must be one of ({', '.join(sorted(tuple(str(v) for v in dvars)))})"
error_msg = f" must be one of ({', '.join(sorted(str(v) for v in dvars))})"

if x not in dvars:
raise ValueError(f"Expected 'x' {error_msg}. Received {x} instead.")
Expand Down
4 changes: 2 additions & 2 deletions xarray/tests/test_coding_times.py
Original file line number Diff line number Diff line change
Expand Up @@ -738,15 +738,15 @@ def test_encode_time_bounds() -> None:

# if time_bounds attrs are same as time attrs, it doesn't matter
ds.time_bounds.encoding = {"calendar": "noleap", "units": "days since 2000-01-01"}
encoded, _ = cf_encoder({k: v for k, v in ds.variables.items()}, ds.attrs)
encoded, _ = cf_encoder(dict(ds.variables.items()), ds.attrs)
assert_equal(encoded["time_bounds"], expected["time_bounds"])
assert "calendar" not in encoded["time_bounds"].attrs
assert "units" not in encoded["time_bounds"].attrs

# for CF-noncompliant case of time_bounds attrs being different from
# time attrs; preserve them for faithful roundtrip
ds.time_bounds.encoding = {"calendar": "noleap", "units": "days since 1849-01-01"}
encoded, _ = cf_encoder({k: v for k, v in ds.variables.items()}, ds.attrs)
encoded, _ = cf_encoder(dict(ds.variables.items()), ds.attrs)
with pytest.raises(AssertionError):
assert_equal(encoded["time_bounds"], expected["time_bounds"])
assert "calendar" not in encoded["time_bounds"].attrs
Expand Down
15 changes: 3 additions & 12 deletions xarray/tests/test_dask.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,7 @@ def test_chunk(self):
assert rechunked.chunks == expected
self.assertLazyAndIdentical(self.eager_var, rechunked)

expected_chunksizes = {
dim: chunks
for dim, chunks in zip(self.lazy_var.dims, expected, strict=True)
}
expected_chunksizes = dict(zip(self.lazy_var.dims, expected, strict=True))
assert rechunked.chunksizes == expected_chunksizes

def test_indexing(self):
Expand Down Expand Up @@ -355,19 +352,13 @@ def test_chunk(self) -> None:
assert rechunked.chunks == expected
self.assertLazyAndIdentical(self.eager_array, rechunked)

expected_chunksizes = {
dim: chunks
for dim, chunks in zip(self.lazy_array.dims, expected, strict=True)
}
expected_chunksizes = dict(zip(self.lazy_array.dims, expected, strict=True))
assert rechunked.chunksizes == expected_chunksizes

# Test Dataset
lazy_dataset = self.lazy_array.to_dataset()
eager_dataset = self.eager_array.to_dataset()
expected_chunksizes = {
dim: chunks
for dim, chunks in zip(lazy_dataset.dims, expected, strict=True)
}
expected_chunksizes = dict(zip(lazy_dataset.dims, expected, strict=True))
rechunked = lazy_dataset.chunk(chunks)

# Dataset.chunks has a different return type to DataArray.chunks - see issue #5843
Expand Down
10 changes: 5 additions & 5 deletions xarray/tests/test_dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,8 @@ def test_indexes(self) -> None:
}
assert array.xindexes.keys() == expected_xindexes.keys()
assert array.indexes.keys() == expected_indexes.keys()
assert all([isinstance(idx, pd.Index) for idx in array.indexes.values()])
assert all([isinstance(idx, Index) for idx in array.xindexes.values()])
assert all(isinstance(idx, pd.Index) for idx in array.indexes.values())
assert all(isinstance(idx, Index) for idx in array.xindexes.values())
for k in expected_indexes:
assert array.xindexes[k].equals(expected_xindexes[k])
assert array.indexes[k].equals(expected_indexes[k])
Expand Down Expand Up @@ -6145,7 +6145,7 @@ def test_argmin_dim(
]:
if np.array([np.isnan(i) for i in inds.values()]).any():
with pytest.raises(ValueError):
ar.argmin(dim=[d for d in inds])
ar.argmin(dim=list(inds))
return

result0 = ar.argmin(dim=["x"])
Expand Down Expand Up @@ -6372,7 +6372,7 @@ def test_argmax_dim(
]:
if np.array([np.isnan(i) for i in inds.values()]).any():
with pytest.raises(ValueError):
ar.argmax(dim=[d for d in inds])
ar.argmax(dim=list(inds))
return

result0 = ar.argmax(dim=["x"])
Expand Down Expand Up @@ -6562,7 +6562,7 @@ def test_idxminmax_dask(self, op: str, ndim: int) -> None:

ar0_raw = xr.DataArray(
np.random.random_sample(size=[10] * ndim),
dims=[i for i in "abcdefghij"[: ndim - 1]] + ["x"],
dims=list("abcdefghij"[: ndim - 1]) + ["x"],
coords={"x": np.arange(10)},
attrs=self.attrs,
)
Expand Down
4 changes: 2 additions & 2 deletions xarray/tests/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -720,13 +720,13 @@ def test_properties(self) -> None:
assert set(ds.xindexes) == {"dim2", "dim3", "time"}
assert len(ds.xindexes) == 3
assert "dim2" in repr(ds.xindexes)
assert all([isinstance(idx, Index) for idx in ds.xindexes.values()])
assert all(isinstance(idx, Index) for idx in ds.xindexes.values())

# indexes
assert set(ds.indexes) == {"dim2", "dim3", "time"}
assert len(ds.indexes) == 3
assert "dim2" in repr(ds.indexes)
assert all([isinstance(idx, pd.Index) for idx in ds.indexes.values()])
assert all(isinstance(idx, pd.Index) for idx in ds.indexes.values())

# coords
assert list(ds.coords) == ["dim2", "dim3", "time", "numbers"]
Expand Down
2 changes: 1 addition & 1 deletion xarray/tests/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -1791,7 +1791,7 @@ def test_groupby_fastpath_for_monotonic(self, use_flox: bool) -> None:
rev = array_rev.groupby("idx", squeeze=False)

for gb in [fwd, rev]:
assert all([isinstance(elem, slice) for elem in gb.encoded.group_indices])
assert all(isinstance(elem, slice) for elem in gb.encoded.group_indices)

with xr.set_options(use_flox=use_flox):
assert_identical(fwd.sum(), array)
Expand Down
2 changes: 1 addition & 1 deletion xarray/tests/test_indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,7 @@ def test_group_by_index(self, unique_indexes, indexes):
def test_to_pandas_indexes(self, indexes) -> None:
pd_indexes = indexes.to_pandas_indexes()
assert isinstance(pd_indexes, Indexes)
assert all([isinstance(idx, pd.Index) for idx in pd_indexes.values()])
assert all(isinstance(idx, pd.Index) for idx in pd_indexes.values())
assert indexes.variables == pd_indexes.variables

def test_copy_indexes(self, indexes) -> None:
Expand Down
4 changes: 1 addition & 3 deletions xarray/tests/test_strategies.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,7 @@ def fixed_array_strategy_fn(*, shape=None, dtype=None):
return st.just(arr)

dim_names = data.draw(dimension_names(min_dims=arr.ndim, max_dims=arr.ndim))
dim_sizes = {
name: size for name, size in zip(dim_names, arr.shape, strict=True)
}
dim_sizes = dict(zip(dim_names, arr.shape, strict=True))

var = data.draw(
variables(
Expand Down
2 changes: 1 addition & 1 deletion xarray/util/deprecation_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def inner(*args, **kwargs):
zip_args = zip(
kwonly_args[:n_extra_args], args[-n_extra_args:], strict=True
)
kwargs.update({name: arg for name, arg in zip_args})
kwargs.update(zip_args)

return func(*args[:-n_extra_args], **kwargs)

Expand Down
Loading