Description
Hi,
I have a problem with mypy and handling of imports. Basically, I have a Python package with several modules which import other modules of the package. When I run mypy
(version 0.770) on the files, I get this error:
error: Skipping analyzing '.file2': found module but no type hints or library stubs
However, I don't understand that error message because (in this example) file2
does contain type hints.
For the purpose of this issue, I have created a minimal reproducer. I have two files file1
and file2
which both define a class. The class in file1
(Example1
) has a method which uses the class from file2
(Example2
), thus file1
imports file2
with a relative import (from .file2 import Example2
). This is the full code:
file1.py
from .file2 import Example2
class Example1:
def __init__(self, value: float) -> None:
self.value = value
def add(self, other: Example2) -> None:
self.value += other.value
file2.py
class Example2:
def __init__(self, value: float) -> None:
self.value = value
When I run mypy file1.py file2.py
, I am receiving the error mentioned above:
file1.py:1: error: Skipping analyzing '.file2': found module but no type hints or library stubs
file1.py:1: note: See https://mypy.readthedocs.io/en/latest/running_mypy.html#missing-imports
Now I did visit that recommended link in the documentation but I couldn't figure out how to solve my problem.
However, one thing that worked was to turn the relative into a global import, i.e. to write from file2 import Example2
. However, I would like to avoid that because in my package I would prefer to use relative imports for ease of readability of the code (amongst others). Also, the error message does not seem to make sense to me because file2.py
does contain type annotations.
Apologies for posting this because it seems like a basic thing, but I couldn't figure it out with the documentation or by browsing the issues here.
It would be great if someone could explain how to do this properly or point me to a documentation of the solution.
Thanks a lot.
(Python version if 3.8.2).