pysam-developers / pysam

Pysam is a Python package for reading, manipulating, and writing genomics data such as SAM/BAM/CRAM and VCF/BCF files. It's a lightweight wrapper of the HTSlib API, the same one that powers samtools, bcftools, and tabix.
https://pysam.readthedocs.io/en/latest/
MIT License
773 stars 274 forks source link

[BUG][Typing] `PileupColumn` not imported in `libcalignmentfile.pyi` #1298

Open vepain opened 1 month ago

vepain commented 1 month ago

Summary

Python type checkers are not able to correctly infer the type of the object returned by pysam.AlignmentFile.pileup(...).__next__ (i.e. by IteratorColumn.__next__).

For example, mypy==1.10.1 infers it as AlignedSegment.

Observations

In libcalignmentfile.pyi:

from pysam.libcalignedsegment import AlignedSegment

The import pysam.libcalignedsegment.PileupColumn is missing.

The pysam.libcalignedsegment.PileupColumn class is used for typing in:

class IteratorColumn:
    def __iter__(self) -> IteratorRow: ...
    def __next__(self) -> PileupColumn: ...
    # ...

Partial fix

Add the import in libcalignmentfile.pyi:

from pysam.libcalignedsegment import AlignedSegment, PileupColumn

Notes

Something is missing to completely fix this typing bug, as mypy continues to infer AlignedSegment even after the previous partial fix.