Skip to content

Commit

Permalink
Switch to ruff for linting and autoformatting
Browse files Browse the repository at this point in the history
  • Loading branch information
djhoese committed Nov 24, 2023
1 parent fb52e49 commit 2910aec
Show file tree
Hide file tree
Showing 18 changed files with 54 additions and 82 deletions.
20 changes: 5 additions & 15 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,22 +1,12 @@
exclude: '^$'
fail_fast: false
repos:
- repo: https://github.com/psf/black
rev: 23.11.0 # Replace by any tag/version: https://github.com/psf/black/tags
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: 'v0.1.6'
hooks:
- id: black
language_version: python3 # Should be a command that runs python3.6+
- repo: https://github.com/pycqa/isort
rev: 5.12.0
hooks:
- id: isort
language_version: python3
- repo: https://github.com/PyCQA/flake8
rev: 6.1.0
hooks:
- id: flake8
additional_dependencies: [flake8-docstrings, flake8-debugger, flake8-bugbear, mccabe]
args: [--max-complexity, "10"]
- id: ruff
args: ["--fix"]
- id: ruff-format
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
hooks:
Expand Down
2 changes: 1 addition & 1 deletion doc/source/toctree_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def filter_entries(self, entries):
for e in entries:
m = self.hasPat.match(e)
if m is not None:
if not m.groups()[0] in excl:
if m.groups()[0] not in excl:
filtered.append(m.groups()[1])
else:
filtered.append(e)
Expand Down
14 changes: 8 additions & 6 deletions polar2grid/_glue_argparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ def _separate_scene_init_load_args(self, reader_subgroups) -> None:
def _parse_reader_args(self, reader_subgroups: list) -> tuple[dict, dict]:
reader_args = {}
load_args = {}
for reader_name, (sgrp1, sgrp2) in zip(self._reader_names, reader_subgroups):
for reader_name, (sgrp1, sgrp2) in zip(self._reader_names, reader_subgroups, strict=True):
if sgrp1 is None:
continue
rargs = _args_to_dict(self._args, sgrp1._group_actions)
Expand All @@ -175,7 +175,7 @@ def _parse_reader_args(self, reader_subgroups: list) -> tuple[dict, dict]:
def _parse_one_writer_args(self, writer_subgroups: list) -> dict:
writer_names: list[str] = self._writer_args["writers"]
writer_specific_args = {}
for writer_name, (sgrp1, sgrp2) in zip(writer_names, writer_subgroups):
for writer_name, (sgrp1, sgrp2) in zip(writer_names, writer_subgroups, strict=True):
wargs = _args_to_dict(self._args, sgrp1._group_actions)
if sgrp2 is not None:
wargs.update(_args_to_dict(self._args, sgrp2._group_actions))
Expand Down Expand Up @@ -283,8 +283,9 @@ def _validate_reader_writer_args(parser, args, use_polar2grid_defaults):
parser.print_usage()
parser.exit(
1,
"\nERROR: Reader must be provided (-r flag).\n"
"Supported readers:\n\t{}\n".format("\n\t".join(_supported_readers(use_polar2grid_defaults))),
"\nERROR: Reader must be provided (-r flag).\n" "Supported readers:\n\t{}\n".format(
"\n\t".join(_supported_readers(use_polar2grid_defaults))
),
)
elif len(args.readers) > 1:
parser.print_usage()
Expand All @@ -297,8 +298,9 @@ def _validate_reader_writer_args(parser, args, use_polar2grid_defaults):
parser.print_usage()
parser.exit(
1,
"\nERROR: Writer must be provided (-w flag) with one or more writer.\n"
"Supported writers:\n\t{}\n".format("\n\t".join(_supported_writers(use_polar2grid_defaults))),
"\nERROR: Writer must be provided (-w flag) with one or more writer.\n" "Supported writers:\n\t{}\n".format(
"\n\t".join(_supported_writers(use_polar2grid_defaults))
),
)


Expand Down
6 changes: 3 additions & 3 deletions polar2grid/add_coastlines.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ def main(argv=sys.argv[1:]):
# gather all options into a single dictionary that we can pass to pycoast
pycoast_options = _args_to_pycoast_dict(args)
colorbar_kwargs = _args_to_colorbar_kwargs(args) if args.add_colorbar else {}
for input_tiff, output_filename in zip(args.input_tiff, args.output_filename):
for input_tiff, output_filename in zip(args.input_tiff, args.output_filename, strict=True):
_process_one_image(input_tiff, output_filename, pycoast_options, args.shapes_dir, colorbar_kwargs)
return 0

Expand Down Expand Up @@ -484,10 +484,10 @@ def find_font(font_name, size):
try:
font = ImageFont.truetype(font_name, size)
return font.path
except IOError:
except IOError as err:
font_path = get_resource_filename("polar2grid.fonts", font_name)
if not os.path.exists(font_path):
raise ValueError("Font path does not exist: {}".format(font_path))
raise ValueError("Font path does not exist: {}".format(font_path)) from err
return font_path


Expand Down
6 changes: 6 additions & 0 deletions polar2grid/compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ def isclose_array(array1, array2, atol=0.0, rtol=0.0, margin_of_error=0.0, **kwa
Args:
array1: numpy array for comparison
array2: numpy array for comparison
atol: absolute tolerance (see numpy ``isclose``)
rtol: relative tolerance (see numpy ``isclose``)
margin_of_error: percentage of pixels that can be different and still
be considered a passing amount.
kwargs: Unused.
Returns:
1 if more than margin_of_error pixels are different, 0 otherwise.
Expand Down
8 changes: 4 additions & 4 deletions polar2grid/core/dtype.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ def str_to_dtype(dtype_str):

try:
return str2dtype[dtype_str]
except KeyError:
raise ValueError("Not a valid data type string: %s" % (dtype_str,))
except KeyError as err:
raise ValueError("Not a valid data type string: %s" % (dtype_str,)) from err


def dtype_to_str(numpy_dtype):
Expand All @@ -114,8 +114,8 @@ def dtype_to_str(numpy_dtype):

try:
return dtype2str[np.dtype(numpy_dtype).type]
except KeyError:
raise ValueError("Unsupported np data type: %r" % (numpy_dtype,))
except KeyError as err:
raise ValueError("Unsupported np data type: %r" % (numpy_dtype,)) from err


def clip_to_data_type(data, data_type):
Expand Down
6 changes: 1 addition & 5 deletions polar2grid/filters/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,7 @@
from typing import Union

from pyresample.boundary import AreaBoundary, AreaDefBoundary, Boundary
from pyresample.geometry import (
AreaDefinition,
SwathDefinition,
get_geostationary_bounding_box,
)
from pyresample.geometry import AreaDefinition, SwathDefinition, get_geostationary_bounding_box
from pyresample.spherical import SphPolygon

logger = logging.getLogger(__name__)
Expand Down
12 changes: 4 additions & 8 deletions polar2grid/glue.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,7 @@
from satpy.writers import compute_writer_results

from polar2grid._glue_argparser import GlueArgumentParser, get_p2g_defaults_env_var
from polar2grid.core.script_utils import (
create_exc_handler,
rename_log_file,
setup_logging,
)
from polar2grid.core.script_utils import create_exc_handler, rename_log_file, setup_logging
from polar2grid.filters import filter_scene
from polar2grid.readers._base import ReaderProxyBase
from polar2grid.resample import resample_scene
Expand Down Expand Up @@ -150,7 +146,7 @@ def _write_scene_with_writer(scn: Scene, writer_name: str, data_ids: list[DataID
res = scn.save_datasets(writer=writer_name, compute=False, datasets=data_ids, **wargs)
if res and isinstance(res[0], (tuple, list)):
# list of (dask-array, file-obj) tuples
to_save.extend(zip(*res))
to_save.extend(zip(*res, strict=True))
else:
# list of delayed objects
to_save.extend(res)
Expand Down Expand Up @@ -472,12 +468,12 @@ def _persist_swath_definition_in_scene(scn: Scene) -> None:
if not to_persist_swath_defs:
return scn

to_update_data_arrays, to_persist_lonlats = zip(*to_persist_swath_defs.values())
to_update_data_arrays, to_persist_lonlats = zip(*to_persist_swath_defs.values(), strict=True)
LOG.info("Loading swath geolocation into memory...")
persisted_lonlats = dask.persist(*to_persist_lonlats)
persisted_swath_defs = [SwathDefinition(plons, plats) for plons, plats in persisted_lonlats]
new_scn = scn.copy()
for arrays_to_update, persisted_swath_def in zip(to_update_data_arrays, persisted_swath_defs):
for arrays_to_update, persisted_swath_def in zip(to_update_data_arrays, persisted_swath_defs, strict=True):
for array_to_update in arrays_to_update:
array_to_update.attrs["area"] = persisted_swath_def
new_scn._datasets[array_to_update.attrs["_satpy_id"]] = array_to_update
Expand Down
6 changes: 4 additions & 2 deletions polar2grid/resample/resample_decisions.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ def find_match(self, **query_dict):

try:
return super().find_match(**query_dict)
except KeyError:
except KeyError as err:
# give a more understandable error message
raise KeyError(f"No resampling configuration found for {query_dict['area_type']=} | {query_dict['name']=}")
raise KeyError(
f"No resampling configuration found for {query_dict['area_type']=} | {query_dict['name']=}"
) from err
4 changes: 2 additions & 2 deletions polar2grid/tests/etc/enhancements/generic.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ enhancements:
method: !!python/name:polar2grid.enhancements.palettize
kwargs:
palettes:
- filename: $POLAR2GRID_HOME/../etc/colormaps/amsr2_36h.cmap
- filename: $POLAR2GRID_HOME/../polar2grid/etc/colormaps/amsr2_36h.cmap
min_value: 180
max_value: 280
test_p2g_palettize2:
Expand Down Expand Up @@ -62,6 +62,6 @@ enhancements:
method: !!python/name:polar2grid.enhancements.colorize
kwargs:
palettes:
- filename: $POLAR2GRID_HOME/../etc/colormaps/amsr2_36h.cmap
- filename: $POLAR2GRID_HOME/../polar2grid/etc/colormaps/amsr2_36h.cmap
min_value: 180
max_value: 280
1 change: 1 addition & 0 deletions polar2grid/tests/test_enhancements.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def setup_method(self):
add_polar2grid_config_paths()
# add test specific configs
curr_path = satpy.config.get("config_path")
print(curr_path, TEST_ETC_DIR)
satpy.config.set(config_path=[TEST_ETC_DIR] + curr_path)

def teardown_method(self):
Expand Down
2 changes: 1 addition & 1 deletion polar2grid/tests/test_utils/test_convert_grids_conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,5 @@ def test_conf_conversion(tmpdir, capsys, conf_content, num_areas, area_types):
s.seek(0)
areas = parse_area_file([s])
assert len(areas) == num_areas
for area_obj, area_type in zip(areas, area_types):
for area_obj, area_type in zip(areas, area_types, strict=True):
assert isinstance(area_obj, area_type)
4 changes: 2 additions & 2 deletions polar2grid/utils/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
def get_polar2grid_etc():
p2g_pkg_location = impr.files("polar2grid")
if _is_editable_installation():
return str(p2g_pkg_location.parent / "etc")
return os.path.join(sys.prefix, "etc", "polar2grid")
return str(p2g_pkg_location / "etc")
return p2g_pkg_location / "etc" / "polar2grid"


def _is_editable_installation():
Expand Down
6 changes: 3 additions & 3 deletions polar2grid/utils/legacy_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def remove_unknown_user_products(
"""
satpy_names = self.convert_p2g_name_to_satpy(self._user_products)
new_user_products = []
for user_name, satpy_name in zip(self._user_products, satpy_names):
for user_name, satpy_name in zip(self._user_products, satpy_names, strict=True):
# convert DataID/DataQuery to string
satpy_name = satpy_name if isinstance(satpy_name, str) else satpy_name["name"]
if satpy_name not in known_dataset_names:
Expand Down Expand Up @@ -212,7 +212,7 @@ def apply_p2g_name_to_scene(
"""
all_ids = list(scn.keys())
all_p2g_names = list(self.convert_satpy_to_p2g_name(all_ids))
for data_id, p2g_name in zip(all_ids, all_p2g_names):
for data_id, p2g_name in zip(all_ids, all_p2g_names, strict=True):
if p2g_name is None:
# the Satpy ID doesn't have a Polar2Grid compatible name
logger.debug("Satpy DataID %s does not have a compatible polar2grid name.", data_id)
Expand All @@ -225,7 +225,7 @@ def available_product_names(
) -> tuple[list[str], list[str], list[str]]:
"""Get separate lists of available Satpy products and Polar2Grid products."""
available_ids_as_p2g_names = list(self.convert_satpy_to_p2g_name(available_satpy_ids, all_p2g_products))
satpy_id_to_p2g_name = dict(zip(available_satpy_ids, available_ids_as_p2g_names))
satpy_id_to_p2g_name = dict(zip(available_satpy_ids, available_ids_as_p2g_names, strict=True))
available_p2g_names = []
available_custom_names = []
available_satpy_names = []
Expand Down
8 changes: 1 addition & 7 deletions polar2grid/writers/binary.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,7 @@
import xarray as xr
from satpy.writers import ImageWriter, get_enhanced_image

from polar2grid.core.dtype import (
NUMPY_DTYPE_STRS,
clip_to_data_type,
dtype_to_str,
int_or_float,
str_to_dtype,
)
from polar2grid.core.dtype import NUMPY_DTYPE_STRS, clip_to_data_type, dtype_to_str, int_or_float, str_to_dtype
from polar2grid.core.script_utils import NumpyDtypeList
from polar2grid.utils.legacy_compat import convert_p2g_pattern_to_satpy

Expand Down
1 change: 1 addition & 0 deletions polar2grid/writers/hdf5.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ def iter_by_area(self, datasets: list[xr.DataArray]):
Args:
datasets (list[xr.DataArray]): A list of dataArray objects stored in Scene.
Returns:
dictionary: a dictionary of {AreaDef: list[xr.DataArray]}
"""
Expand Down
24 changes: 7 additions & 17 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,36 +56,26 @@ include-package-data = true
[tool.setuptools.packages]
find = {}

[tool.isort]
sections = ["FUTURE", "STDLIB", "THIRDPARTY", "FIRSTPARTY", "LOCALFOLDER"]
profile = "black"
skip_gitignore = true
force_to_top = true
default_section = "THIRDPARTY"
known_first_party = "polar2grid"
line_length = 120

[tool.black]
line-length = 120
target-version = ['py310']
include = '\.pyi?$'

[tool.coverage.run]
relative_files = true

[tool.ruff]
# See https://docs.astral.sh/ruff/rules/
select = ["E", "W", "B", "D", "T10", "C90"]
# Remove D416 when all docstrings have been converted to google-style
ignore = ["D107", "D416"]
ignore = ["D101", "D102", "D103", "D104", "D105", "D106", "D107", "E203", "D416"]
line-length = 120
exclude = ["versioneer.py", "pyresample/version.py"]

[tool.ruff.per-file-ignores]
"pyresample/test/*.py" = ["D102", "S101"] # assert allowed in tests
"doc/source/conf.py" = ["E501"]
"polar2grid/readers/*.py" = ["D205", "D400", "D415", "S101"] # assert allowed in tests

[tool.ruff.pydocstyle]
convention = "google"

[tool.ruff.mccabe]
max-complexity = 10

[tool.ruff.lint.isort]
known-first-party = ["polar2grid"]
known-third-party = ["src"]
6 changes: 0 additions & 6 deletions setup.cfg

This file was deleted.

0 comments on commit 2910aec

Please sign in to comment.