Skip to content

Add pyupgrade onto pre-commit #6152

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

Merged
merged 11 commits into from
Jan 19, 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
24 changes: 14 additions & 10 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,24 @@ repos:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
# isort should run before black as black sometimes tweaks the isort output
- id: debug-statements
- id: mixed-line-ending
# This wants to go before isort & flake8
- repo: https://github.com/myint/autoflake
rev: "v1.4"
hooks:
- id: autoflake # isort should run before black as black sometimes tweaks the isort output
args: ["--in-place", "--ignore-init-module-imports"]
- repo: https://github.com/PyCQA/isort
rev: 5.10.1
hooks:
- id: isort
- repo: https://github.com/asottile/pyupgrade
rev: v2.31.0
hooks:
- id: pyupgrade
args:
- "--py37-plus"
# https://github.com/python/black#version-control-integration
- repo: https://github.com/psf/black
rev: 21.12b0
Expand Down Expand Up @@ -47,12 +60,3 @@ repos:
typing-extensions==3.10.0.0,
numpy,
]
# run this occasionally, ref discussion https://github.com/pydata/xarray/pull/3194
# - repo: https://github.com/asottile/pyupgrade
# rev: v1.22.1
# hooks:
# - id: pyupgrade
# args:
# - "--py3-only"
# # remove on f-strings in Py3.7
# - "--keep-percent-format"
2 changes: 2 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ Internal Changes
- Removed internal checks for ``pd.Panel`` (:issue:`6145`).
By `Matthew Roeschke <https://github.com/mroeschke>`_.

- Add ``pyupgrade`` pre-commit hook (:pull:`6152`).
By `Maximilian Roos <https://github.com/max-sixty>`_.

.. _whats-new.0.20.2:

Expand Down
6 changes: 2 additions & 4 deletions xarray/backends/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,8 @@ def get_backend(engine):
backend = engine()
else:
raise TypeError(
(
"engine must be a string or a subclass of "
f"xarray.backends.BackendEntrypoint: {engine}"
)
"engine must be a string or a subclass of "
f"xarray.backends.BackendEntrypoint: {engine}"
)

return backend
12 changes: 6 additions & 6 deletions xarray/coding/cftime_offsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def rollback(self, date):
return date - type(self)()

def __str__(self):
return "<{}: n={}>".format(type(self).__name__, self.n)
return f"<{type(self).__name__}: n={self.n}>"

def __repr__(self):
return str(self)
Expand Down Expand Up @@ -399,10 +399,10 @@ def __mul__(self, other):
return type(self)(n=other * self.n, month=self.month)

def rule_code(self):
return "{}-{}".format(self._freq, _MONTH_ABBREVIATIONS[self.month])
return f"{self._freq}-{_MONTH_ABBREVIATIONS[self.month]}"

def __str__(self):
return "<{}: n={}, month={}>".format(type(self).__name__, self.n, self.month)
return f"<{type(self).__name__}: n={self.n}, month={self.month}>"


class QuarterBegin(QuarterOffset):
Expand Down Expand Up @@ -485,10 +485,10 @@ def __mul__(self, other):
return type(self)(n=other * self.n, month=self.month)

def rule_code(self):
return "{}-{}".format(self._freq, _MONTH_ABBREVIATIONS[self.month])
return f"{self._freq}-{_MONTH_ABBREVIATIONS[self.month]}"

def __str__(self):
return "<{}: n={}, month={}>".format(type(self).__name__, self.n, self.month)
return f"<{type(self).__name__}: n={self.n}, month={self.month}>"


class YearBegin(YearOffset):
Expand Down Expand Up @@ -741,7 +741,7 @@ def _generate_linear_range(start, end, periods):

total_seconds = (end - start).total_seconds()
values = np.linspace(0.0, total_seconds, periods, endpoint=True)
units = "seconds since {}".format(format_cftime_datetime(start))
units = f"seconds since {format_cftime_datetime(start)}"
calendar = start.calendar
return cftime.num2date(
values, units=units, calendar=calendar, only_use_cftime_datetimes=True
Expand Down
4 changes: 2 additions & 2 deletions xarray/coding/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

def create_vlen_dtype(element_type):
if element_type not in (str, bytes):
raise TypeError("unsupported type for vlen_dtype: {!r}".format(element_type))
raise TypeError(f"unsupported type for vlen_dtype: {element_type!r}")
# based on h5py.special_dtype
return np.dtype("O", metadata={"element_type": element_type})

Expand Down Expand Up @@ -227,7 +227,7 @@ def shape(self):
return self.array.shape[:-1]

def __repr__(self):
return "{}({!r})".format(type(self).__name__, self.array)
return f"{type(self).__name__}({self.array!r})"

def __getitem__(self, key):
# require slicing the last dimension completely
Expand Down
8 changes: 4 additions & 4 deletions xarray/coding/times.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ def _ensure_padded_year(ref_date):
matches_start_digits = re.match(r"(\d+)(.*)", ref_date)
if not matches_start_digits:
raise ValueError(f"invalid reference date for time units: {ref_date}")
ref_year, everything_else = [s for s in matches_start_digits.groups()]
ref_date_padded = "{:04d}{}".format(int(ref_year), everything_else)
ref_year, everything_else = (s for s in matches_start_digits.groups())
ref_date_padded = f"{int(ref_year):04d}{everything_else}"

warning_msg = (
f"Ambiguous reference date string: {ref_date}. The first value is "
Expand All @@ -155,7 +155,7 @@ def _unpack_netcdf_time_units(units):
if not matches:
raise ValueError(f"invalid time units: {units}")

delta_units, ref_date = [s.strip() for s in matches.groups()]
delta_units, ref_date = (s.strip() for s in matches.groups())
ref_date = _ensure_padded_year(ref_date)

return delta_units, ref_date
Expand Down Expand Up @@ -545,7 +545,7 @@ def _should_cftime_be_used(source, target_calendar, use_cftime):
def _cleanup_netcdf_time_units(units):
delta, ref_date = _unpack_netcdf_time_units(units)
try:
units = "{} since {}".format(delta, format_timestamp(ref_date))
units = f"{delta} since {format_timestamp(ref_date)}"
except (OutOfBoundsDatetime, ValueError):
# don't worry about reifying the units if they're out of bounds or
# formatted badly
Expand Down
63 changes: 28 additions & 35 deletions xarray/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,11 @@
TYPE_CHECKING,
Any,
Callable,
Dict,
Hashable,
Iterable,
Iterator,
List,
Mapping,
Optional,
Tuple,
TypeVar,
Union,
overload,
)

Expand Down Expand Up @@ -164,9 +159,7 @@ def __iter__(self: Any) -> Iterator[Any]:
raise TypeError("iteration over a 0-d array")
return self._iter()

def get_axis_num(
self, dim: Union[Hashable, Iterable[Hashable]]
) -> Union[int, Tuple[int, ...]]:
def get_axis_num(self, dim: Hashable | Iterable[Hashable]) -> int | tuple[int, ...]:
"""Return axis number(s) corresponding to dimension(s) in this array.

Parameters
Expand Down Expand Up @@ -244,7 +237,7 @@ def __getattr__(self, name: str) -> Any:
with suppress(KeyError):
return source[name]
raise AttributeError(
"{!r} object has no attribute {!r}".format(type(self).__name__, name)
f"{type(self).__name__!r} object has no attribute {name!r}"
)

# This complicated two-method design boosts overall performance of simple operations
Expand Down Expand Up @@ -284,37 +277,37 @@ def __setattr__(self, name: str, value: Any) -> None:
"assignment (e.g., `ds['name'] = ...`) instead of assigning variables."
) from e

def __dir__(self) -> List[str]:
def __dir__(self) -> list[str]:
"""Provide method name lookup and completion. Only provide 'public'
methods.
"""
extra_attrs = set(
extra_attrs = {
item
for source in self._attr_sources
for item in source
if isinstance(item, str)
)
}
return sorted(set(dir(type(self))) | extra_attrs)

def _ipython_key_completions_(self) -> List[str]:
def _ipython_key_completions_(self) -> list[str]:
"""Provide method for the key-autocompletions in IPython.
See http://ipython.readthedocs.io/en/stable/config/integrating.html#tab-completion
For the details.
"""
items = set(
items = {
item
for source in self._item_sources
for item in source
if isinstance(item, str)
)
}
return list(items)


def get_squeeze_dims(
xarray_obj,
dim: Union[Hashable, Iterable[Hashable], None] = None,
axis: Union[int, Iterable[int], None] = None,
) -> List[Hashable]:
dim: Hashable | Iterable[Hashable] | None = None,
axis: int | Iterable[int] | None = None,
) -> list[Hashable]:
"""Get a list of dimensions to squeeze out."""
if dim is not None and axis is not None:
raise ValueError("cannot use both parameters `axis` and `dim`")
Expand Down Expand Up @@ -346,15 +339,15 @@ def get_squeeze_dims(
class DataWithCoords(AttrAccessMixin):
"""Shared base class for Dataset and DataArray."""

_close: Optional[Callable[[], None]]
_close: Callable[[], None] | None

__slots__ = ("_close",)

def squeeze(
self,
dim: Union[Hashable, Iterable[Hashable], None] = None,
dim: Hashable | Iterable[Hashable] | None = None,
drop: bool = False,
axis: Union[int, Iterable[int], None] = None,
axis: int | Iterable[int] | None = None,
):
"""Return a new object with squeezed data.

Expand Down Expand Up @@ -416,8 +409,8 @@ def get_index(self, key: Hashable) -> pd.Index:
return pd.Index(range(self.sizes[key]), name=key)

def _calc_assign_results(
self: C, kwargs: Mapping[Any, Union[T, Callable[[C], T]]]
) -> Dict[Hashable, T]:
self: C, kwargs: Mapping[Any, T | Callable[[C], T]]
) -> dict[Hashable, T]:
return {k: v(self) if callable(v) else v for k, v in kwargs.items()}

def assign_coords(self, coords=None, **coords_kwargs):
Expand Down Expand Up @@ -535,7 +528,7 @@ def assign_attrs(self, *args, **kwargs):

def pipe(
self,
func: Union[Callable[..., T], Tuple[Callable[..., T], str]],
func: Callable[..., T] | tuple[Callable[..., T], str],
*args,
**kwargs,
) -> T:
Expand Down Expand Up @@ -802,7 +795,7 @@ def groupby_bins(
},
)

def weighted(self: T_DataWithCoords, weights: "DataArray") -> Weighted[T_Xarray]:
def weighted(self: T_DataWithCoords, weights: DataArray) -> Weighted[T_Xarray]:
"""
Weighted operations.

Expand All @@ -825,7 +818,7 @@ def rolling(
self,
dim: Mapping[Any, int] = None,
min_periods: int = None,
center: Union[bool, Mapping[Any, bool]] = False,
center: bool | Mapping[Any, bool] = False,
**window_kwargs: int,
):
"""
Expand Down Expand Up @@ -940,7 +933,7 @@ def coarsen(
self,
dim: Mapping[Any, int] = None,
boundary: str = "exact",
side: Union[str, Mapping[Any, str]] = "left",
side: str | Mapping[Any, str] = "left",
coord_func: str = "mean",
**window_kwargs: int,
):
Expand Down Expand Up @@ -1290,7 +1283,7 @@ def where(self, cond, other=dtypes.NA, drop: bool = False):

return ops.where_method(self, cond, other)

def set_close(self, close: Optional[Callable[[], None]]) -> None:
def set_close(self, close: Callable[[], None] | None) -> None:
"""Register the function that releases any resources linked to this object.

This method controls how xarray cleans up resources associated
Expand Down Expand Up @@ -1523,20 +1516,20 @@ def __getitem__(self, value):

@overload
def full_like(
other: "Dataset",
other: Dataset,
fill_value,
dtype: Union[DTypeLike, Mapping[Any, DTypeLike]] = None,
) -> "Dataset":
dtype: DTypeLike | Mapping[Any, DTypeLike] = None,
) -> Dataset:
...


@overload
def full_like(other: "DataArray", fill_value, dtype: DTypeLike = None) -> "DataArray":
def full_like(other: DataArray, fill_value, dtype: DTypeLike = None) -> DataArray:
...


@overload
def full_like(other: "Variable", fill_value, dtype: DTypeLike = None) -> "Variable":
def full_like(other: Variable, fill_value, dtype: DTypeLike = None) -> Variable:
...


Expand Down Expand Up @@ -1815,9 +1808,9 @@ def ones_like(other, dtype: DTypeLike = None):

def get_chunksizes(
variables: Iterable[Variable],
) -> Mapping[Any, Tuple[int, ...]]:
) -> Mapping[Any, tuple[int, ...]]:

chunks: Dict[Any, Tuple[int, ...]] = {}
chunks: dict[Any, tuple[int, ...]] = {}
for v in variables:
if hasattr(v.data, "chunks"):
for dim, c in v.chunksizes.items():
Expand Down
Loading