Skip to content

Commit fb4058f

Browse files
committed
Add mypy support and fixed some found mypy errors.
1 parent 6e43766 commit fb4058f

File tree

5 files changed

+36
-29
lines changed

5 files changed

+36
-29
lines changed

.gitignore

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
*__pycache__
22
poetry.lock
3-
doc/build
3+
doc/build/
44
doc/source/generated/
5-
dist
5+
dist/
6+
.mypy_cache/

mypy.ini

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[mypy]
2+
3+
[mypy-Bio.*]
4+
ignore_missing_imports = True

pyproject.toml

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ authors = ['Bennett Bremer <[email protected]>']
88
python = '3.9'
99
biopython = '^1.79'
1010
scipy = '^1.6.3'
11+
mypy = "^0.910"
1112

1213
[tool.poetry.dev-dependencies]
1314
pytest = '^6.2.4'

src/ggrecomb/parent_alignment/parent_alignment.py

+27-26
Original file line numberDiff line numberDiff line change
@@ -141,20 +141,27 @@ def __init__(self, sequences: list[SeqRecord],
141141

142142
self._auto_align = auto_align
143143
self._prealigned = prealigned
144-
self.aligned_sequences = None # not needed here, included for clarity
145-
self._amino_acids = None
144+
self.aligned_sequences: list[SeqRecord]
146145
self.sequences = sequences # must be set here since it affects others
147-
self.pdb_structure = pdb_structure # relies on aligned_sequences
146+
if pdb_structure is not None:
147+
self.pdb_structure = pdb_structure # relies on aligned_sequences
148148

149149
def __setattr__(self, name, value) -> None:
150150
"""Set aligned_sequences according to auto_align if sequences reset."""
151+
# Prealigned implies immutability. The exception is when
152+
# aligned_sequences is None, which means self is being
153+
# initialized.
154+
if self._prealigned and self.aligned_sequences is not None:
155+
raise ValueError('aligned_sequences cannot be changed if '
156+
'prealigned.')
157+
151158
super().__setattr__(name, value)
152159

153160
if name == 'sequences':
154161
# Prealigned implies immutability. The exception is when
155162
# aligned_sequences is None, which means self is being
156163
# initialized.
157-
if self._prealigned and self.aligned_sequences is not None:
164+
if self._prealigned and hasattr(self, 'aligned_sequences'):
158165
raise ValueError('sequences cannot be changed if prealigned.')
159166

160167
# If sequences is changed, want to modify aligned_sequences.
@@ -166,30 +173,23 @@ def __setattr__(self, name, value) -> None:
166173
self.aligned_sequences = self.sequences
167174
else:
168175
# Reset aligned_sequences and wait for align() call.
169-
self.aligned_sequences = None
176+
del self.aligned_sequences
170177

171178
elif name == 'aligned_sequences':
172-
# Prealigned implies immutability. The exception is when
173-
# aligned_sequences is None, which means self is being
174-
# initialized.
175-
if self._prealigned and self.aligned_sequences is not None:
176-
raise ValueError('aligned_sequences cannot be changed if '
177-
'prealigned.')
179+
aln_seqs = [str(sr.seq) for sr in self.aligned_sequences]
180+
self._amino_acids = tuple(zip(*aln_seqs))
181+
if hasattr(self, 'pdb_structure') and \
182+
self.pdb_structure is not None:
183+
self.pdb_structure.renumber(self.aligned_sequences[0])
178184

179-
if value is None:
180-
self._amino_acids = None
181-
else:
182-
aln_seqs = [str(sr.seq) for sr in self.aligned_sequences]
183-
self._amino_acids = tuple(zip(*aln_seqs))
184-
if hasattr(self, 'pdb_structure') and \
185-
self.pdb_structure is not None:
186-
self.pdb_structure.renumber(self.aligned_sequences[0])
187-
188-
elif name == 'pdb_structure' and value is not None \
189-
and self.aligned_sequences is not None:
185+
elif name == 'pdb_structure' and hasattr(self, 'aligned_sequences'):
190186
# Want to renumber pdb if aligned.
191187
self.pdb_structure.renumber(self.aligned_sequences[0])
192188

189+
def __delattr__(self, name):
190+
if name == 'aligned_sequences':
191+
del self._amino_acids
192+
193193
def __getitem__(self, key):
194194
"""Amino acids at position key in the alignment.
195195
@@ -357,13 +357,14 @@ def to_json(self) -> str:
357357
aligned = True
358358

359359
# TODO: probably add a version?
360-
out_dict = {'seq_records': [], 'aligned': aligned,
361-
'auto_align': self._auto_align}
360+
seq_records = []
362361
# important to preserve order of sequences
363362
for sr in seq_records:
364363
sr_dict = {'seq': str(sr.seq), 'id': sr.id, 'name': sr.name,
365364
'description': sr.description}
366-
out_dict['seq_records'].append(sr_dict)
365+
seq_records.append(sr_dict)
366+
out_dict = {'seq_records': seq_records, 'aligned': aligned,
367+
'auto_align': self._auto_align}
367368
return json.dumps(out_dict)
368369

369370
@classmethod
@@ -398,7 +399,7 @@ def from_json(cls, in_json: str) -> 'ParentAlignment':
398399
new_instance._auto_align = in_dict['auto_align']
399400
return new_instance
400401

401-
def _check_identity(self, desired_identity: float) -> float:
402+
def _check_identity(self, desired_identity: Optional[float]) -> float:
402403
"""Validate the input desired_identity, set to default if None."""
403404
if desired_identity is None:
404405
# Follow default behavior: 0.7 if there's only one sequence.

src/ggrecomb/raspp.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def __init__(
5757
n: int,
5858
start_overhang: Optional[tuple[int, str]] = None,
5959
end_overhang: Optional[tuple[int, str]] = None,
60-
codon_options: dict[str: list[str]] = AA_C31,
60+
codon_options: dict[str, list[str]] = AA_C31,
6161
energy_func: Union[EnergyFunction, type[EnergyFunction]] = SCHEMA
6262
):
6363
"""Init a RASPP graph.

0 commit comments

Comments
 (0)