From 6b500963c90dd6287def6bb32895d77a4977b8f1 Mon Sep 17 00:00:00 2001 From: Malte Franken Date: Wed, 31 Jan 2024 20:44:55 +1100 Subject: [PATCH] Add ruff (#25) * add ruff * code quality improvements * compatibility fix * compatibility fix --- .pre-commit-config.yaml | 6 +++ .../__init__.py | 4 +- .../consts.py | 6 +-- .../feed.py | 4 +- .../feed_entry.py | 12 +++--- .../feed_manager.py | 4 +- pyproject.toml | 39 +++++++++++++++++++ 7 files changed, 59 insertions(+), 16 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 1da4157..3907721 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,4 +1,10 @@ repos: + - repo: https://github.com/astral-sh/ruff-pre-commit + rev: v0.1.11 + hooks: + - id: ruff + args: + - --fix - repo: https://github.com/psf/black rev: 23.12.1 hooks: diff --git a/georss_ingv_centro_nazionale_terremoti_client/__init__.py b/georss_ingv_centro_nazionale_terremoti_client/__init__.py index a191281..1e31c6e 100644 --- a/georss_ingv_centro_nazionale_terremoti_client/__init__.py +++ b/georss_ingv_centro_nazionale_terremoti_client/__init__.py @@ -1,3 +1,3 @@ """INGV Centro Nazionale Terremoti (Earthquakes) library.""" -from .feed import IngvCentroNazionaleTerremotiFeed # noqa -from .feed_manager import IngvCentroNazionaleTerremotiFeedManager # noqa +from .feed import IngvCentroNazionaleTerremotiFeed # noqa: F401 +from .feed_manager import IngvCentroNazionaleTerremotiFeedManager # noqa: F401 diff --git a/georss_ingv_centro_nazionale_terremoti_client/consts.py b/georss_ingv_centro_nazionale_terremoti_client/consts.py index 52940ee..fa35907 100644 --- a/georss_ingv_centro_nazionale_terremoti_client/consts.py +++ b/georss_ingv_centro_nazionale_terremoti_client/consts.py @@ -5,12 +5,10 @@ "http://shakemap.rm.ingv.it/shake4/data/{}/current/products/intensity.jpg" ) -REGEXP_ATTR_MAGNITUDE = r"Magnitude\(M.{{0,3}}\) (?P<{}>[^ ]+) ".format( - CUSTOM_ATTRIBUTE -) +REGEXP_ATTR_MAGNITUDE = rf"Magnitude\(M.{{0,3}}\) (?P<{CUSTOM_ATTRIBUTE}>[^ ]+) " REGEXP_ATTR_REGION = r"Magnitude\(M.{{0,3}}\) [^ ]+[ ]+-[ ]+(?P<{}>.+)$".format( CUSTOM_ATTRIBUTE ) -REGEXP_ATTR_EVENT_ID = r"eventId=(?P<{}>\d+)$".format(CUSTOM_ATTRIBUTE) +REGEXP_ATTR_EVENT_ID = rf"eventId=(?P<{CUSTOM_ATTRIBUTE}>\d+)$" URL = "http://cnt.rm.ingv.it/feed/atom/all_week" diff --git a/georss_ingv_centro_nazionale_terremoti_client/feed.py b/georss_ingv_centro_nazionale_terremoti_client/feed.py index 297a8f8..29a8159 100644 --- a/georss_ingv_centro_nazionale_terremoti_client/feed.py +++ b/georss_ingv_centro_nazionale_terremoti_client/feed.py @@ -1,5 +1,5 @@ """INGV Centro Nazionale Terremoti (Earthquakes) feed.""" -from typing import Tuple +from __future__ import annotations from georss_client import ATTR_ATTRIBUTION, GeoRssFeed @@ -12,7 +12,7 @@ class IngvCentroNazionaleTerremotiFeed(GeoRssFeed): def __init__( self, - home_coordinates: Tuple[float, float], + home_coordinates: tuple[float, float], filter_radius: float = None, filter_minimum_magnitude: float = None, ): diff --git a/georss_ingv_centro_nazionale_terremoti_client/feed_entry.py b/georss_ingv_centro_nazionale_terremoti_client/feed_entry.py index 77c5ee2..fba830d 100644 --- a/georss_ingv_centro_nazionale_terremoti_client/feed_entry.py +++ b/georss_ingv_centro_nazionale_terremoti_client/feed_entry.py @@ -1,5 +1,5 @@ """INGV Centro Nazionale Terremoti (Earthquakes) feed entry.""" -from typing import Optional, Tuple +from __future__ import annotations from georss_client import FeedEntry @@ -14,7 +14,7 @@ class IngvCentroNazionaleTerremotiFeedEntry(FeedEntry): """INGV Centro Nazionale Terremoti feed entry.""" - def __init__(self, home_coordinates: Tuple[float, float], rss_entry, attribution): + def __init__(self, home_coordinates: tuple[float, float], rss_entry, attribution): """Initialise this service.""" super().__init__(home_coordinates, rss_entry) self._attribution = attribution @@ -25,7 +25,7 @@ def attribution(self) -> str: return self._attribution @property - def magnitude(self) -> Optional[float]: + def magnitude(self) -> float | None: """Return the magnitude of this entry.""" magnitude = self._search_in_title(REGEXP_ATTR_MAGNITUDE) if magnitude: @@ -33,12 +33,12 @@ def magnitude(self) -> Optional[float]: return magnitude @property - def region(self) -> Optional[float]: + def region(self) -> float | None: """Return the region of this entry.""" return self._search_in_title(REGEXP_ATTR_REGION) @property - def event_id(self) -> Optional[int]: + def event_id(self) -> int | None: """Return the event id of this entry.""" event_id = self._search_in_external_id(REGEXP_ATTR_EVENT_ID) if event_id: @@ -46,7 +46,7 @@ def event_id(self) -> Optional[int]: return None @property - def image_url(self) -> Optional[str]: + def image_url(self) -> str | None: """Return the image url of this entry.""" if self.event_id and self.magnitude >= 3: return IMAGE_URL_PATTERN.format(self.event_id) diff --git a/georss_ingv_centro_nazionale_terremoti_client/feed_manager.py b/georss_ingv_centro_nazionale_terremoti_client/feed_manager.py index 862aace..b765ef5 100644 --- a/georss_ingv_centro_nazionale_terremoti_client/feed_manager.py +++ b/georss_ingv_centro_nazionale_terremoti_client/feed_manager.py @@ -1,5 +1,5 @@ """INGV Centro Nazionale Terremoti (Earthquakes) feed manager.""" -from typing import Tuple +from __future__ import annotations from georss_client.feed_manager import FeedManagerBase @@ -14,7 +14,7 @@ def __init__( generate_callback, update_callback, remove_callback, - coordinates: Tuple[float, float], + coordinates: tuple[float, float], filter_radius: float = None, filter_minimum_magnitude: float = None, ): diff --git a/pyproject.toml b/pyproject.toml index 5110771..d4c1601 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,3 +4,42 @@ target-version = ['py38', 'py39', 'py310'] [tool.isort] profile = "black" src_paths = ["georss_ingv_centro_nazionale_terremoti_client", "tests"] + +[tool.ruff] +target-version = "py38" + +select = [ + "B007", # Loop control variable {name} not used within loop body + "B014", # Exception handler with duplicate exception + "C", # complexity + "D", # docstrings + "E", # pycodestyle + "F", # pyflakes/autoflake + "ICN001", # import concentions; {name} should be imported as {asname} + "PGH004", # Use specific rule codes when using noqa + "PLC0414", # Useless import alias. Import alias does not rename original package. + "SIM105", # Use contextlib.suppress({exception}) instead of try-except-pass + "SIM117", # Merge with-statements that use the same scope + "SIM118", # Use {key} in {dict} instead of {key} in {dict}.keys() + "SIM201", # Use {left} != {right} instead of not {left} == {right} + "SIM212", # Use {a} if {a} else {b} instead of {b} if not {a} else {a} + "SIM300", # Yoda conditions. Use 'age == 42' instead of '42 == age'. + "SIM401", # Use get from dict with default instead of an if block + "T20", # flake8-print + "TRY004", # Prefer TypeError exception for invalid type + "RUF006", # Store a reference to the return value of asyncio.create_task + "UP", # pyupgrade + "W", # pycodestyle +] + +ignore = [ + "D202", # No blank lines allowed after function docstring + "D203", # 1 blank line required before class docstring + "D213", # Multi-line docstring summary should start at the second line + "D406", # Section name should end with a newline + "D407", # Section name underlining + "E501", # line too long + "E731", # do not assign a lambda expression, use a def + # Ignored due to performance: https://github.com/charliermarsh/ruff/issues/2923 + "UP038", # Use `X | Y` in `isinstance` call instead of `(X, Y)` +]