Skip to content

Lint with ruff #7458

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 30, 2023
Merged
29 changes: 5 additions & 24 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,12 @@ repos:
- id: absolufy-imports
name: absolufy-imports
files: ^xarray/
# This wants to go before isort & flake8
- repo: https://github.com/PyCQA/autoflake
rev: "v2.0.0"
- repo: https://github.com/charliermarsh/ruff-pre-commit
# Ruff version.
rev: 'v0.0.235'
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.11.4
hooks:
- id: isort
- repo: https://github.com/asottile/pyupgrade
rev: v3.3.1
hooks:
- id: pyupgrade
args:
- "--py39-plus"
- id: ruff
args: ["--fix"]
# https://github.com/python/black#version-control-integration
- repo: https://github.com/psf/black
rev: 22.12.0
Expand All @@ -43,15 +33,6 @@ repos:
exclude: "generate_aggregations.py"
additional_dependencies: ["black==22.12.0"]
- id: blackdoc-autoupdate-black
- repo: https://github.com/PyCQA/flake8
rev: 6.0.0
hooks:
- id: flake8
# - repo: https://github.com/Carreau/velin
# rev: 0.0.8
# hooks:
# - id: velin
# args: ["--write", "--compact"]
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.991
hooks:
Expand Down
5 changes: 2 additions & 3 deletions ci/min_deps_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import sys
from collections.abc import Iterator
from datetime import datetime
from typing import Optional

import conda.api # type: ignore[import]
import yaml
Expand Down Expand Up @@ -45,7 +44,7 @@ def warning(msg: str) -> None:
print("WARNING:", msg)


def parse_requirements(fname) -> Iterator[tuple[str, int, int, Optional[int]]]:
def parse_requirements(fname) -> Iterator[tuple[str, int, int, int | None]]:
"""Load requirements/py37-min-all-deps.yml

Yield (package name, major version, minor version, [patch version])
Expand Down Expand Up @@ -116,7 +115,7 @@ def metadata(entry):


def process_pkg(
pkg: str, req_major: int, req_minor: int, req_patch: Optional[int]
pkg: str, req_major: int, req_minor: int, req_patch: int | None
) -> tuple[str, str, str, str, str, str]:
"""Compare package version from requirements file to available versions in conda.
Return row to build pandas dataframe:
Expand Down
31 changes: 31 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,34 @@ module = [
[[tool.mypy.overrides]]
ignore_errors = true
module = []

[tool.ruff]
target-version = "py39"
builtins = ["ellipsis"]
exclude = [
".eggs",
"doc",
"_typed_ops.pyi",
]
# E402: module level import not at top of file
# E501: line too long - let black worry about that
# E731: do not assign a lambda expression, use a def
ignore = [
"E402",
"E501",
"E731",
]
select = [
# Pyflakes
"F",
# Pycodestyle
"E",
"W",
# isort
"I",
# Pyupgrade
"UP",
]

[tool.ruff.isort]
known-first-party = ["xarray"]
21 changes: 0 additions & 21 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -145,27 +145,6 @@ markers =
network: tests requiring a network connection
slow: slow tests

[flake8]
ignore =
# E203: whitespace before ':' - doesn't work well with black
# E402: module level import not at top of file
# E501: line too long - let black worry about that
# E731: do not assign a lambda expression, use a def
# W503: line break before binary operator
E203, E402, E501, E731, W503
exclude =
.eggs
doc
builtins =
ellipsis

[isort]
profile = black
skip_gitignore = true
float_to_top = true
default_section = THIRDPARTY
known_first_party = xarray

[aliases]
test = pytest

Expand Down
10 changes: 8 additions & 2 deletions xarray/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,15 @@

from xarray.coding.calendar_ops import convert_calendar, interp_calendar
from xarray.coding.cftimeindex import CFTimeIndex, _parse_array_of_cftime_strings
from xarray.core import alignment
from xarray.core import (
alignment,
duck_array_ops,
formatting,
formatting_html,
ops,
utils,
)
from xarray.core import dtypes as xrdtypes
from xarray.core import duck_array_ops, formatting, formatting_html, ops, utils
from xarray.core._aggregations import DatasetAggregations
from xarray.core.alignment import (
_broadcast_helper,
Expand Down
6 changes: 3 additions & 3 deletions xarray/core/duck_array_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@
import pandas as pd
from numpy import all as array_all # noqa
from numpy import any as array_any # noqa
from numpy import around # noqa
from numpy import zeros_like # noqa
from numpy import concatenate as _concatenate
from numpy import ( # noqa
around, # noqa
einsum,
gradient,
isclose,
Expand All @@ -29,7 +27,9 @@
tensordot,
transpose,
unravel_index,
zeros_like, # noqa
)
from numpy import concatenate as _concatenate
from numpy.lib.stride_tricks import sliding_window_view # noqa

from xarray.core import dask_array_ops, dtypes, nputils
Expand Down
33 changes: 14 additions & 19 deletions xarray/tests/test_distributed.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,58 +2,53 @@
from __future__ import annotations

import pickle
import numpy as np

from typing import Any, TYPE_CHECKING
from typing import TYPE_CHECKING, Any

import numpy as np
import pytest
from packaging.version import Version

if TYPE_CHECKING:
import dask
import dask.array as da
import distributed
else:
dask = pytest.importorskip("dask")
da = pytest.importorskip("dask.array")
distributed = pytest.importorskip("distributed")

from dask.distributed import Client, Lock
from distributed.client import futures_of
from distributed.utils_test import ( # noqa: F401
cleanup,
cluster,
gen_cluster,
loop,
cleanup,
loop_in_thread,
)

import xarray as xr
from xarray.backends.locks import HDF5_LOCK, CombinedLock
from xarray.tests.test_backends import (
ON_WINDOWS,
create_tmp_file,
create_tmp_geotiff,
open_example_dataset,
)
from xarray.tests.test_dataset import create_test_data

from xarray.tests import (
assert_allclose,
assert_identical,
has_h5netcdf,
has_netCDF4,
requires_rasterio,
has_scipy,
requires_zarr,
requires_cfgrib,
requires_cftime,
requires_netCDF4,
requires_rasterio,
requires_zarr,
)
from xarray.tests.test_backends import (
ON_WINDOWS,
create_tmp_file,
create_tmp_geotiff,
open_example_dataset,
)
from xarray.tests.test_dataset import create_test_data

# this is to stop isort throwing errors. May have been easier to just use
# `isort:skip` in retrospect


da = pytest.importorskip("dask.array")
loop = loop # loop is an imported fixture, which flake8 has issues ack-ing


Expand Down
3 changes: 1 addition & 2 deletions xarray/tests/test_ufuncs.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
import pytest

import xarray as xr
from xarray.tests import assert_array_equal
from xarray.tests import assert_array_equal, mock
from xarray.tests import assert_identical as assert_identical_
from xarray.tests import mock


def assert_identical(a, b):
Expand Down