Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix typechecking for top-level samtools commands like pysam.view(…) #1315

Merged
merged 3 commits into from
Oct 24, 2024

Conversation

jmarshall
Copy link
Member

As reported in PR #1306, mypy rejects pysam.view(…) et al because it doesn't know that they exist. This is due to the definition of __all__ in pysam/samtools.py: either HAVE_FINAL is True for mypy so it skips the __all__ definition, or the definition is complex enough that mypy does not evaluate it, or both (as #1306 did not fix the problem).

The first commit fixes this by lifting the __all__ definition to the top-level and making it a simple assignment of a literal list. The second commit simplifies the rest of the code in pysam/samtools.py and pysam/bcftools.py.

It seems that mypy acts on this value better if it is a simple literal.
This fixes type checking for top-level calls such as `pysam.view(...)`.
Hat tip @msto, who reported the problem and proposed a draft fix.

Add test case checking type hints for samtools and bcftools calls.
Simplify the code by just writing these out directly, and hence letting
type checkers just infer the obvious type. Make PysamDispatch(parsers=...)
an optional parameter, as nothing now uses these parsers anyway.

Remove "import_", which was added as an alias of "fqimport" in PR pysam-developers#1143
without announcement or documentation and seemingly accidentally. The
redundant "import_" appeared in v0.21.0, but "fqimport" has been present
since v0.17.0.

Define __all__ in pysam/bcftools.py for good measure.

Import all of pysam.utils to avoid pysam.{sam,bcf}tools.PysamDispatcher
namespace pollution -- pysam.utils will be imported anyway.
@jmarshall
Copy link
Member Author

This also removes the pysam.samtools.import_(…) alias of pysam.samtools.fqimport(…), which was quietly added without review during PR #1143.

@jmarshall jmarshall force-pushed the subcmd-typechecking branch from 3ab9506 to 256adfc Compare October 22, 2024 20:58
@jmarshall jmarshall merged commit effb935 into pysam-developers:master Oct 24, 2024
12 checks passed
@jmarshall jmarshall deleted the subcmd-typechecking branch October 24, 2024 09:39
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.

1 participant