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: adding py.typed file as marker for mypy #7008

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Drezzerd
Copy link

@Drezzerd Drezzerd commented Nov 13, 2023

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

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

📚 Documentation preview 📚: https://pymc--7008.org.readthedocs.build/en/7008/

Copy link

welcome bot commented Nov 13, 2023

Thank You Banner
💖 Thanks for opening this pull request! 💖 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.

@thomasaarholt
Copy link
Contributor

thomasaarholt commented Jan 29, 2024

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.

@maresb
Copy link
Contributor

maresb commented May 21, 2024

py.typed should go in the package root, not the project root. (Just pushed a commit to fix.)

@maresb
Copy link
Contributor

maresb commented May 21, 2024

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 scipy/scipy#11946 and removed in in scipy/scipy#12210 shortly after. However, the removal doesn't seem associated with any particular complaint.

@maresb
Copy link
Contributor

maresb commented May 21, 2024

@michaelosthege, could you explain what this is for? It seems to be unused.

pymc/scripts/run_mypy.py

Lines 53 to 61 in 05b557f

def enforce_pep561(module_name):
try:
module = importlib.import_module(module_name)
fp = pathlib.Path(module.__path__[0], "py.typed")
if not fp.exists():
fp.touch()
except ModuleNotFoundError:
print(f"Can't enforce PEP 561 for {module_name} because it is not installed.")
return

@lucianopaz
Copy link
Contributor

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?

@maresb
Copy link
Contributor

maresb commented May 21, 2024

@lucianopaz I think it's included due to setuptools-scm. I verified that it's included in both sdist and wheels.

@maresb
Copy link
Contributor

maresb commented May 21, 2024

Ah, no, we're not using setuptools-scm. In any case I verified that it's included.

@michaelosthege
Copy link
Member

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 👍

@maresb
Copy link
Contributor

maresb commented May 21, 2024

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?

@michaelosthege
Copy link
Member

michaelosthege commented May 21, 2024

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?

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.

6 participants