althonos / pyhmmer

Cython bindings and Python interface to HMMER3.
https://pyhmmer.readthedocs.io
MIT License
120 stars 12 forks source link

"Erroneous" type warning? #72

Closed leoburgy closed 1 month ago

leoburgy commented 1 month ago

Hello,

I would like to know why I get a type warning in PyCharm in the conditions described below. In addition, the type hint possibilities are repeated three times in the warning. Nothing is preventing me from using the package but I would love to see no warning at all when false positive ;)

I get the warning

Expected type 'DigitalSequenceBlock | DigitalSequenceBlock | DigitalSequenceBlock | SequenceFile | SequenceFile | SequenceFile', got 'SequenceBlock[Sequence]' instead 

when I write the following code

import pyhmmer
from pathlib import Path

def main():
    path_model = Path("/path/to/PF16531.hmm")  # https://www.ebi.ac.uk/interpro/wwwapi//entry/pfam/PF16531?annotation=hmm

    path_sequences = Path("/path/to/sequences.fasta")  # https://rest.uniprot.org/uniprotkb/accessions?accessions=O62479%2CQ6UVJ0%2CQ7ZVT3%2CQ80UK7&format=fasta

    with pyhmmer.hmmer.HMMFile(path_model) as hmm_file:
        hmm = hmm_file.read()
    with pyhmmer.easel.SequenceFile(path_sequences, digital=True) as seq_file:
        sequences = seq_file.read_block()

    pipeline = pyhmmer.plan7.Pipeline(hmm.alphabet)
    hits = pipeline.search_hmm(hmm, sequences)  # ←

    return 0

if __name__ == '__main__':
    main()

My environment is the following:

Thanks in advance for any information and I am happy to help fix the problem ;)

althonos commented 1 month ago

So the problem here is that SequenceFile is generic over the sequence class (either TextSequence or DigitalSequence) dependening on the value of the digital flag. Last time I checked it wasn't possible to overload the __init__ method of a class with different arguments, so even if you open a SequenceFile in digital mode MyPy thinks that the read_block returns a SequenceBlock[Sequence] instead of a SequenceBlock[DigitalSequence] and you get the warning.

I guess if you write:

sequences: DigitalSequenceBlock = seq_file.read_block()

it should be fine? I don't have a solution at the moment.

leoburgy commented 1 month ago

Thank you for explaining the cause :) I tried to type-hint the returned sequences object; however, in that case the method call get highlighted…