From eb1a866d9ef16655edd3162c39506a7dd7d510cd Mon Sep 17 00:00:00 2001 From: Cagtay Fabry Date: Thu, 4 Jul 2024 14:45:20 +0200 Subject: [PATCH] use ruff.toml and lint --- .pre-commit-config.yaml | 3 ++ .ruff.toml | 62 ++++++++++++++++++++++++++ pyproject.toml | 62 -------------------------- weldx_widgets/visualization/colors.py | 49 ++++++++++---------- weldx_widgets/visualization/csm_mpl.py | 6 +-- weldx_widgets/visualization/types.py | 6 +-- weldx_widgets/widget_evaluate.py | 2 +- weldx_widgets/widget_gas.py | 4 +- weldx_widgets/widget_measurement.py | 4 +- 9 files changed, 99 insertions(+), 99 deletions(-) create mode 100644 .ruff.toml diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index c1c001e..ca8ae4e 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -23,5 +23,8 @@ repos: hooks: # Run the linter. - id: ruff + args: + - --quiet + - --fix # Run the formatter. - id: ruff-format diff --git a/.ruff.toml b/.ruff.toml new file mode 100644 index 0000000..4277b18 --- /dev/null +++ b/.ruff.toml @@ -0,0 +1,62 @@ +# Exclude a variety of commonly ignored directories. +extend-exclude = [ + "__init__.py", +] + +# Same as Black. +line-length = 88 +indent-width = 4 + +# Assume Python 3.9 +target-version = "py39" + + +[lint] +# TODO: should be the following list, but Ruff does not yet impl all of them. +# W503,W504 +# E203 +# C408 +ignore = [ + "C408", + #"E203", + "E402", + #"W503", + #"W504", + "D203", + "D211", + "D213", + "UP006", + "UP007", # see ruff GH#4427 +] +select = [ + "B", # flake8-bugbear + "C", # flake8-comprehensions + #"D", # note: all relevant D's will be set by setting pydocstyle.convention=numpy! + "E", # pycodestyles + "F", # pyflakes + "W", # pycodestyle warnings + "UP", # pyupgrade + "T2", # flake8-print + "I001", # isort + "ICN", # import conventions, e.g. import numpy as np + #"B950", # not yet implemented by Ruff. + "RUF100", # ensure 'noqa' declarations are still valid. +] + +[lint.pydocstyle] +convention = "numpy" + +[lint.mccabe] +max-complexity = 15 # max branches inside a function. + +[lint.isort] +known-first-party = [ + "weldx", + "weldx_widgets", +] +required-imports = [ + "from __future__ import annotations", +] + +[flake8-import-conventions] +extend-aliases = { xarray = "xr" } diff --git a/pyproject.toml b/pyproject.toml index d73d791..35187ce 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -89,65 +89,3 @@ exclude_lines = [ # Have to re-enable the standard pragma "pragma: no cover", ] - -[tool.ruff] -target-version = "py38" # designated Python version -exclude = [ - "__init__.py", - "doc/src/conf.py", -] -# TODO: should be the following list, but Ruff does not yet impl all of them. -# W503,W504 -# E203 -# C408 -ignore = [ - "C408", - #"E203", - "E402", - #"W503", - #"W504", - "D203", "D211", "D213" -] -line-length = 88 -select = [ - "B", # flake8-bugbear - "C", # flake8-comprehensions - #"D", # note: all relevant D's will be set by setting pydocstyle.convention=numpy! - "E", # pycodestyles - "F", # pyflakes - "W", # pycodestyle warnings - "UP", # pyupgrade - "T2", # flake8-print - "I001", # isort - "ICN", # import conventions, e.g. import numpy as np - #"B950", # not yet implemented by Ruff. - "RUF100", # ensure 'noqa' declarations are still valid. -] - -# Allow pydocstyle violations in certain areas. -per-file-ignores."**/{tests,tags,asdf,devtools}/**" = ["D"] -per-file-ignores."conftest.py" = ["D"] -per-file-ignores."doc/src/tutorials/*" = ["D"] -per-file-ignores."doc/src/conf.py" = ["E501", # ignore long lines. - "RUF100", # do no check if 'noqa' is needed (circular import workaround) -] -# Allow prints in certain areas. -per-file-ignores."**/{cli,tests,tutorials,devtools}/**/*{.py,ipynb}" = ["T2"] - -external = ["B950"] -pydocstyle = {convention = "numpy"} -pyupgrade = {keep-runtime-typing = true} - -mccabe = {max-complexity = 15} # max branches inside a function. - -[tool.ruff.isort] -known-first-party = ["weldx"] -required-imports = ["from __future__ import annotations"] - -[tool.ruff.flake8-import-conventions] -extend-aliases = {xarray = "xr"} - -[tool.nbqa.addopts] -ruff = [ - "--extend-ignore=B018" -] diff --git a/weldx_widgets/visualization/colors.py b/weldx_widgets/visualization/colors.py index f7e7c89..74807bf 100644 --- a/weldx_widgets/visualization/colors.py +++ b/weldx_widgets/visualization/colors.py @@ -1,6 +1,7 @@ """Color related tools.""" -from typing import Dict, Generator, List, Sized, Tuple, Union +from collections.abc import Generator, Sized +from typing import Union import matplotlib.pyplot as plt import numpy as np @@ -15,12 +16,12 @@ RGB_GREY = 0x999999 -def _color_rgb_to_int(rgb_color_tuple: Tuple[int, int, int]) -> int: +def _color_rgb_to_int(rgb_color_tuple: tuple[int, int, int]) -> int: """Convert an RGB color tuple to an 24 bit integer. Parameters ---------- - rgb_color_tuple : Tuple[int, int, int] + rgb_color_tuple : tuple[int, int, int] The color as RGB tuple. Values must be in the range 0-255. Returns @@ -32,7 +33,7 @@ def _color_rgb_to_int(rgb_color_tuple: Tuple[int, int, int]) -> int: return int("0x{:02x}{:02x}{:02x}".format(*rgb_color_tuple), 0) -def _color_int_to_rgb(integer: int) -> Tuple[int, int, int]: +def _color_int_to_rgb(integer: int) -> tuple[int, int, int]: """Convert an 24 bit integer into a RGB color tuple with the value range (0-255). Parameters @@ -42,7 +43,7 @@ def _color_int_to_rgb(integer: int) -> Tuple[int, int, int]: Returns ------- - Tuple[int, int, int]: + tuple[int, int, int]: The resulting RGB tuple. """ @@ -50,18 +51,18 @@ def _color_int_to_rgb(integer: int) -> Tuple[int, int, int]: def _color_rgb_to_rgb_normalized( - rgb: Tuple[int, int, int], -) -> Tuple[float, float, float]: + rgb: tuple[int, int, int], +) -> tuple[float, float, float]: """Normalize an RGB color tuple with the range (0-255) to the range (0.0-1.0). Parameters ---------- - rgb : Tuple[int, int, int] + rgb : tuple[int, int, int] Color tuple with values in the range (0-255) Returns ------- - Tuple[float, float, float] : + tuple[float, float, float] : Color tuple with values in the range (0.0-1.0) """ @@ -69,18 +70,18 @@ def _color_rgb_to_rgb_normalized( def _color_rgb_normalized_to_rgb( - rgb: Tuple[float, float, float], -) -> Tuple[int, int, int]: + rgb: tuple[float, float, float], +) -> tuple[int, int, int]: """Normalize an RGB color tuple with the range (0.0-1.0) to the range (0-255). Parameters ---------- - rgb : Tuple[float, float, float] + rgb : tuple[float, float, float] Color tuple with values in the range (0.0-1.0) Returns ------- - Tuple[int, int, int] : + tuple[int, int, int] : Color tuple with values in the range (0-255) """ @@ -97,7 +98,7 @@ def color_int_to_rgb_normalized(integer): Returns ------- - Tuple[float, float, float]: + tuple[float, float, float]: The resulting RGB tuple. """ @@ -105,12 +106,12 @@ def color_int_to_rgb_normalized(integer): return _color_rgb_to_rgb_normalized(rgb) -def _color_rgb_normalized_to_int(rgb: Tuple[float, float, float]) -> int: +def _color_rgb_normalized_to_int(rgb: tuple[float, float, float]) -> int: """Convert a normalized RGB color tuple to an 24 bit integer. Parameters ---------- - rgb : Tuple[float, float, float] + rgb : tuple[float, float, float] The color as RGB tuple. Values must be in the range 0.0-1.0. Returns @@ -122,13 +123,13 @@ def _color_rgb_normalized_to_int(rgb: Tuple[float, float, float]) -> int: return _color_rgb_to_int(_color_rgb_normalized_to_rgb(rgb)) -def _shuffled_tab20_colors() -> List[int]: +def _shuffled_tab20_colors() -> list[int]: """Get a shuffled list of matplotlib 'tab20' colors. Returns ------- - List[int] : - List of colors + list[int] : + list of colors """ num_colors = 20 @@ -143,20 +144,20 @@ def _shuffled_tab20_colors() -> List[int]: def color_to_rgb_normalized( - color: Union[int, Tuple[int, int, int], Tuple[float, float, float]], -) -> Tuple[float, float, float]: + color: Union[int, tuple[int, int, int], tuple[float, float, float]], +) -> tuple[float, float, float]: """Convert an arbitrary RGB color representation into a normalized RGB triplet. Parameters ---------- - color : Union[int, Tuple[int, int, int], Tuple[float, float, float]] + color : Union[int, tuple[int, int, int], tuple[float, float, float]] A 24 bit integer, a triplet of integers with a value range of 0-255 or a triplet of floats with a value range of 0.0-1.0 that represent an RGB color. Returns ------- - Tuple[float, float, float] : + tuple[float, float, float] : RGB color triplet with the value range 0.0 to 1.0 """ @@ -198,7 +199,7 @@ def color_generator_function() -> Generator: def get_color( key: str, - color_dict: Dict[str, Union[int, Tuple[int, int, int]]], + color_dict: dict[str, Union[int, tuple[int, int, int]]], color_generator: Generator, ) -> int: """Get a 24 bit RGB color from a dictionary or generator function. diff --git a/weldx_widgets/visualization/csm_mpl.py b/weldx_widgets/visualization/csm_mpl.py index 470d093..beaf721 100644 --- a/weldx_widgets/visualization/csm_mpl.py +++ b/weldx_widgets/visualization/csm_mpl.py @@ -2,7 +2,7 @@ from __future__ import annotations -from typing import TYPE_CHECKING, Any, List, Union +from typing import TYPE_CHECKING, Any, Union import matplotlib.pyplot as plt import numpy as np @@ -156,7 +156,7 @@ def draw_coordinate_system_matplotlib( tips = dsx.orientation else: if not isinstance(scale_vectors, np.ndarray): - if isinstance(scale_vectors, List): + if isinstance(scale_vectors, list): scale_vectors = np.array(scale_vectors) else: scale_vectors = np.array([scale_vectors for _ in range(3)]) @@ -296,7 +296,7 @@ def _set_limits_matplotlib( """ if limits is not None: - if not isinstance(limits, List): + if not isinstance(limits, list): limits = [limits] if len(limits) == 1: limits = [limits[0] for _ in range(3)] diff --git a/weldx_widgets/visualization/types.py b/weldx_widgets/visualization/types.py index 3e0080b..9a71fc9 100644 --- a/weldx_widgets/visualization/types.py +++ b/weldx_widgets/visualization/types.py @@ -1,10 +1,10 @@ """Type aliases shared in visualization package.""" -from typing import List, Tuple, Union +from typing import Union import pandas as pd -types_timeindex = Union[pd.DatetimeIndex, pd.TimedeltaIndex, List[pd.Timestamp]] -types_limits = Union[List[Tuple[float, float]], Tuple[float, float]] +types_timeindex = Union[pd.DatetimeIndex, pd.TimedeltaIndex, list[pd.Timestamp]] +types_limits = Union[list[tuple[float, float]], tuple[float, float]] __all__ = ("types_timeindex", "types_limits") diff --git a/weldx_widgets/widget_evaluate.py b/weldx_widgets/widget_evaluate.py index 97de893..4719ab6 100644 --- a/weldx_widgets/widget_evaluate.py +++ b/weldx_widgets/widget_evaluate.py @@ -133,7 +133,7 @@ def make_output(): scans_available = True try: foo = [ - _clean_nans_from_spatial_data(csm.get_data("scan_%s" % i)) + _clean_nans_from_spatial_data(csm.get_data(f"scan_{i}")) for i in range(0, 2) ] assert len(foo) == 2 diff --git a/weldx_widgets/widget_gas.py b/weldx_widgets/widget_gas.py index 6bfa93d..0b69b09 100644 --- a/weldx_widgets/widget_gas.py +++ b/weldx_widgets/widget_gas.py @@ -1,7 +1,5 @@ """Widgets to handle shielding gas selection.""" -from typing import List - from bidict import bidict from ipywidgets import Button, Dropdown, HBox, IntSlider, Layout, Output @@ -119,7 +117,7 @@ def to_tree(self) -> dict: return dict(gas_component=gas_components) def from_tree(self, tree): - gc_list: List[GasComponent] = tree["gas_component"] + gc_list: list[GasComponent] = tree["gas_component"] self._clear() for gc in gc_list: # create widget for gas element with percentage, then add to components dict diff --git a/weldx_widgets/widget_measurement.py b/weldx_widgets/widget_measurement.py index d67fca1..019ddea 100644 --- a/weldx_widgets/widget_measurement.py +++ b/weldx_widgets/widget_measurement.py @@ -1,7 +1,5 @@ """Widget to wrap around a measurement.""" -from typing import List - from matplotlib import pylab as plt import weldx @@ -62,7 +60,7 @@ def plot_measurements( class WidgetMeasurement(WidgetSimpleOutput): """Widget to wrap around a measurement.""" - def __init__(self, measurements: List["weldx.measurement.Measurement"], out=None): + def __init__(self, measurements: list["weldx.measurement.Measurement"], out=None): super().__init__(out=out) n = len(measurements)