niltz
niltz

Reputation: 1128

MyPy can't find types for my local package

I have two python packages. One is a util library, and one is an application that will use the util library (eventually I will have more apps sharing the library.

I am using poetry for both, and the app specifies the common library as a dependency using the path and develop properties.

For example, my layout looks something like this:

- common/
   - common/
      - __init__.py
      - py.typed
   - pyproject.toml

- myapp/
   - myapp/
      - __init__.py
      - py.typed
   - pyproject.toml

And the myapp\pyproject.toml looks something like this:

[tool.poetry]
name = "myapp"
version = "0.1.0"
description = ""
authors = ["Your Name <[email protected]>"]

[tool.poetry.dependencies]
python = "^3.9"
common = { path = "../common", develop = true }

[tool.poetry.dev-dependencies]
mypy = "^0.910"
flake8 = "^4.0.1"
black = {version = "^21.12b0", allow-prereleases = true}
pytest = "^6.2.5"
pytest-cov = "^3.0.0"
pytest-mock = "^3.6.1"

[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"

When I run mypy on myapp I get something like:

myapp/__init__.py:1:1: error: Cannot find implementation or library stub for module named "common"  [import]

Upvotes: 12

Views: 6855

Answers (3)

nikniknik
nikniknik

Reputation: 171

There is a known issue in mypy, where py.typed is not recognized in editable installs: https://github.com/python/mypy/issues/13392

Pyright has some workarounds, which work in mypy also: https://microsoft.github.io/pyright/#/import-resolution?id=editable-installs

For setuptools try to force the legacy behavior (compat mode) with:

pip install -e . --config-settings editable_mode=compat

Upvotes: 4

user76284
user76284

Reputation: 1328

The following worked for me: Add your library's path to the MYPYPATH environment variable:

export MYPYPATH=$MYPYPATH:path/to/common

You can set this in your bash profile, if desired. I don't know if there's a cleaner way to make mypy detect such local packages automatically. The documentation says:

Cannot find implementation or library stub

If you are getting a Cannot find implementation or library stub for module error, this means mypy was not able to find the module you are trying to import, whether it comes bundled with type hints or not. If you are getting this error, try:

  1. Reading the How imports are found section below to make sure you understand how exactly mypy searches for and finds modules and modify how you’re invoking mypy accordingly.

  2. Directly specifying the directory containing the module you want to type check from the command line, by using the mypy_path or files config file options, or by using the MYPYPATH environment variable.

Upvotes: -2

Giorgio Balestrieri
Giorgio Balestrieri

Reputation: 1062

Assuming the "util" package has type hints, you'll want to add a py.typed file to it (in the root directory of the package) so mypy understands it comes with type hints. py.typed should be empty, it's just a flag file.

If your package does not have type hints, then you'd have to add stubs files (.pyi).

More details: https://mypy.readthedocs.io/en/stable/installed_packages.html#creating-pep-561-compatible-packages

Upvotes: 7

Related Questions