8

I am attempting to use MyPy with modules that use ruamel.yaml and Mypy cannot find ruamel.yaml even though Python has no problem finding it. I am puzzled because I can't find a module called YAML.py or class called YAML either, even though these statements work in Python:

from ruamel.yaml import YAML
yaml = YAML()
x = yaml.load()

What do I need to do to get MyPy to recognize ruamel.yaml?

Anthon
  • 69,918
  • 32
  • 186
  • 246
Jonathan
  • 2,635
  • 3
  • 30
  • 49

3 Answers3

7

A workaround is to run without the incremental logic of mypy:

python -m mypy --no-incremental myfile.py

Background

There is a known issue in mypy, see here.

In summary:

Something is not working with the incremental logic of mypy when it is encountering ruamel.

  • When you run it once, all goes ok. This is the command: python -m mypy myfile.py

  • Then, when you run it again, you get an error:

    error: Skipping analyzing 'ruamel': found module but no type hints or library stubs [import]

  • Then, when you run it again, it all goes ok

  • etc.

Arjaan Buijk
  • 1,306
  • 16
  • 18
1

You should not be looking for a file YAML.py. The YAML in

yaml = YAML()

is a class that is defined in ruamel/yaml/main.py and that gets imported into ruamel/yaml/__init__.py (both under site-packages). That is why you do:

from ruamel.yaml import YAML

(the alternative would be that there is a file yaml.py under the directory ruamel, but the loader/dumper is a bit too much to put in one file).

What might work if the above knowledge doesn't help you resolve things, is explicitly set the global flag mypy_path or the environment variable MYPYPATH. This has to include the directory in which the directory ruamel is located.

( I could not find it mentioned in the documentation, but from the source ( mypy/build.py:mypy_path() ) you can see that this is supposed to be a string that gets split on os.pathsep (which is the colon (:) on my Linux based system))

Anthon
  • 69,918
  • 32
  • 186
  • 246
  • I tried this and mypy instructed me to remove the entry, I think because it is already specified in the standard PYTHONPATH since ruamel.yaml is installed in site_packages in a virtual environment on my machine. I suspect that mypy may not take cognizance of the code in your __init__.py file since it uses it's own code for finding Python packages and modules rather than using the standard Python methods. I am looking into the mypy source code right now to determine exactly how it does things. – Jonathan Sep 05 '18 at 22:30
0

I have the same issue.

Even after setting MYPYPATH=./.venv/lib/python3.7/site-packages

A temporary 'solution' is ignoring the missing import exception

mypy --ignore-missing-imports