From 53fa45866ba8efd8e154e83e0fc8a263828ff9a1 Mon Sep 17 00:00:00 2001 From: James Stevenson Date: Fri, 3 Jan 2025 15:50:25 -0500 Subject: [PATCH] style: update ruff --- .pre-commit-config.yaml | 2 +- pyproject.toml | 17 +++++++++++------ src/regbot/fetch/clinical_trials.py | 14 +++++++------- src/regbot/fetch/drugsfda.py | 12 ++++++------ src/regbot/fetch/rxclass.py | 8 +++++--- 5 files changed, 30 insertions(+), 23 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index e6a53e2..097092c 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -13,7 +13,7 @@ repos: - id: mixed-line-ending args: [ --fix=lf ] - repo: https://github.com/astral-sh/ruff-pre-commit - rev: v0.5.0 # ruff version + rev: v0.8.4 # ruff version hooks: - id: ruff-format - id: ruff diff --git a/pyproject.toml b/pyproject.toml index 38957d3..b50706c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -23,7 +23,7 @@ dynamic = ["version"] [project.optional-dependencies] tests = ["pytest", "pytest-cov", "requests-mock"] -dev = ["pre-commit>=3.7.1", "ruff==0.5.0"] +dev = ["pre-commit>=3.7.1", "ruff==0.8.4"] [project.urls] @@ -82,10 +82,14 @@ select = [ "RSE", # https://docs.astral.sh/ruff/rules/#flake8-raise-rse "RET", # https://docs.astral.sh/ruff/rules/#flake8-return-ret "SLF", # https://docs.astral.sh/ruff/rules/#flake8-self-slf + "SLOT", # https://docs.astral.sh/ruff/rules/#flake8-slots-slot "SIM", # https://docs.astral.sh/ruff/rules/#flake8-simplify-sim "ARG", # https://docs.astral.sh/ruff/rules/#flake8-unused-arguments-arg "PTH", # https://docs.astral.sh/ruff/rules/#flake8-use-pathlib-pth "PGH", # https://docs.astral.sh/ruff/rules/#pygrep-hooks-pgh + "PLC", # https://docs.astral.sh/ruff/rules/#convention-c + "PLE", # https://docs.astral.sh/ruff/rules/#error-e_1 + "TRY", # https://docs.astral.sh/ruff/rules/#tryceratops-try "PERF", # https://docs.astral.sh/ruff/rules/#perflint-perf "FURB", # https://docs.astral.sh/ruff/rules/#refurb-furb "RUF", # https://docs.astral.sh/ruff/rules/#ruff-specific-rules-ruf @@ -104,13 +108,14 @@ fixable = [ "PT", "RSE", "SIM", + "PLC", + "PLE", + "TRY", "PERF", "FURB", "RUF" ] # ANN003 - missing-type-kwargs -# ANN101 - missing-type-self -# ANN102 - missing-type-cls # D203 - one-blank-line-before-class # D205 - blank-line-after-summary # D206 - indent-with-spaces* @@ -124,19 +129,20 @@ fixable = [ # E501 - line-too-long* # W191 - tab-indentation* # S321 - suspicious-ftp-lib-usage +# PLC0206 - dict-index-missing-items # *ignored for compatibility with formatter ignore = [ - "ANN003", "ANN101", "ANN102", + "ANN003", "D203", "D205", "D206", "D213", "D300", "D400", "D415", "E111", "E114", "E117", "E501", "W191", "S321", + "PLC0206", ] [tool.ruff.lint.per-file-ignores] # ANN001 - missing-type-function-argument # ANN2 - missing-return-type -# ANN102 - missing-type-cls # D100 - undocumented-public-module # D102 - undocumented-public-class # S101 - assert @@ -146,7 +152,6 @@ ignore = [ "tests/*" = [ "ANN001", "ANN2", - "ANN102", "D100", "D102", "D103", diff --git a/src/regbot/fetch/clinical_trials.py b/src/regbot/fetch/clinical_trials.py index 655dfcd..967b3c5 100644 --- a/src/regbot/fetch/clinical_trials.py +++ b/src/regbot/fetch/clinical_trials.py @@ -76,7 +76,7 @@ def _get_dt_object(raw_date: str) -> datetime.datetime: ) except ValueError as e: msg = f"Unable to format {raw_date} as YYYY-MM-DD, YYYY-MM, or YYYY" - _logger.error(msg) + _logger.exception(msg) raise ValueError(msg) from e @@ -1011,21 +1011,21 @@ def make_fda_clinical_trials_request( with requests.get(formatted_url, timeout=30) as r: try: r.raise_for_status() - except RequestException as e: - _logger.warning( + except RequestException: + _logger.exception( "Request to %s returned status code %s", url, r.status_code ) - raise e + raise raw_data = r.json() for i, study in enumerate(raw_data.get("studies", [])): try: parsed_data = _format_study(study) - except ValueError as e: + except ValueError: if skip_parsing_failures: nct_id = _get_id(study, formatted_url, i) - _logger.warning("Failed to parse study %s: %s", nct_id, e) + _logger.exception("Failed to parse study %s", nct_id) continue - raise e + raise results.append(parsed_data) next_page_token = raw_data.get("nextPageToken") diff --git a/src/regbot/fetch/drugsfda.py b/src/regbot/fetch/drugsfda.py index 329039e..c0e6415 100644 --- a/src/regbot/fetch/drugsfda.py +++ b/src/regbot/fetch/drugsfda.py @@ -448,7 +448,7 @@ def _enumify(value: str | None, CandidateEnum: type[Enum]) -> Enum | str | None: .replace("/", "_") ) except ValueError: - _logger.error( + _logger.exception( "Unable to enumify value '%s' into enum '%s'", value, CandidateEnum ) return value @@ -458,7 +458,7 @@ def _intify(value: str) -> int | None: try: return int(value) except ValueError: - _logger.error("Cannot convert value '%s' to int", value) + _logger.exception("Cannot convert value '%s' to int", value) return None @@ -466,7 +466,7 @@ def _make_datetime(value: str) -> datetime.datetime | None: try: return datetime.datetime.strptime(value, "%Y%m%d").replace(tzinfo=datetime.UTC) except ValueError: - _logger.error("Unable to convert value '%s' to datetime", value) + _logger.exception("Unable to convert value '%s' to datetime", value) return None @@ -654,11 +654,11 @@ def make_drugsatfda_request( with requests.get(full_url, timeout=30) as r: try: r.raise_for_status() - except RequestException as e: - _logger.warning( + except RequestException: + _logger.exception( "Request to %s returned status code %s", full_url, r.status_code ) - raise e + raise data = r.json() results += data["results"] skip = data["meta"]["results"]["skip"] + len(data["results"]) diff --git a/src/regbot/fetch/rxclass.py b/src/regbot/fetch/rxclass.py index 3231c11..6cf8164 100644 --- a/src/regbot/fetch/rxclass.py +++ b/src/regbot/fetch/rxclass.py @@ -178,9 +178,11 @@ def make_rxclass_request( with requests.get(url, timeout=30) as r: try: r.raise_for_status() - except RequestException as e: - _logger.warning("Request to %s returned status code %s", url, r.status_code) - raise e + except RequestException: + _logger.exception( + "Request to %s returned status code %s", url, r.status_code + ) + raise raw_data = r.json() if not raw_data: return []