229

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
try:
    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
try:
    from yaml import CLoader as Loader, CDumper as Dumper  # nomypy
except ImportError:
    from yaml import Loader, Dumper
Pridkett
  • 4,883
  • 4
  • 30
  • 47

5 Answers5

321

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.

Salem
  • 13,516
  • 4
  • 51
  • 70
  • 10
    Any idea how to put the # type: ignore on the line above? – Greg Hilston Aug 27 '19 at 16:33
  • 9
    my prospector linter requires a certain length – Greg Hilston Aug 30 '19 at 21:45
  • 2
    @GregHilston ah, ouch. Unfortunately I don't know if this is possible [(PEP 484 doesn't seem to list many other options)](https://www.python.org/dev/peps/pep-0484/#compatibility-with-other-uses-of-function-annotations), though it is possible to ignore an entire function ([mypy#557](https://github.com/python/mypy/issues/557)). – Salem Aug 30 '19 at 21:51
  • 2
    @GregHilston you could split the line using `\\` or parenthesis and write the comment to the line where the mypy error occurs. – steffen Dec 29 '19 at 19:43
  • 11
    To be more specific, MyPy supports specifying the error code/s to ignore with `type: ignore[code, ...]`. See https://mypy.readthedocs.io/en/stable/error_codes.html?#silencing-errors-based-on-error-codes. In this case, it would be `# type: ignore[attr-defined]`. – Gino Mempin Jan 02 '21 at 11:00
  • 2
    thanks @GinoMempin and for other cases, you can run mypy with the `--show-error-codes` flag to see the codes – teichert Jun 10 '21 at 18:18
39

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

Alan Garrido
  • 604
  • 5
  • 16
  • 3
    Note this ignore **all** errors on the **whole** file, not just errors on a single line like what OP was asking about. – bfontaine Feb 18 '22 at 17:46
  • 1
    That's why I write "all", sure it is not the accepted answer like the OP wanted, but just an complement to this thread. If you want to add to the answer write an edit or another answer :) – Alan Garrido Feb 19 '22 at 00:16
11

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
Waket Zheng
  • 5,065
  • 2
  • 17
  • 30
  • 15
    Note that this will ignore errors *for the entire file* and *for all types of errors*, not just for the error on a *specific line* (which the OP was originally asking about). – Gino Mempin Jan 02 '21 at 10:58
3

I used

# type: ignore # noqa: F401

to ignore one line that was giving me F401 error. I am sure you can expand it to other error codes

Areza
  • 5,623
  • 7
  • 48
  • 79
1

Note that # type: ignore will ignore all errors. If you don't want that, ignore only the specific error code.

Example:

def knows(a: int, b: int) -> bool:  # type: ignore[empty-body]
    pass
Abhijit Sarkar
  • 21,927
  • 20
  • 110
  • 219