pymc
pymc copied to clipboard
fix: adding py.typed file as marker for mypy
What is this PR about?
This PR add a new file py.typed
at the root of the module to fix this issue
The goal is to allow mypy analyze
Checklist
- [x] Explain important implementation details 👆
- [x] Make sure that the pre-commit linting/style checks pass.
- [x] Link relevant issues (preferably in nice commit messages)
- [ ] Are the changes covered by tests and docstrings?
- [x] Fill out the short summary sections 👇
Major / Breaking Changes
- None
New features
- Enable mypy type analysis
- Fixes mypy error message
Bugfixes
-
my_file.py:1: error: Skipping analyzing “pymc”: module is installed, but missing library stubs or py.typed marker [import-untyped]
Documentation
Maintenance
- None
:books: Documentation preview :books:: https://pymc--7008.org.readthedocs.build/en/7008/
:sparkling_heart: Thanks for opening this pull request! :sparkling_heart: The PyMC community really appreciates your time and effort to contribute to the project. Please make sure you have read our Contributing Guidelines and filled in our pull request template to the best of your ability.
The pyright docs also make it clear that a package that does type hinting (it doesn't need to be fully typed, or even correctly typed), should have a py.typed file in the root module directory.
This PR makes sense.
py.typed
should go in the package root, not the project root. (Just pushed a commit to fix.)
Our typing seems very rudimentary to me, so I wonder if doing this will lead to more chaos than it solves.
Out of curiosity I checked the situation with scipy, and they still don't have a py.typed
file yet. They added one in https://github.com/scipy/scipy/pull/11946 and removed in in https://github.com/scipy/scipy/pull/12210 shortly after. However, the removal doesn't seem associated with any particular complaint.
@michaelosthege, could you explain what this is for? It seems to be unused.
https://github.com/pymc-devs/pymc/blob/05b557f6460a10c29c3db33690ee535f5b1ecde0/scripts/run_mypy.py#L53-L61
I'm confused with the PEP. It says that we need to include the py.typed file as a package data in pyproject.toml, but maybe this is not necessary thanks to this?
@lucianopaz I think it's included due to setuptools-scm. I verified that it's included in both sdist and wheels.
Ah, no, we're not using setuptools-scm. In any case I verified that it's included.
The enforce_pep561
was a hack to add a py.typed
to Aesara back in the day when the Aesara devs categorically refused to add py.typed
until type hints were flawless.
We used this to already start working on improving PyMC type hints.
Meanwhile, PyTensor has the py.typed
marker so the hack can safely be removed 👍
Thanks a lot @michaelosthege for the context! Seems like you already have some experience with py.typed
. Do you see any potential disadvantages to enabling?
I'm assuming that users who are using pymc as a library may see a bunch of new type errors, but the main userbase is probably people writing notebooks who just want their IDEs to give type hints, so the benefits will greatly outweigh the downsides. Does this seem like an accurate assessment?
Thanks a lot @michaelosthege for the context! Seems like you already have some experience with
py.typed
. Do you see any potential disadvantages to enabling?I'm assuming that users who are using pymc as a library may see a bunch of new type errors, but the main userbase is probably people writing notebooks who just want their IDEs to give type hints, so the benefits will greatly outweigh the downsides. Does this seem like an accurate assessment?
I'll have to try this myself in one project with a rather involved model. If you check the mypy logs, we still have a bunch (>100?) errors that get ignored and many of those are due to ~function dispatching~ method overloading and PyMCs abuse of __new__
in the distribution API.
95 % of user interaction with the PyMC API goes through the distribution API (which totally breaks mypy and IDEs) and another 4 % is via pm.sample*
which is trivial w.r.t. type hints.
@thomasaarholt did you try a real-world project with py.typed
enabled?