Skip to content

Comments

Add type annotations#5087

Open
szabgab wants to merge 4 commits intobiopython:masterfrom
szabgab:szabgab/type-annotations
Open

Add type annotations#5087
szabgab wants to merge 4 commits intobiopython:masterfrom
szabgab:szabgab/type-annotations

Conversation

@szabgab
Copy link

@szabgab szabgab commented Oct 24, 2025

  • [X ] I hereby agree to dual licence this and any previous contributions under both
    the Biopython License Agreement AND the BSD 3-Clause License.

  • [ X] I have read the CONTRIBUTING.rst file, have run pre-commit
    locally, and understand that continuous integration checks will be used to
    confirm the Biopython unit tests and style checks pass with these changes.

  • [ X] I have added my name to the alphabetical contributors listings in the files
    NEWS.rst and CONTRIB.rst as part of this pull request, am listed
    already, or do not wish to be listed. (This acknowledgement is optional.)

Addresses #2236 and does not touch files that are addressed in #5057

"""
try:
table_id = int(table)
# TODO: only call int if table is of type int
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This try/except is the check for it being an int, or int like.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it be better to change this to isinstance(table, int) and remove the try/except ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could be something else int-like but not a subclass. I would be cautious about changing this for fear of unintended side effects.

Bio/Seq.py Outdated


def reverse_complement(sequence, inplace=False):
def reverse_complement(sequence: Seq, inplace: bool = False) -> str:
Copy link
Member

@peterjc peterjc Oct 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Surely Union[str, Seq, MutableSeq, SeqRecord] as per the docstring?

Bio/Seq.py Outdated


def reverse_complement_rna(sequence, inplace=False):
def reverse_complement_rna(sequence: Seq, inplace: bool = False) -> str:
Copy link
Member

@peterjc peterjc Oct 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Surely Union[str, Seq, MutableSeq, SeqRecord] as per the docstring?

Bio/Seq.py Outdated


def complement(sequence, inplace=False):
def complement(sequence: Seq, inplace: bool = False) -> str:
Copy link
Member

@peterjc peterjc Oct 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Surely Union[str, Seq, MutableSeq, SeqRecord] as per the docstring?

Bio/Seq.py Outdated


def complement_rna(sequence, inplace=False):
def complement_rna(sequence: Seq, inplace: bool = False) -> str:
Copy link
Member

@peterjc peterjc Oct 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Surely Union[str, Seq, MutableSeq, SeqRecord] as per the docstring?

Bio/Seq.py Outdated
def translate(
sequence, table="Standard", stop_symbol="*", to_stop=False, cds=False, gap=None
):
sequence: Seq,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Surely Union[str, Seq, MutableSeq] as per the docstring?

Copy link
Member

@peterjc peterjc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lots to fix here - see comments.

Bio/Seq.py Outdated
def _translate_str(
sequence, table, stop_symbol="*", to_stop=False, cds=False, pos_stop="X", gap=None
):
sequence: Seq,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, as per the docstring, this should be a string.

* In some cases a new, temporary variable called `encoded_sequence`
  was used as that value has a different type.
* In 3 places attr-defined had to be ignored as mypy complained about
  missing methods in SeqRecord. This probably needs to be addressed.
@szabgab
Copy link
Author

szabgab commented Oct 29, 2025

Thanks for the feedback. I hope this one is going to be a step forward.

from Bio import BiopythonWarning
from Bio.Data import CodonTable
from Bio.Data import IUPACData
from Bio.SeqRecord import SeqRecord
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'll have to remove that, or put it an if-typing conditional or something. It is making a circular import (see the CI failures).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants