Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
I'm using
mypy
in my python project for type checking. I'm also using PyYAML for reading and writing the project configuration files. Unfortunately, when using the
recommended import mechanism from the PyYAML documentation
this generates a spurious error in a try/except clause that attempts to import native libraries:
from yaml import load, dump
from yaml import CLoader as Loader, CDumper as Dumper
except ImportError:
from yaml import Loader, Dumper
On my system CLoader and CDumper aren't present, which results in the errors error: Module 'yaml' has no attribute 'CLoader' and error: Module 'yaml' has no attribute 'CDumper'.
Is there a way to have mypy ignore errors on this line? I was hoping that I could do something like this to have mypy skip that line:
from yaml import load, dump
from yaml import CLoader as Loader, CDumper as Dumper # nomypy
except ImportError:
from yaml import Loader, Dumper
You can ignore type errors with # type: ignore as of version 0.2 (see issue #500, Ignore specific lines):
PEP 484 uses # type: ignore for ignoring type errors on particular lines ...
Also, using # type: ignore close to the top of a file [skips] checking that file altogether.
Source: mypy#500. See also the mypy documentation.
–
–
–
Also # mypy: ignore-errors at the top of the file you want to ignore all errors works, if you are using shebang and coding lines should be like this:
#!/usr/bin/env python
#-*- coding: utf-8 -*-
# mypy: ignore-errors
Gvanrossum comment
–
–
Of course, the answer of this question is add # type:ignore at the end of the line that want mypy to ignore it.
When I was google for how to ignore the files for django migrations,
this question was recomment to me several times.
So I post an answer about how to ignore Django migrations:
# mypy.ini
[mypy-*.migrations.*]
ignore_errors = True
And for mypy>=0.910, pyproject.toml is supported which can be set as follows:
[tool.mypy]
python_version = 3.8
ignore_missing_imports = true
[[tool.mypy.overrides]]
module = "*.migrations.*"
ignore_errors = true
–
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.