Skip to content
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

style: update to ruff==0.5.0 #425

Merged
merged 3 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ repos:
- id: trailing-whitespace
- id: end-of-file-fixer
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.2.0
rev: v0.5.0 # ruff version
hooks:
- id: ruff-format
- id: ruff
Expand Down
24 changes: 20 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ etl = [
"rich"
]
test = ["pytest", "pytest-cov", "pytest-mock"]
dev = ["pre-commit", "ruff==0.2.0", "lxml", "xmlformatter", "types-pyyaml"]
dev = ["pre-commit", "ruff==0.5.0", "lxml", "xmlformatter", "types-pyyaml"]

[project.urls]
Homepage = "https://github.com/cancervariants/therapy-normalization"
Expand Down Expand Up @@ -97,16 +97,22 @@ select = [
"DTZ", # https://docs.astral.sh/ruff/rules/#flake8-datetimez-dtz
"T10", # https://docs.astral.sh/ruff/rules/#flake8-datetimez-dtz
"EM", # https://docs.astral.sh/ruff/rules/#flake8-errmsg-em
"LOG", # https://docs.astral.sh/ruff/rules/#flake8-logging-log
"G", # https://docs.astral.sh/ruff/rules/#flake8-logging-format-g
"INP", # https://docs.astral.sh/ruff/rules/#flake8-no-pep420-inp
"PIE", # https://docs.astral.sh/ruff/rules/#flake8-pie-pie
"T20", # https://docs.astral.sh/ruff/rules/#flake8-print-t20
"PT", # https://docs.astral.sh/ruff/rules/#flake8-pytest-style-pt
"Q", # https://docs.astral.sh/ruff/rules/#flake8-quotes-q
"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
"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
"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
]
fixable = [
Expand All @@ -117,15 +123,19 @@ fixable = [
"ANN",
"B",
"C4",
"LOG",
"G",
"PIE",
"PT",
"RSE",
"SIM",
"PERF",
"FURB",
"RUF"
]
# ANN101 - missing-type-self
# 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*
Expand All @@ -142,7 +152,7 @@ fixable = [
# S321 - suspicious-ftp-lib-usage
# *ignored for compatibility with formatter
ignore = [
"ANN101", "ANN003",
"ANN003", "ANN101", "ANN102",
"D203", "D205", "D206", "D213", "D300", "D400", "D415",
"E111", "E114", "E117", "E501",
"PGH003",
Expand All @@ -158,6 +168,12 @@ ignore = [
# B011 - assert-false
# N805 - invalid-first-argument-name-for-method
# RUF001 - ambiguous-unicode-character-string
"tests/*" = ["ANN001", "ANN2", "ANN102", "S101", "B011"]
# INP001 - implicit-namespace-package
# SLF001 - private-member-access
# PERF401 - manual-list-comprehension
"tests/*" = ["ANN001", "ANN2", "ANN102", "S101", "B011", "INP001", "SLF001", "PERF401"]
"tests/unit/test_emit_warnings.py" = ["RUF001"]
"src/therapy/schemas.py" = ["ANN001", "ANN201", "N805"]

[tool.ruff.format]
docstring-code-format = true
1 change: 1 addition & 0 deletions src/therapy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""The VICC library for normalizing therapies."""

import re
from pathlib import Path

Expand Down
1 change: 1 addition & 0 deletions src/therapy/cli.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Provides a CLI util to make updates to normalizer database."""

import logging
from collections.abc import Collection
from timeit import default_timer as timer
Expand Down
1 change: 1 addition & 0 deletions src/therapy/database/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Provide database clients."""

from .database import (
AWS_ENV_VAR_NAME,
AbstractDatabase,
Expand Down
4 changes: 2 additions & 2 deletions src/therapy/database/database.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Provide abstract Database class and relevant tools for database initialization."""

import abc
import sys
from collections.abc import Generator
Expand Down Expand Up @@ -313,8 +314,7 @@ def create_db(
>>> local_db = create_db() # creates DynamoDB connection on port 8001
>>>
>>> os.environ["THERAPY_NORM_ENV"] = "Prod"
>>> prod_db = create_db() # creates connection to AWS cloud DynamoDB instance,
>>> # overruling `THERAPY_NORM_DB_URL` variable setting
>>> prod_db = create_db() # creates connection to AWS cloud DynamoDB instance, overruling `THERAPY_NORM_DB_URL` variable setting

Precedence is handled for connection settings like so:

Expand Down
3 changes: 2 additions & 1 deletion src/therapy/database/dynamodb.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Provide DynamoDB client."""

import atexit
import logging
import sys
Expand Down Expand Up @@ -607,7 +608,7 @@ def delete_source(self, src_name: SourceName) -> None:
"concept_id": record["concept_id"],
}
)
except ClientError as e:
except ClientError as e: # noqa: PERF203
raise DatabaseWriteError(e) from e

def complete_write_transaction(self) -> None:
Expand Down
1 change: 1 addition & 0 deletions src/therapy/etl/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Module to load and init namespace at package level"""

from .base import EtlError
from .chembl import ChEMBL
from .chemidplus import ChemIDplus
Expand Down
15 changes: 12 additions & 3 deletions src/therapy/etl/base.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""A base class for extraction, transformation, and loading of data."""

import contextlib
import json
import logging
Expand Down Expand Up @@ -71,9 +72,17 @@ def __init__(
# self._name = SourceName[self.__class__.__name__.upper()]
self._silent = silent
self._name = SourceName(self.__class__.__name__)
self._data_source: ChemblData | ChemIDplusData | DrugBankData | DrugsAtFdaData | GToPLigandData | HemOncData | NcitData | RxNormData | CustomData = self._get_data_handler(
data_path
) # type: ignore
self._data_source: (
ChemblData
| ChemIDplusData
| DrugBankData
| DrugsAtFdaData
| GToPLigandData
| HemOncData
| NcitData
| RxNormData
| CustomData
) = self._get_data_handler(data_path) # type: ignore
self.database = database
self._added_ids: list[str] = []
self._rules = Rules(self._name)
Expand Down
1 change: 1 addition & 0 deletions src/therapy/etl/chembl.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Defines the ChEMBL ETL methods."""

import sqlite3

from tqdm import tqdm
Expand Down
7 changes: 4 additions & 3 deletions src/therapy/etl/chemidplus.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

Courtesy of the U.S. National Library of Medicine.
"""

import re
import xml.etree.ElementTree as ElTree
from collections.abc import Generator
Expand Down Expand Up @@ -61,9 +62,9 @@ def _transform_data(self) -> None:
reg_no = chemical.find("NumberList").find("CASRegistryNumber")
if not reg_no:
continue
params[
"concept_id"
] = f"{NamespacePrefix.CASREGISTRY.value}:{reg_no.text}"
params["concept_id"] = (
f"{NamespacePrefix.CASREGISTRY.value}:{reg_no.text}"
)

# get aliases
aliases = []
Expand Down
1 change: 1 addition & 0 deletions src/therapy/etl/drugbank.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Defines the DrugBank ETL methods."""

import csv
from typing import Any

Expand Down
1 change: 1 addition & 0 deletions src/therapy/etl/drugsatfda.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""ETL methods for the Drugs@FDA source."""

import json
import logging

Expand Down
1 change: 1 addition & 0 deletions src/therapy/etl/guidetopharmacology.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Module for Guide to PHARMACOLOGY ETL methods."""

import csv
import html
import logging
Expand Down
1 change: 1 addition & 0 deletions src/therapy/etl/hemonc.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Provide ETL methods for HemOnc.org data."""

import csv
import logging

Expand Down
1 change: 1 addition & 0 deletions src/therapy/etl/ncit.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""ETL methods for NCIt source"""

import logging

import owlready2 as owl
Expand Down
1 change: 1 addition & 0 deletions src/therapy/etl/rules.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Apply manual data restrictions and annotations to extracted records."""

import csv

from therapy import APP_ROOT
Expand Down
5 changes: 3 additions & 2 deletions src/therapy/etl/rxnorm.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
and Human Services; NLM is not responsible for the product and does not
endorse or recommend this or any other product."
"""

import csv
import logging
import re
Expand Down Expand Up @@ -89,7 +90,7 @@ def _extract_data(self, use_existing: bool) -> None:
src_name="rxnorm_drug_forms",
filetype="yaml",
latest_version_cb=lambda: self._version,
download_cb=lambda version, file: self._create_drug_form_yaml(
download_cb=lambda version, file: self._create_drug_form_yaml( # noqa: ARG005
file,
self._data_file, # type: ignore
),
Expand Down Expand Up @@ -194,7 +195,7 @@ def _get_trade_names(

if "PIN" in value and value["PIN"] in precise_ingredient:
for pin in precise_ingredient[value["PIN"]]:
labels.append(pin.lower())
labels.append(pin.lower()) # noqa: PERF401

for label in labels:
trade_names: list[str] = [
Expand Down
3 changes: 2 additions & 1 deletion src/therapy/etl/wikidata.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Defines the Wikidata ETL methods."""

import datetime
import json
from pathlib import Path
Expand Down Expand Up @@ -90,7 +91,7 @@ class Wikidata(Base):
"""Class for Wikidata ETL methods."""

@staticmethod
def _download_data(version: str, file: Path) -> None:
def _download_data(version: str, file: Path) -> None: # noqa: ARG004
"""Download latest Wikidata source dump.

:param version: not used by this method
Expand Down
1 change: 1 addition & 0 deletions src/therapy/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Main application for FastAPI"""

import html

from fastapi import FastAPI, HTTPException, Query
Expand Down
15 changes: 6 additions & 9 deletions src/therapy/query.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Provides methods for handling queries."""

import datetime
import json
import re
Expand Down Expand Up @@ -143,7 +144,7 @@ def _fetch_records(
raise KeyError(msg)
(response, src) = self._add_record(response, match, match_type)
matched_sources.add(src)
except ClientError as e:
except ClientError as e: # noqa: PERF203
logger.error(e.response["Error"]["Message"])

return response, matched_sources
Expand Down Expand Up @@ -302,9 +303,7 @@ def search(
if invalid source names are given.
"""
sources = {}
for k, v in SOURCES.items():
if self.db.get_source_metadata(v):
sources[k] = v
sources = {k: v for k, v in SOURCES.items() if self.db.get_source_metadata(v)}
if not incl and not excl:
query_sources = set(sources.values())
elif incl and excl:
Expand Down Expand Up @@ -359,7 +358,7 @@ def _add_merged_meta(self, response: NormalizationService) -> NormalizationServi
for src in sources:
try:
src_name = SourceName(PREFIX_LOOKUP[src])
except KeyError:
except KeyError: # noqa: PERF203
# not an imported source
continue
else:
Expand All @@ -382,7 +381,6 @@ def _add_therapeutic_agent(
self,
response: NormalizationService,
record: dict,
query: str,
match_type: MatchType,
) -> NormalizationService:
"""Format received DB record as therapeutic agent and update response object.
Expand Down Expand Up @@ -538,10 +536,9 @@ def normalize(self, query: str, infer: bool = True) -> NormalizationService:
# prepare basic response
response = NormalizationService(**self._prepare_normalized_response(query))

add_ta_curry = lambda res, rec, mat: self._add_therapeutic_agent( # noqa: E731
res, rec, query, mat
return self._perform_normalized_lookup(
response, query, infer, self._add_therapeutic_agent
)
return self._perform_normalized_lookup(response, query, infer, add_ta_curry)

def _construct_drug_match(self, record: dict) -> Therapy:
"""Create individual Drug match for unmerged normalization endpoint.
Expand Down
5 changes: 3 additions & 2 deletions src/therapy/schemas.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Contains data models for representing VICC therapy records."""

from datetime import datetime
from enum import Enum, IntEnum
from typing import Any, Literal
Expand Down Expand Up @@ -354,9 +355,9 @@ class ServiceMeta(BaseModel):
name: Literal["thera-py"] = "thera-py"
version: Literal[__version__] = __version__ # type: ignore[valid-type]
response_datetime: datetime
url: Literal[
url: Literal["https://github.com/cancervariants/therapy-normalization"] = (
"https://github.com/cancervariants/therapy-normalization"
] = "https://github.com/cancervariants/therapy-normalization"
)

model_config = ConfigDict(
json_schema_extra={
Expand Down
1 change: 1 addition & 0 deletions src/therapy/version.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
"""Therapy normalizer version"""

__version__ = "0.5.0-dev5"
1 change: 1 addition & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Pytest test config tools."""

import json
import logging
import os
Expand Down
1 change: 1 addition & 0 deletions tests/scripts/build_chembl_data.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Construct test data for ChEMBL source."""

import contextlib
import sqlite3
from pathlib import Path
Expand Down
1 change: 1 addition & 0 deletions tests/scripts/build_chemidplus_data.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Build chemidplus test data."""

from collections.abc import Generator
from pathlib import Path
from xml.etree import ElementTree
Expand Down
1 change: 1 addition & 0 deletions tests/scripts/build_disease_normalizer_data.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Construct static mapping of disease normalizer input.
Assumes complete and functioning disease normalizer endpoint is available.
"""

import json
from pathlib import Path

Expand Down
1 change: 1 addition & 0 deletions tests/scripts/build_drugbank_data.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Build DrugBank test data."""

import csv
from pathlib import Path

Expand Down
1 change: 1 addition & 0 deletions tests/scripts/build_drugsatfda_data.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Build drugsatfda test data"""

import json
from pathlib import Path

Expand Down
1 change: 1 addition & 0 deletions tests/scripts/build_gtop_data.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Build test data for GuideToPharmacology"""

import csv
from pathlib import Path

Expand Down
Loading
Loading