Use Mypy with Ruamel.yaml
Asked Answered
F

4

10

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?

Familist answered 5/9, 2018 at 15:54 Comment(0)
E
8

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.

Erastian answered 9/12, 2020 at 18:45 Comment(0)
K
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))

Kienan answered 5/9, 2018 at 16:34 Comment(1)
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.Familist
F
1

According to the ticket 7276, adding --no-incremental resolves the problem. You may also prefer to set the parameter incremental = false in the [mypy] section of your mypy.ini configuration file.

I tested this solution, and it does solve the problem in my environment.

Feigned answered 25/4 at 16:29 Comment(1)
The links to the issues are great (thanks!) and make me wonder why the two sides have to point fingers back and forth instead of fixing this bug. (I'm inclined to describe this as a mypy bug in that it's not handling something that's explicitly listed as valid in an accepted PEP.)Geyer
G
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

Goidelic answered 9/7, 2019 at 13:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.