Pridkett
Pridkett

Reputation: 5503

How can mypy ignore a single line in a source file?

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

Upvotes: 286

Views: 320189

Answers (5)

Waket Zheng
Waket Zheng

Reputation: 6331

The answer of this question is:

from yaml import load, dump
try:
    from yaml import CLoader as Loader, CDumper as Dumper
except ImportError:
    from yaml import Loader, Dumper  # type:ignore[assignment]

BTW: 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 example about how to ignore Django migrations (ignore entire file no just one line):

# mypy.ini
[mypy-*.migrations.*]
ignore_errors = True

And for mypy>=0.910, pyproject.toml is supported which can be set as follows:

# pyproject.toml
[tool.mypy]
ignore_missing_imports = true

[[tool.mypy.overrides]]
module = "*.migrations.*"
ignore_errors = true

Upvotes: 13

Abhijit Sarkar
Abhijit Sarkar

Reputation: 24518

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

The above ignores error code empty-body for function knows. If you want to ignore a specific error for the whole file, put the following on top of the file, just after the imports:

# mypy: disable-error-code="empty-body"

Upvotes: 42

Alan Garrido
Alan Garrido

Reputation: 704

Also # mypy: ignore-errors at the top of the file works if you want to ignore all errors in the whole file. If you are using shebang and coding lines, they should be ordered like this:

#!/usr/bin/env python 
#-*- coding: utf-8 -*-
# mypy: ignore-errors

Source: Gvanrossum comment to a related mypy issue

Upvotes: 49

Areza
Areza

Reputation: 6080

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

Upvotes: 11

Salem
Salem

Reputation: 14887

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.

Upvotes: 391

Related Questions