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

Mypy won't follow imports to local files. " cannot find implementation" #11044

Closed
Luttik opened this issue Sep 2, 2021 · 3 comments
Closed
Labels
bug mypy got something wrong

Comments

@Luttik
Copy link

Luttik commented Sep 2, 2021

Bug Report

The exception "error: Cannot find implementation or library stub for module named "mypy_src_issue_example.a" is thrown for local files that are in scope, even when py.typing is added to the project. An example project is added here: mypy-local-import-issue-example, but the gist is that:

For a file src/my_project/a.py

def foo() -> str:
    ...

and a file src/my_project/a.py

from my_project.a import foo

foo()

The following exception will be thrown:

my_project\b.py:1: error: Cannot find implementation or library stub for module named "my_project.a"
src\my_project\b.py:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
Found 1 error in 1 file (checked 2 source files)

this happens with and without the following pyproject.toml config:

# more non-mypy config above

[tool.mypy]
mypy_path = "src"
python_version = "3.8"

To Reproduce

I've stored a small examplary project here: mypy-local-import-issue-example

Expected Behavior
It should not throw any error since there is no reason why it shouldn't be able to resolve src/my_project/a.py.

Your Environment

  • Mypy version used: "0.910"
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): see above
  • Python version used: 3.8
  • Operating system and version: Windows 10 Pro (build 19043.1165)
@Luttik Luttik added the bug mypy got something wrong label Sep 2, 2021
@hauntsaninja
Copy link
Collaborator

I believe this should work if you set --namespace-packages=True (this should be made the default, see #8584)

@pietermarsman
Copy link

An alternative solution is to add a __init__.py file to the directory. This indicates that the directory is a python module.

@KomaGR
Copy link

KomaGR commented Oct 19, 2023

An alternative solution is to add a __init__.py file to the directory. This indicates that the directory is a python module.

Why might this solution not be working for me? I'm not entirely familiar with python modules but this didn't work whereas adding --namespace-packages=True worked.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

4 participants