Skip to content

Commit 457673f

Browse files
authored
1 parent 891cc39 commit 457673f

File tree

8 files changed

+44
-40
lines changed

8 files changed

+44
-40
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ repos:
1313
- id: mixed-line-ending
1414
args: [ --fix=lf ]
1515
- repo: https://github.com/astral-sh/ruff-pre-commit
16-
rev: v0.5.0
16+
rev: v0.8.6
1717
hooks:
1818
- id: ruff
1919
- id: ruff-format

pyproject.toml

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ etl = [
4242
"setuptools", # pinned for 3.12 because yoyo-migrations still uses pkg_resources
4343
]
4444
tests = ["pytest>=6.0", "pytest-cov", "mock", "httpx", "deepdiff"]
45-
dev = ["pre-commit>=3.7.1", "ruff==0.5.0"]
45+
dev = ["pre-commit>=3.7.1", "ruff==0.8.6"]
4646
docs = [
4747
"sphinx==6.1.3",
4848
"sphinx-autodoc-typehints==1.22.0",
@@ -117,10 +117,14 @@ select = [
117117
"RSE", # https://docs.astral.sh/ruff/rules/#flake8-raise-rse
118118
"RET", # https://docs.astral.sh/ruff/rules/#flake8-return-ret
119119
"SLF", # https://docs.astral.sh/ruff/rules/#flake8-self-slf
120+
"SLOT", # https://docs.astral.sh/ruff/rules/#flake8-slots-slot
120121
"SIM", # https://docs.astral.sh/ruff/rules/#flake8-simplify-sim
121122
"ARG", # https://docs.astral.sh/ruff/rules/#flake8-unused-arguments-arg
122123
"PTH", # https://docs.astral.sh/ruff/rules/#flake8-use-pathlib-pth
123124
"PGH", # https://docs.astral.sh/ruff/rules/#pygrep-hooks-pgh
125+
"PLC", # https://docs.astral.sh/ruff/rules/#convention-c
126+
"PLE", # https://docs.astral.sh/ruff/rules/#error-e_1
127+
"TRY", # https://docs.astral.sh/ruff/rules/#tryceratops-try
124128
"PERF", # https://docs.astral.sh/ruff/rules/#perflint-perf
125129
"FURB", # https://docs.astral.sh/ruff/rules/#refurb-furb
126130
"RUF", # https://docs.astral.sh/ruff/rules/#ruff-specific-rules-ruf
@@ -139,13 +143,14 @@ fixable = [
139143
"PT",
140144
"RSE",
141145
"SIM",
146+
"PLC",
147+
"PLE",
148+
"TRY",
142149
"PERF",
143150
"FURB",
144151
"RUF"
145152
]
146153
# ANN003 - missing-type-kwargs
147-
# ANN101 - missing-type-self
148-
# ANN102 - missing-type-cls
149154
# D203 - one-blank-line-before-class
150155
# D205 - blank-line-after-summary
151156
# D206 - indent-with-spaces*
@@ -159,19 +164,20 @@ fixable = [
159164
# E501 - line-too-long*
160165
# W191 - tab-indentation*
161166
# S321 - suspicious-ftp-lib-usage
167+
# PLC0206 - dict-index-missing-items
162168
# *ignored for compatibility with formatter
163169
ignore = [
164-
"ANN003", "ANN101", "ANN102",
170+
"ANN003",
165171
"D203", "D205", "D206", "D213", "D300", "D400", "D415",
166172
"E111", "E114", "E117", "E501",
167173
"W191",
168174
"S321",
175+
"PLC0206"
169176
]
170177

171178
[tool.ruff.lint.per-file-ignores]
172179
# ANN001 - missing-type-function-argument
173180
# ANN2 - missing-return-type
174-
# ANN102 - missing-type-cls
175181
# F401 - unused-import
176182
# N805 - invalid-first-argument-name-for-method
177183
# D100 - undocumented-public-module
@@ -186,7 +192,6 @@ ignore = [
186192
"tests/*" = [
187193
"ANN001",
188194
"ANN2",
189-
"ANN102",
190195
"D100",
191196
"D102",
192197
"S101",

src/gene/cli.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ def _load_source(
206206
try:
207207
processed_ids += source.perform_etl(use_existing)
208208
except GeneNormalizerEtlError as e:
209-
_logger.error(e)
209+
_logger.exception("ETL error while loading %s", n)
210210
click.echo(f"Encountered error while loading {n}: {e}.")
211211
click.get_current_context().exit()
212212
end_load = timer()
@@ -322,13 +322,13 @@ def update_normalizer_db(
322322

323323
if len(sources_split) == 0:
324324
err_msg = "Must enter 1 or more source names to update"
325-
raise Exception(err_msg)
325+
raise ValueError(err_msg)
326326

327327
non_sources = set(sources_split) - set(SOURCES)
328328

329329
if len(non_sources) != 0:
330330
err_msg = f"Not valid source(s): {non_sources}"
331-
raise Exception(err_msg)
331+
raise ValueError(err_msg)
332332

333333
parsed_source_names = {SourceName(SOURCES[s]) for s in sources_split}
334334
_update_normalizer(parsed_source_names, db, update_merged, use_existing)

src/gene/database/dynamodb.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ def drop_db(self) -> None:
105105
try:
106106
if not self._check_delete_okay():
107107
return
108-
except DatabaseWriteException as e:
109-
raise e
108+
except DatabaseWriteException: # noqa: TRY203
109+
raise
110110

111111
if self.gene_table in self.list_tables():
112112
self.dynamodb.Table(self.gene_table).delete()
@@ -250,9 +250,9 @@ def get_record_by_id(
250250
response = self.genes.query(KeyConditionExpression=exp)
251251
record = response["Items"][0]
252252
del record["label_and_type"]
253-
return record
253+
return record # noqa: TRY300
254254
except ClientError as e:
255-
_logger.error(
255+
_logger.exception(
256256
"boto3 client error on get_records_by_id for search term %s: %s",
257257
concept_id,
258258
e.response["Error"]["Message"],
@@ -275,7 +275,7 @@ def get_refs_by_type(self, search_term: str, ref_type: RefType) -> list[str]:
275275
matches = self.genes.query(KeyConditionExpression=filter_exp)
276276
return [m["concept_id"] for m in matches.get("Items", None)]
277277
except ClientError as e:
278-
_logger.error(
278+
_logger.exception(
279279
"boto3 client error on get_refs_by_type for search term %s: %s",
280280
search_term,
281281
e.response["Error"]["Message"],
@@ -378,7 +378,7 @@ def add_record(self, record: dict, src_name: SourceName) -> None:
378378
try:
379379
self.batch.put_item(Item=record)
380380
except ClientError as e:
381-
_logger.error(
381+
_logger.exception(
382382
"boto3 client error on add_record for %s: %s",
383383
concept_id,
384384
e.response["Error"]["Message"],
@@ -411,7 +411,7 @@ def add_merged_record(self, record: dict) -> None:
411411
try:
412412
self.batch.put_item(Item=record)
413413
except ClientError as e:
414-
_logger.error(
414+
_logger.exception(
415415
"boto3 client error on add_record for " "%s: %s",
416416
concept_id,
417417
e.response["Error"]["Message"],
@@ -438,7 +438,7 @@ def _add_ref_record(
438438
try:
439439
self.batch.put_item(Item=record)
440440
except ClientError as e:
441-
_logger.error(
441+
_logger.exception(
442442
"boto3 client error adding reference %s for %s with match type %s: %s",
443443
term,
444444
concept_id,
@@ -473,7 +473,7 @@ def update_merge_ref(self, concept_id: str, merge_ref: Any) -> None: # noqa: AN
473473
)
474474
raise DatabaseWriteException(err_msg) from e
475475

476-
_logger.error(
476+
_logger.exception(
477477
"boto3 client error in `database.update_record()`: %s",
478478
e.response["Error"]["Message"],
479479
)

src/gene/database/postgresql.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,8 @@ def drop_db(self) -> None:
121121
try:
122122
if not self._check_delete_okay():
123123
return
124-
except DatabaseWriteException as e:
125-
raise e
124+
except DatabaseWriteException: # noqa: TRY203
125+
raise
126126

127127
with self.conn.cursor() as cur:
128128
cur.execute(self._drop_db_query)
@@ -601,7 +601,7 @@ def add_record(self, record: dict, src_name: SourceName) -> None: # noqa: ARG00
601601
cur.execute(self._ins_symbol_query, [record["symbol"], concept_id])
602602
self.conn.commit()
603603
except UniqueViolation:
604-
_logger.error("Record with ID %s already exists", concept_id)
604+
_logger.exception("Record with ID %s already exists", concept_id)
605605
self.conn.rollback()
606606

607607
_add_merged_record_query = b"""
@@ -804,12 +804,12 @@ def load_from_remote(self, url: str | None) -> None:
804804
for chunk in r.iter_content(chunk_size=8192):
805805
if chunk:
806806
h.write(chunk)
807-
tar = tarfile.open(temp_tarfile, "r:gz")
808-
tar_dump_file = next(
809-
f for f in tar.getmembers() if f.name.startswith("gene_norm_")
810-
)
811-
tar.extractall(path=tempdir_path, members=[tar_dump_file]) # noqa: S202
812-
dump_file = tempdir_path / tar_dump_file.name
807+
with tarfile.open(temp_tarfile, "r:gz") as tar:
808+
tar_dump_file = next(
809+
f for f in tar.getmembers() if f.name.startswith("gene_norm_")
810+
)
811+
tar.extractall(path=tempdir_path, members=[tar_dump_file]) # noqa: S202
812+
dump_file = tempdir_path / tar_dump_file.name
813813

814814
self.drop_db()
815815
system_call = f"psql {self.conninfo} -f {dump_file.absolute()}"

src/gene/etl/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
from .ncbi import NCBI
1111

1212
__all__ = [
13-
"Ensembl",
1413
"HGNC",
1514
"NCBI",
16-
"GeneNormalizerEtlError",
15+
"Ensembl",
1716
"GeneFileVersionError",
17+
"GeneNormalizerEtlError",
1818
"GeneSourceFetchError",
1919
]

src/gene/etl/merge.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,13 @@ def create_merged_concepts(self, record_ids: set[str]) -> None:
5757
self._database.update_merge_ref(concept_id, merge_ref)
5858
except DatabaseWriteException as dw:
5959
if str(dw).startswith("No such record exists"):
60-
_logger.error(
60+
_logger.exception(
6161
"Updating nonexistent record: %s for merge ref to %s",
6262
concept_id,
6363
merge_ref,
6464
)
6565
else:
66-
_logger.error(str(dw))
66+
_logger.exception("Encountered unknown DB write exception")
6767
uploaded_ids |= group
6868
self._database.complete_write_transaction()
6969
_logger.info("Merged concept generation successful.")
@@ -135,9 +135,9 @@ def record_order(record: dict) -> tuple:
135135
source_rank = SourcePriority[src].value
136136
else:
137137
err_msg = (
138-
f"Prohibited source: {src} in concept_id " f"{record['concept_id']}"
138+
f"Prohibited source: {src} in concept_id {record['concept_id']}"
139139
)
140-
raise Exception(err_msg)
140+
raise ValueError(err_msg)
141141
return source_rank, record["concept_id"]
142142

143143
records.sort(key=record_order)

src/gene/query.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,9 @@ def _fetch_record(
167167
"""
168168
try:
169169
match = self.db.get_record_by_id(concept_id, case_sensitive=False)
170-
except DatabaseReadException as e:
171-
_logger.error(
172-
"Encountered DatabaseReadException looking up %s: %s", concept_id, e
170+
except DatabaseReadException:
171+
_logger.exception(
172+
"Encountered DatabaseReadException looking up %s", concept_id
173173
)
174174
else:
175175
if match:
@@ -243,12 +243,11 @@ def _get_search_response(self, query: str, sources: set[str]) -> dict:
243243
self._fetch_record(resp, ref, MatchType[item_type.upper()])
244244
matched_concept_ids.append(ref)
245245

246-
except DatabaseReadException as e:
247-
_logger.error(
246+
except DatabaseReadException:
247+
_logger.exception(
248248
"Encountered DatabaseReadException looking up %s %s: ",
249249
item_type,
250250
term,
251-
e,
252251
)
253252
continue
254253

0 commit comments

Comments
 (0)