Skip to content

Commit

Permalink
Better typing for DNA base methods
Browse files Browse the repository at this point in the history
  • Loading branch information
BooleanCat committed Nov 26, 2024
1 parent f6c5ae4 commit 2fb459f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
14 changes: 12 additions & 2 deletions haem.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,22 @@ class DNABase:
def __invert__(self) -> DNABase:
"""See `DNABase.complement`."""

def __add__(self, other: typing.Union[DNABase, DNASequence, str]) -> DNASequence:
def __add__(
self,
other: typing.Union[
DNABase, DNASequence, typing.Iterator[str], typing.Sequence[str], str
],
) -> DNASequence:
"""Create a new sequence consisting of this base followed by the given
sequence member(s)."""
...

def __radd__(self, other: typing.Union[DNABase, DNASequence, str]) -> DNASequence:
def __radd__(
self,
other: typing.Union[
DNABase, DNASequence, str, typing.Iterator[str], typing.Sequence[str], str
],
) -> DNASequence:
"""Create a new sequence consisting of the given sequence member(s)
followed by this base."""
...
Expand Down
12 changes: 11 additions & 1 deletion tests/test_dna_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,19 @@ def test__bool__(base: haem.DNABase, result: bool) -> None:
(haem.DNABase("A"), haem.DNASequence(), haem.DNASequence("A")),
(haem.DNABase("A"), "", haem.DNASequence("A")),
(haem.DNABase("A"), haem.DNASequence("T"), haem.DNASequence("AT")),
(haem.DNABase("A"), iter("ACG"), haem.DNASequence("AACG")),
(haem.DNABase("A"), ["A", "C", "G"], haem.DNASequence("AACG")),
],
)
def test__add__(
left: haem.DNABase,
right: typing.Union[haem.DNABase, haem.DNASequence, str],
right: typing.Union[
haem.DNABase,
haem.DNASequence,
typing.Iterator[str],
typing.Sequence[str],
str,
],
result: haem.DNASequence,
) -> None:
assert left + right == result
Expand All @@ -196,6 +204,8 @@ def test__add__(
(haem.DNASequence(), haem.DNABase("A"), haem.DNASequence("A")),
("", haem.DNABase("A"), haem.DNASequence("A")),
("T", haem.DNABase("A"), haem.DNASequence("TA")),
(iter("ACG"), haem.DNABase("A"), haem.DNASequence("ACGA")),
(["A", "C", "G"], haem.DNABase("A"), haem.DNASequence("ACGA")),
],
)
def test__radd__(
Expand Down

0 comments on commit 2fb459f

Please sign in to comment.