Skip to content

Commit 7b29d10

Browse files
author
drickett
committed
add ruff as part of pre-commit config, apply/resolve lint errors ruff check . --fix produced
1 parent 57a50be commit 7b29d10

File tree

12 files changed

+80
-73
lines changed

12 files changed

+80
-73
lines changed

.pre-commit-config.yaml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@ repos:
99
- id: end-of-file-fixer
1010
- id: name-tests-test
1111
- id: trailing-whitespace
12-
- repo: https://github.com/psf/black
13-
rev: 23.1.0
12+
- repo: https://github.com/astral-sh/ruff-pre-commit
13+
# Ruff version.
14+
rev: v0.9.9
1415
hooks:
15-
- id: black
16+
# Run the linter.
17+
- id: ruff
18+
args: [ --fix ]
19+
# Run the formatter.
20+
- id: ruff-format

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ dev = [
4444
"pytest-mock>=3.14.0",
4545
"pytest-xdist>=3.6.1",
4646
"ruff>=0.9.9",
47+
"types-pytz>=2025.1.0.20250204",
4748
]
4849

4950
[project.urls]

src/easyhla/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
from .easyhla import EasyHLA, HLAType
2-
from .entrypoint import run
1+
from .easyhla import EasyHLA as EasyHLA
2+
from .easyhla import HLAType as HLAType
3+
from .entrypoint import run as run

src/easyhla/easyhla.py

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1+
# ruff: noqa: C901
2+
# TODO: Remove this noqa line and refactor/reduce code-complexity
3+
14
import logging
25
import os
36
import re
47
from datetime import datetime
58
from enum import Enum
6-
from pathlib import Path
79
from typing import Any, Dict, Final, List, Literal, Optional, Tuple
810

911
import Bio.SeqIO
1012
import numpy as np
11-
import typer
1213

1314
from .models import (
1415
Alleles,
@@ -342,7 +343,7 @@ def combine_stds(
342343
seq: List[int],
343344
max_mismatch_threshold: Optional[int],
344345
) -> Dict[int, List[HLACombinedStandardResult]]:
345-
length = len(matching_stds[0].sequence)
346+
# length = len(matching_stds[0].sequence)
346347

347348
default_min = 9999
348349
if max_mismatch_threshold is None:
@@ -376,9 +377,9 @@ def combine_stds(
376377
computed_minimum_mismatches = max(
377378
mismatches, tmp_max_mismatch_threshold
378379
)
379-
if not mismatches in combos:
380+
if mismatches not in combos:
380381
combos[mismatches] = {}
381-
if not combined_std_name in combos[mismatches]:
382+
if combined_std_name not in combos[mismatches]:
382383
combos[mismatches][combined_std_name] = []
383384
stds = [std_a.allele, std_b.allele]
384385
stds.sort()
@@ -423,11 +424,11 @@ def load_hla_frequencies(self, letter: HLA_TYPES) -> Dict[str, int]:
423424
with open(filepath, "r", encoding="utf-8") as f:
424425
for line in f.readlines():
425426
column_id = EasyHLA.COLUMN_IDS[letter]
426-
l = line.strip().split(",")[column_id : column_id + 2]
427-
_l = ",".join([f"{a[:2]}|{a[-2:]}" for a in l])
428-
if hla_freqs.get(_l, None) is None:
429-
hla_freqs[_l] = 0
430-
hla_freqs[_l] += 1
427+
line_array = line.strip().split(",")[column_id : column_id + 2]
428+
elements = ",".join([f"{a[:2]}|{a[-2:]}" for a in line_array])
429+
if hla_freqs.get(elements, None) is None:
430+
hla_freqs[elements] = 0
431+
hla_freqs[elements] += 1
431432
return hla_freqs
432433

433434
# TODO: Convert this to a dictionary instead of a object that looks like:
@@ -449,9 +450,9 @@ def load_hla_stds(self, letter: HLA_TYPES) -> List[HLAStandard]:
449450

450451
with open(filepath, "r", encoding="utf-8") as f:
451452
for line in f.readlines():
452-
l = line.strip().split(",")
453-
seq = self.nuc2bin((l[1] + l[2]))
454-
hla_stds.append(HLAStandard(allele=l[0], sequence=seq))
453+
line_array = line.strip().split(",")
454+
seq = self.nuc2bin((line_array[1] + line_array[2]))
455+
hla_stds.append(HLAStandard(allele=line_array[0], sequence=seq))
455456
return hla_stds
456457

457458
def load_allele_definitions_last_modified_time(self) -> datetime:
@@ -497,7 +498,7 @@ def interpret(
497498
return None
498499
if not self.check_bases(str(entry.seq), samp):
499500
return None
500-
except ValueError as e:
501+
except ValueError:
501502
return None
502503

503504
is_exon = False
@@ -691,7 +692,7 @@ def run(
691692
f"{npats} patients, {nseqs} sequences processed.", to_stdout=to_stdout
692693
)
693694

694-
self.log.info(f"% patients, % sequences processed.", npats, nseqs)
695+
self.log.info("% patients, % sequences processed.", npats, nseqs)
695696

696697
with open(output_filename, "w", encoding="utf-8") as f:
697698
f.write(
@@ -814,9 +815,9 @@ def get_mismatches(
814815
_seq = np.array([int(nuc) for nuc in hla_csr.standard.split("-")])
815816
# TODO: replace with https://stackoverflow.com/questions/16094563/numpy-get-index-where-value-is-true
816817
for idx in np.flatnonzero(_seq ^ seq):
817-
if not idx in correct_bases_at_pos:
818+
if idx not in correct_bases_at_pos:
818819
correct_bases_at_pos[idx] = []
819-
if not _seq[idx] in correct_bases_at_pos[idx]:
820+
if _seq[idx] not in correct_bases_at_pos[idx]:
820821
correct_bases_at_pos[idx].append(_seq[idx])
821822

822823
mislist: List[str] = []

src/easyhla/entrypoint.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ def main(
2828
"--print",
2929
"-p",
3030
help="Print to stdout as sequences are interpretted",
31-
flag_value=True,
32-
is_flag=True,
3331
),
3432
sequence_file: Path = typer.Argument(
3533
...,

src/easyhla/models.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import re
2-
from typing import Dict, List, Optional, Set, Tuple
2+
from typing import Dict, List, Set, Tuple
33

44
import numpy as np
55
import pydantic_numpy.typing as pnd
@@ -28,7 +28,7 @@ def is_homozygous(self) -> bool:
2828
:return: ...
2929
:rtype: bool
3030
"""
31-
return any([_a[0] == _a[1] for _a in self.alleles])
31+
return any(_a[0] == _a[1] for _a in self.alleles)
3232

3333
def is_ambiguous(self) -> bool:
3434
"""
@@ -108,7 +108,7 @@ def stringify_clean(self) -> str:
108108
clean_allele: List[str] = []
109109
for n in [0, 1]:
110110
for i in [4, 3, 2, 1]:
111-
if len(set([":".join(a[n][0:i]) for a in self.get_collection()])) == 1:
111+
if len({":".join(a[n][0:i]) for a in self.get_collection()}) == 1:
112112
clean_allele.append(
113113
re.sub(
114114
r"[A-Z]$", "", ":".join(self.get_collection()[0][n][0:i])
@@ -151,7 +151,7 @@ class HLAStandard(NumpyModel):
151151
sequence: pnd.NpNDArray
152152

153153
def __eq__(self, other):
154-
if type(self) != type(other):
154+
if not isinstance(other, self.__class__):
155155
raise TypeError(f"Cannot compare against {type(other)}")
156156
return all(
157157
[self.allele == other.allele, np.array_equal(self.sequence, other.sequence)]
@@ -162,7 +162,7 @@ class HLAStandardMatch(HLAStandard):
162162
mismatch: int
163163

164164
def __eq__(self, other):
165-
if type(other) != type(self):
165+
if not isinstance(other, self.__class__):
166166
raise TypeError(f"Cannot compare against {type(other)}")
167167
return all(
168168
[
@@ -205,7 +205,7 @@ def get_result(self) -> List[str]:
205205
]
206206

207207
def get_result_as_str(self) -> str:
208-
return ",".join([el for el in self.get_result()])
208+
return ",".join(el for el in self.get_result())
209209

210210

211211
class HLAResult(BaseModel):

tests/easyhla_test.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
import json
21
import os
3-
from contextlib import nullcontext as does_not_raise
42
from datetime import datetime
53
from pathlib import Path
6-
from typing import Any, Dict, List, Optional, Tuple
4+
from typing import List, Tuple
75

86
import numpy as np
97
import pytest
@@ -17,7 +15,7 @@
1715
HLAStandardMatch,
1816
)
1917

20-
from .conftest import compare_ref_vs_test, make_comparison
18+
from .conftest import compare_ref_vs_test
2119

2220

2321
@pytest.fixture(scope="module")
@@ -71,14 +69,14 @@ def test_unknown_hla_type(self):
7169
Assert we raise a value error if we put in an unknown HLA type.
7270
"""
7371
with pytest.raises(ValueError):
74-
easyhla = EasyHLA("D")
72+
_ = EasyHLA("D")
7573

7674
def test_known_hla_type_lowercase(self):
7775
"""
7876
Assert we raise a value error if we put in an HLA type with wrong case.
7977
"""
8078
with pytest.raises(ValueError):
81-
easyhla = EasyHLA("a")
79+
_ = EasyHLA("a")
8280

8381
@pytest.mark.parametrize("easyhla", ["A"], indirect=True)
8482
def test_load_allele_definitions_last_modified_time(
@@ -1167,10 +1165,10 @@ def test_run(self, easyhla: EasyHLA):
11671165

11681166
print(f"Test ended at {end_compare_time.isoformat()}")
11691167

1170-
print(f"Time elapsed: {(end_compare_time-start_time).total_seconds()}")
1168+
print(f"Time elapsed: {(end_compare_time - start_time).total_seconds()}")
11711169
print(
1172-
f"Time elapsed for interpretation: {(end_time-start_time).total_seconds()}"
1170+
f"Time elapsed for interpretation: {(end_time - start_time).total_seconds()}"
11731171
)
11741172
print(
1175-
f"Time elapsed for output comparison: {(end_compare_time-end_time).total_seconds()}"
1173+
f"Time elapsed for output comparison: {(end_compare_time - end_time).total_seconds()}"
11761174
)

tests/models_test.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1-
from typing import Dict, List, Set, Tuple
1+
from typing import List, Set, Tuple
22

33
import pytest
44

5-
from easyhla.easyhla import DATE_FORMAT, EasyHLA
65
from easyhla.models import (
76
Alleles,
8-
HLACombinedStandardResult,
9-
HLAStandard,
10-
HLAStandardMatch,
117
)
128

139

tools/check_date_modified.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,8 @@
1717
file.
1818
"""
1919

20-
2120
import os
2221
from datetime import datetime
23-
from math import ceil
2422
from typing import List, Tuple
2523

2624
import typer
@@ -133,8 +131,8 @@ def _check_dates() -> bool:
133131
def check_dates():
134132
if not _check_dates():
135133
warning_msg = "WARNING: The last modified file date has changed!"
136-
preamble_height = 4
137-
len_msg = ceil(len(warning_msg) / preamble_height)
134+
# preamble_height = 4
135+
# len_msg = ceil(len(warning_msg) / preamble_height)
138136
print("#" * len(warning_msg))
139137
print("#" * len(warning_msg))
140138
# if you want a silly message on an angle, uncomment this.

tools/data_parse.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,21 +47,21 @@
4747
chance = Random().random()
4848
if chance > perc_pure_exon and row[1]["INTRON"] != "":
4949
if HLA_TYPE == "A":
50-
sample_input_seqs[
51-
row[1][ENUM_IDENTIFIER[HLA_TYPE]]
52-
] = f"{row[1]['EXON2']}{row[1]['INTRON']}{row[1]['EXON3']}"
50+
sample_input_seqs[row[1][ENUM_IDENTIFIER[HLA_TYPE]]] = (
51+
f"{row[1]['EXON2']}{row[1]['INTRON']}{row[1]['EXON3']}"
52+
)
5353
else:
54-
sample_input_seqs[
55-
row[1][ENUM_IDENTIFIER[HLA_TYPE]]
56-
] = f"{row[1]['EXON2']}{row[1]['EXON3']}"
54+
sample_input_seqs[row[1][ENUM_IDENTIFIER[HLA_TYPE]]] = (
55+
f"{row[1]['EXON2']}{row[1]['EXON3']}"
56+
)
5757
else:
5858
pure_exon_samples.append(row[1][ENUM_IDENTIFIER[HLA_TYPE]])
59-
sample_input_seqs[
60-
row[1][ENUM_IDENTIFIER[HLA_TYPE]] + "_exon2"
61-
] = f"{row[1]['EXON2']}"
62-
sample_input_seqs[
63-
row[1][ENUM_IDENTIFIER[HLA_TYPE]] + "_exon3"
64-
] = f"{row[1]['EXON3']}"
59+
sample_input_seqs[row[1][ENUM_IDENTIFIER[HLA_TYPE]] + "_exon2"] = (
60+
f"{row[1]['EXON2']}"
61+
)
62+
sample_input_seqs[row[1][ENUM_IDENTIFIER[HLA_TYPE]] + "_exon3"] = (
63+
f"{row[1]['EXON3']}"
64+
)
6565

6666
with open(
6767
os.path.join(
@@ -98,7 +98,7 @@
9898
row[1][ENUM_IDENTIFIER[HLA_TYPE]] in pure_exon_samples
9999
and col == "INTRON"
100100
):
101-
f.write(f",")
101+
f.write(",")
102102
elif col == output_columns[-1]:
103103
f.write(f"{row[1][col]}")
104104
else:

0 commit comments

Comments
 (0)