pymc icon indicating copy to clipboard operation
pymc copied to clipboard

fix: adding py.typed file as marker for mypy

Open Drezzerd opened this issue 1 year ago • 11 comments

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

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

Drezzerd avatar Nov 13 '23 10:11 Drezzerd

Thank You Banner :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.

welcome[bot] avatar Nov 13 '23 10:11 welcome[bot]

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.

thomasaarholt avatar Jan 29 '24 17:01 thomasaarholt

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

maresb avatar May 21 '24 10:05 maresb

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.

maresb avatar May 21 '24 10:05 maresb

@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

maresb avatar May 21 '24 10:05 maresb

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 avatar May 21 '24 11:05 lucianopaz

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

maresb avatar May 21 '24 12:05 maresb

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

maresb avatar May 21 '24 12:05 maresb

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 👍

michaelosthege avatar May 21 '24 18:05 michaelosthege

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?

maresb avatar May 21 '24 21:05 maresb

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?

michaelosthege avatar May 21 '24 23:05 michaelosthege