1

I created a package "mypackage" that uses typing hints.

I import mypackage in a new module (not part of mypackage), mymodule, that also used typing hints.

I would like to apply mypy on mymodule, but get:

error: Cannot find implementation or library stub for module named 'mypackage'

I have been reading the documentation about stubs:

https://mypy.readthedocs.io/en/stable/stubs.html#stub-files

Yet, it is unclear to me what the best practices are.

Here something that works, but feels inefficient.

  • I create a package "mypackage". All the code is typed.
  • I call stubgen at the root of the package, it creates the folder ./out/mypackage with the stub files
  • I move this generated directory to a folder which has the stubs for my other packages, e.g. /path/to/stubs
  • I set the environment variable MYPYPATH to /path/to/stubs

mypy mymodule.py

now works, but is this the best way of doing things?

  • the information about the types is already in the source code, yet the information is duplicated in pyi files
  • we work as a group: everybody has to generate the stubs and copy them to their local stubs folder
  • everybody has to regenerate the stubs again when the code is updated

Most likely, I am missing something. Is there a simpler way to apply mypy to mymodule ?

Vince
  • 3,979
  • 10
  • 41
  • 69
  • I think posixsubprocess files are just the way to type annotate the code you don't want to touch. [This](https://stackoverflow.com/questions/59051631/what-is-the-use-of-stub-files-pyi-in-python) question has very nice answer on that topic – sudden_appearance May 11 '22 at 12:46
  • @sudden_appearance I saw this answer, and it is one of the things which triggered my question. "You can also add type hints directly in the .py module like the following [...] But there are some cases where you want to keep them separate in stubs [...]". So it seems to indicate that if the type hints are in the .py source, you may not need to have to use .pyi files. But I could not figure out how to get things to work without the stubs. – Vince May 11 '22 at 12:50
  • If you mean by _keeping separate_ that different parameter types result in different return types (which is done by overloading in stub files), then it can't be achieved without stub files (assuming `Union[]` or ` | ` are not applicable). But I think using stub files for proper type hint is overkill – sudden_appearance May 11 '22 at 12:57
  • @sudden_appearance "using stub files for proper type hint is overkill" : is there any way not using stud files that would allow me to apply mypy on packages that import "mypackage"? I will edit my question to make it clearer – Vince May 11 '22 at 15:57

1 Answers1

1

The error message in the question was displayed when using mypy version 0.761, but version 0.950 gives better information, as it points to:

https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports

which points in turn to:

https://mypy.readthedocs.io/en/stable/installed_packages.html#installed-packages

It indicates that the setup.py of mypackage should include:

from distutils.core import setup

setup(
    # ...
    package_data={"mypackage": ["py.typed"]},
    #...
)

and an empty file py.typed should be placed in the mypackage folder.

After mypackage is (pip) installed, then applying mypy on mymodule works fine.

Vince
  • 3,979
  • 10
  • 41
  • 69