BjornFJohansson / pydna

Clone with Python! Data structures for double stranded DNA & simulation of homologous recombination, Gibson assembly, cut & paste cloning.
Other
166 stars 45 forks source link

Typing of dseq #254

Closed manulera closed 2 months ago

manulera commented 3 months ago

Hi @JeffXiePL, this is an example of what you could do for including typing for a module. I have also included some extra small changes beyond indicating the types of arguments and function returns, but you don't really have to do that unless you spot something that you think needs changing.

Have a look at the files changed of this PR to see my changes.

Note the usage of TypeVar. This is useful when you want to use the same class method for a class and subclass, and the method returns that same class or subclass as output. Below is a silly minimal example that you can test in vscode:

from typing import TypeVar

T = TypeVar("T")

class myParentClass():
    def __init__(self, name: str):
        self.name = name

    def with_name_suffix(self: T, other_name: str) -> T:
        return self.__class__(other_name)

class myChildClass(myParentClass):
    def __init__(self, name: str):
        super().__init__(name)

parent = myParentClass("parent")
child = myChildClass("child")

parent2 = parent.with_name_suffix("parent2")
child2 = child.with_name_suffix("child2")

If you hold cmd and hover over parent2 and child2, you will see that the type displayed is the correct one. This is also the case if you omit the typing entirely, because vscode can infer it for this code, but it's not always the case depending on which functions are used in the body of the method, so we can be explicit and indicate it.

manulera commented 3 months ago

@BjornFJohansson this is mostly typing for #176, but also a few refactors of dseq for better clarity or practice (e.g. checking the length of arrays instead of using try / raise).

codecov[bot] commented 3 months ago

Codecov Report

Attention: Patch coverage is 95.18072% with 4 lines in your changes missing coverage. Please review.

Project coverage is 93.57%. Comparing base (f156549) to head (787d00c). Report is 1 commits behind head on dev_bjorn.

Files with missing lines Patch % Lines
src/pydna/dseq.py 94.36% 2 Missing and 2 partials :warning:
Additional details and impacted files [![Impacted file tree graph](https://app.codecov.io/gh/BjornFJohansson/pydna/pull/254/graphs/tree.svg?width=650&height=150&src=pr&token=8gKFpo7HAl&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Bj%C3%B6rn+Johansson)](https://app.codecov.io/gh/BjornFJohansson/pydna/pull/254?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Bj%C3%B6rn+Johansson) ```diff @@ Coverage Diff @@ ## dev_bjorn #254 +/- ## ============================================= - Coverage 93.67% 93.57% -0.10% ============================================= Files 40 40 Lines 3906 3909 +3 Branches 582 585 +3 ============================================= - Hits 3659 3658 -1 - Misses 197 199 +2 - Partials 50 52 +2 ``` | [Files with missing lines](https://app.codecov.io/gh/BjornFJohansson/pydna/pull/254?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Bj%C3%B6rn+Johansson) | Coverage Δ | | |---|---|---| | [src/pydna/common\_sub\_strings.py](https://app.codecov.io/gh/BjornFJohansson/pydna/pull/254?src=pr&el=tree&filepath=src%2Fpydna%2Fcommon_sub_strings.py&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Bj%C3%B6rn+Johansson#diff-c3JjL3B5ZG5hL2NvbW1vbl9zdWJfc3RyaW5ncy5weQ==) | `100.00% <100.00%> (ø)` | | | [src/pydna/utils.py](https://app.codecov.io/gh/BjornFJohansson/pydna/pull/254?src=pr&el=tree&filepath=src%2Fpydna%2Futils.py&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Bj%C3%B6rn+Johansson#diff-c3JjL3B5ZG5hL3V0aWxzLnB5) | `93.11% <100.00%> (-0.03%)` | :arrow_down: | | [src/pydna/dseq.py](https://app.codecov.io/gh/BjornFJohansson/pydna/pull/254?src=pr&el=tree&filepath=src%2Fpydna%2Fdseq.py&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Bj%C3%B6rn+Johansson#diff-c3JjL3B5ZG5hL2RzZXEucHk=) | `96.17% <94.36%> (-0.84%)` | :arrow_down: |