Skip to content

Commit 42e5782

Browse files
committed
Let Ruff fix some typing issues
1 parent c6a00db commit 42e5782

19 files changed

Lines changed: 152 additions & 155 deletions

‎pyproject.toml‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,12 @@ ignore = [
7676
"BLE001", # Do not catch blind exception: `Exception`
7777
"C408", # Unnecessary `dict()` call (rewrite as a literal)
7878
"S102", # Use of `exec` detected
79+
"SIM905", # Consider using a list literal instead of `str.split`
7980

8081
# Possibly disable the following ones:
8182
"I001", # Import block is un-sorted or un-formatted
82-
"UP035", # Import from `collections.abc` instead: `Sequence`
83+
"PLC0414", # Import alias does not rename original package
8384
"ISC004", # Unparenthesized implicit string concatenation in collection
84-
"SIM905", # Consider using a list literal instead of `str.split`
85-
"UP030", # Use implicit references for positional format fields
86-
"UP006", # Use `list` instead of `List` for type annotation
8785
"RUF059", # Unpacked variable `matches` is never used
8886
"SIM102", # Use a single `if` statement instead of nested `if` statements
8987
"FA100", # Add `from __future__ import annotations` to simplify `typing.Optional`
@@ -92,11 +90,9 @@ ignore = [
9290
"RUF012", # Mutable default value for class attribute
9391
"FURB188", # Prefer `str.removesuffix()` over conditionally replacing with slice.
9492
"PYI029", # Defining `__repr__` in a stub is almost always redundant
95-
"PLC0414", # Import alias does not rename original package
9693
"PLR1704", # Redefining argument with the local name `s`
9794
"B020", # Loop control variable `s` overrides iterable it iterates
9895
"RUF022", # `__all__` is not sorted
9996
"B006", # Do not use mutable data structures for argument defaults
10097
"RUF023", # `SingleMatch.__slots__` is not sorted
101-
"UP045", # Use `X | None` for type annotations
10298
]

‎src/cutadapt/_align.pyi‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Optional, Tuple, Iterable
1+
from collections.abc import Iterable
22

33
class DPMatrix:
44
m: int
@@ -24,7 +24,7 @@ class Aligner:
2424
def dpmatrix(self) -> DPMatrix: ...
2525
def scorematrix(self) -> DPMatrix: ...
2626
def enable_debug(self) -> None: ...
27-
def locate(self, query: str) -> Optional[Tuple[int, int, int, int, int, int]]: ...
27+
def locate(self, query: str) -> tuple[int, int, int, int, int, int] | None: ...
2828

2929
class PrefixComparer:
3030
@property
@@ -38,9 +38,9 @@ class PrefixComparer:
3838
min_overlap: int = 1,
3939
): ...
4040
def __repr__(self) -> str: ...
41-
def locate(self, query: str) -> Optional[Tuple[int, int, int, int, int, int]]: ...
41+
def locate(self, query: str) -> tuple[int, int, int, int, int, int] | None: ...
4242

4343
class SuffixComparer(PrefixComparer): ...
4444

4545
def hamming_sphere(s: str, k: int) -> Iterable[str]: ...
46-
def edit_environment(t: str, k: int) -> Iterable[Tuple[str, int, int]]: ...
46+
def edit_environment(t: str, k: int) -> Iterable[tuple[str, int, int]]: ...

‎src/cutadapt/_kmer_finder.pyi‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
from typing import List, Optional, Tuple
2-
31
MAXIMUM_WORD_SIZE: int
42

53
class KmerFinder:
64
def __init__(
75
self,
8-
positions_and_kmers: List[Tuple[int, Optional[int], List[str]]],
6+
positions_and_kmers: list[tuple[int, int | None, list[str]]],
97
ref_wildcards: bool = False,
108
query_wildcards: bool = False,
119
): ...

‎src/cutadapt/adapters.py‎

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
import logging
99
from enum import IntFlag
1010
from collections import defaultdict
11-
from typing import Optional, Tuple, Sequence, Dict, Any, List, Union
11+
from typing import Optional, Any, Union
12+
from collections.abc import Sequence
1213
from abc import ABC, abstractmethod
1314
import time
1415

@@ -80,7 +81,7 @@ def __init__(self, adapter: "SingleAdapter"):
8081
self.adapter_type: str = adapter.descriptive_identifier()
8182
self.allows_partial_matches: bool = adapter.allows_partial_matches
8283
# self.errors[l][e] == n iff a sequence of length l matching at e errors was removed n times
83-
self.errors: Dict[int, Dict[int, int]] = defaultdict(returns_defaultdict_int)
84+
self.errors: dict[int, dict[int, int]] = defaultdict(returns_defaultdict_int)
8485
self.adjacent_bases = {"A": 0, "C": 0, "G": 0, "T": 0, "": 0}
8586
# TODO avoid hard-coding the list of classes
8687
self._remove_prefix = isinstance(adapter, FrontAdapter)
@@ -111,7 +112,7 @@ def lengths(self):
111112
d = {length: sum(errors.values()) for length, errors in self.errors.items()}
112113
return d
113114

114-
def random_match_probabilities(self, gc_content: float) -> List[float]:
115+
def random_match_probabilities(self, gc_content: float) -> list[float]:
115116
"""
116117
Estimate probabilities that this adapter end matches a
117118
random sequence. Indels are not taken into account.
@@ -147,7 +148,7 @@ def __iadd__(self, other):
147148
pass
148149

149150
@abstractmethod
150-
def end_statistics(self) -> Tuple[Optional[EndStatistics], Optional[EndStatistics]]:
151+
def end_statistics(self) -> tuple[Optional[EndStatistics], Optional[EndStatistics]]:
151152
pass
152153

153154
@abstractmethod
@@ -181,7 +182,7 @@ class FrontAdapterStatistics(SingleAdapterStatistics):
181182
def add_match(self, match: "RemoveBeforeMatch"):
182183
self.end.errors[match.removed_sequence_length()][match.errors] += 1
183184

184-
def end_statistics(self) -> Tuple[Optional[EndStatistics], Optional[EndStatistics]]:
185+
def end_statistics(self) -> tuple[Optional[EndStatistics], Optional[EndStatistics]]:
185186
return self.end, None
186187

187188

@@ -194,7 +195,7 @@ def add_match(self, match: "RemoveAfterMatch"):
194195
except KeyError:
195196
self.end.adjacent_bases[""] += 1
196197

197-
def end_statistics(self) -> Tuple[Optional[EndStatistics], Optional[EndStatistics]]:
198+
def end_statistics(self) -> tuple[Optional[EndStatistics], Optional[EndStatistics]]:
198199
return None, self.end
199200

200201

@@ -242,7 +243,7 @@ def add_match(self, match: "LinkedMatch"):
242243
except KeyError:
243244
self.back.adjacent_bases[""] += 1
244245

245-
def end_statistics(self) -> Tuple[Optional[EndStatistics], Optional[EndStatistics]]:
246+
def end_statistics(self) -> tuple[Optional[EndStatistics], Optional[EndStatistics]]:
246247
return self.front, self.back
247248

248249

@@ -281,23 +282,23 @@ def add_match(self, match: Union["RemoveBeforeMatch", "RemoveAfterMatch"]) -> No
281282
except KeyError:
282283
self.back.adjacent_bases[""] += 1
283284

284-
def end_statistics(self) -> Tuple[Optional[EndStatistics], Optional[EndStatistics]]:
285+
def end_statistics(self) -> tuple[Optional[EndStatistics], Optional[EndStatistics]]:
285286
return self.front, self.back
286287

287288

288289
class Match(ABC):
289290
adapter: "Adapter"
290291

291292
@abstractmethod
292-
def remainder_interval(self) -> Tuple[int, int]:
293+
def remainder_interval(self) -> tuple[int, int]:
293294
pass
294295

295296
@abstractmethod
296-
def retained_adapter_interval(self) -> Tuple[int, int]:
297+
def retained_adapter_interval(self) -> tuple[int, int]:
297298
pass
298299

299300
@abstractmethod
300-
def get_info_records(self, read) -> List[List]:
301+
def get_info_records(self, read) -> list[list]:
301302
pass
302303

303304
@abstractmethod
@@ -388,7 +389,7 @@ def wildcards(self, wildcard_char: str = "N") -> str:
388389
]
389390
return "".join(wildcards)
390391

391-
def get_info_records(self, read) -> List[List]:
392+
def get_info_records(self, read) -> list[list]:
392393
seq = read.sequence
393394
qualities = read.qualities
394395
info = [
@@ -432,14 +433,14 @@ def rest(self) -> str:
432433
"""
433434
return self.sequence[: self.rstart]
434435

435-
def remainder_interval(self) -> Tuple[int, int]:
436+
def remainder_interval(self) -> tuple[int, int]:
436437
"""
437438
Return an interval (start, stop) that describes the part of the read that would
438439
remain after trimming
439440
"""
440441
return self.rstop, len(self.sequence)
441442

442-
def retained_adapter_interval(self) -> Tuple[int, int]:
443+
def retained_adapter_interval(self) -> tuple[int, int]:
443444
return self.rstart, len(self.sequence)
444445

445446
def trim_slice(self):
@@ -465,14 +466,14 @@ def rest(self) -> str:
465466
"""
466467
return self.sequence[self.rstop :]
467468

468-
def remainder_interval(self) -> Tuple[int, int]:
469+
def remainder_interval(self) -> tuple[int, int]:
469470
"""
470471
Return an interval (start, stop) that describes the part of the read that would
471472
remain after trimming
472473
"""
473474
return 0, self.rstart
474475

475-
def retained_adapter_interval(self) -> Tuple[int, int]:
476+
def retained_adapter_interval(self) -> tuple[int, int]:
476477
return 0, self.rstop
477478

478479
def trim_slice(self):
@@ -710,7 +711,7 @@ def match_to(self, sequence: str):
710711
"""
711712
if not self.kmer_finder.kmers_present(sequence):
712713
return None
713-
alignment: Optional[Tuple[int, int, int, int, int, int]] = self.aligner.locate(
714+
alignment: Optional[tuple[int, int, int, int, int, int]] = self.aligner.locate(
714715
sequence
715716
)
716717
if self._debug:
@@ -762,7 +763,7 @@ def match_to(self, sequence: str):
762763
reversed_sequence = sequence[::-1]
763764
if not self.kmer_finder.kmers_present(reversed_sequence):
764765
return None
765-
alignment: Optional[Tuple[int, int, int, int, int, int]] = self.aligner.locate(
766+
alignment: Optional[tuple[int, int, int, int, int, int]] = self.aligner.locate(
766767
reversed_sequence
767768
)
768769
if self._debug:
@@ -818,7 +819,7 @@ def match_to(self, sequence: str):
818819
"""
819820
if not self.kmer_finder.kmers_present(sequence):
820821
return None
821-
alignment: Optional[Tuple[int, int, int, int, int, int]] = self.aligner.locate(
822+
alignment: Optional[tuple[int, int, int, int, int, int]] = self.aligner.locate(
822823
sequence
823824
)
824825
if self._debug:
@@ -866,7 +867,7 @@ def match_to(self, sequence: str):
866867
reversed_sequence = sequence[::-1]
867868
if not self.kmer_finder.kmers_present(reversed_sequence):
868869
return None
869-
alignment: Optional[Tuple[int, int, int, int, int, int]] = self.aligner.locate(
870+
alignment: Optional[tuple[int, int, int, int, int, int]] = self.aligner.locate(
870871
reversed_sequence
871872
)
872873
if self._debug:
@@ -1130,13 +1131,13 @@ def trimmed(self, read):
11301131
read = self.back_match.trimmed(read)
11311132
return read
11321133

1133-
def remainder_interval(self) -> Tuple[int, int]:
1134+
def remainder_interval(self) -> tuple[int, int]:
11341135
matches = [
11351136
match for match in [self.front_match, self.back_match] if match is not None
11361137
]
11371138
return remainder(matches)
11381139

1139-
def retained_adapter_interval(self) -> Tuple[int, int]:
1140+
def retained_adapter_interval(self) -> tuple[int, int]:
11401141
if self.front_match:
11411142
start = self.front_match.rstart
11421143
offset = self.front_match.rstop
@@ -1148,7 +1149,7 @@ def retained_adapter_interval(self) -> Tuple[int, int]:
11481149
end = len(self.front_match.sequence)
11491150
return start, end
11501151

1151-
def get_info_records(self, read) -> List[List]:
1152+
def get_info_records(self, read) -> list[list]:
11521153
records = []
11531154
for match, namesuffix in [
11541155
(self.front_match, ";1"),
@@ -1295,7 +1296,7 @@ class AdapterIndex:
12951296
Use the is_acceptable() method to check individual adapters.
12961297
"""
12971298

1298-
AdapterIndexDict = Dict[str, Tuple[SingleAdapter, int, int]]
1299+
AdapterIndexDict = dict[str, tuple[SingleAdapter, int, int]]
12991300

13001301
def __init__(self, adapters, prefix: bool):
13011302
"""All given adapters must be of the same type"""
@@ -1387,7 +1388,7 @@ def is_acceptable(cls, adapter: SingleAdapter, prefix: bool):
13871388
return False
13881389
return True
13891390

1390-
def _make_index(self) -> Tuple[List[int], "AdapterIndexDict", int]:
1391+
def _make_index(self) -> tuple[list[int], "AdapterIndexDict", int]:
13911392
start_time = time.time()
13921393
max_k = max(
13931394
(
@@ -1404,7 +1405,7 @@ def _make_index(self) -> Tuple[List[int], "AdapterIndexDict", int]:
14041405
"Indexing could take long and use a lot of memory. "
14051406
"If this becomes a problem, try --no-indels and/or --no-index."
14061407
)
1407-
index: Dict[str, Tuple[SingleAdapter, int, int]] = dict()
1408+
index: dict[str, tuple[SingleAdapter, int, int]] = dict()
14081409
lengths = set()
14091410
ambiguous = {}
14101411
for adapter in self._adapters:
@@ -1579,7 +1580,7 @@ def warn_duplicate_adapters(adapters):
15791580
d[key] = adapter.name
15801581

15811582

1582-
def remainder(matches: Sequence[Match]) -> Tuple[int, int]:
1583+
def remainder(matches: Sequence[Match]) -> tuple[int, int]:
15831584
"""
15841585
Determine which section of the read would not be trimmed. Return a tuple (start, stop)
15851586
that gives the interval of the untrimmed part relative to the original read.

‎src/cutadapt/align.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
]
1111

1212
from enum import IntFlag
13-
from typing import Iterator, Tuple
13+
from collections.abc import Iterator
1414

1515
from cutadapt._align import (
1616
Aligner,
@@ -60,7 +60,7 @@ def edit_distance(s: str, t: str) -> int:
6060
return costs[-1]
6161

6262

63-
def hamming_environment(s: str, k: int) -> Iterator[Tuple[str, int, int]]:
63+
def hamming_environment(s: str, k: int) -> Iterator[tuple[str, int, int]]:
6464
"""
6565
Find all strings t for which the hamming distance between s and t is at most k,
6666
assuming the alphabet is A, C, G, T.
@@ -96,7 +96,7 @@ def naive_edit_environment(s: str, k: int) -> Iterator[str]:
9696
yield s[:i] + s[i + 1 :]
9797

9898

99-
def py_edit_environment(s: str, k: int) -> Iterator[Tuple[str, int, int]]:
99+
def py_edit_environment(s: str, k: int) -> Iterator[tuple[str, int, int]]:
100100
"""
101101
Find all strings t for which the edit distance between s and t is at most k,
102102
assuming the alphabet is A, C, G, T.
@@ -116,7 +116,7 @@ def py_edit_environment(s: str, k: int) -> Iterator[Tuple[str, int, int]]:
116116
yield t, errors, score
117117

118118

119-
def slow_edit_environment(s: str, k: int) -> Iterator[Tuple[str, int, int]]:
119+
def slow_edit_environment(s: str, k: int) -> Iterator[tuple[str, int, int]]:
120120
"""
121121
Find all strings t for which the edit distance between s and t is at most k,
122122
assuming the alphabet is A, C, G, T.

0 commit comments

Comments
 (0)