How do you ignore specific Pyright type checks by project, file, line?

Question:

I cannot quite find clear documentation on how to ignore one or more specific Pyright checks:

  1. Using a config file at the root of your project.
  2. At the top of a file, function, or method.
  3. Each ligne as a trailing comment.

Thanks in advance for sharing this information.

Asked By: Douglas Lassance

||

Answers:

The usual mypy in-line comments like # type: ignore should work (see details), and for pyright specific config, you can put a pyrightconfig.json in your project root. You can find available config options here. It’s just a JSON file, so it looks something like this:

{
    "venvPath": "/home/username/.virtualenvs/",
    "venv": "myenv",
    "reportOptionalSubscript": false,
    "reportOptionalMemberAccess": false
}

EDIT:

In-source configuration can be as type-ignore statements as supported by mypy. # type: ignore is not a place holder for something else, it is literal. To narrow it down and ignore a specific error (it can be only one of the mypy error codes), like this: # type: ignore[error-code]

To use the specific example of import mentioned in the comments, here are the two variants:

from os import non_existent  # type: ignore[attr-defined]

from missing_module import name  # type: ignore

This is all discussed in the link to the mypy docs I provided, and the list of error codes linked from there.

pyright specific configuration can only be project wide (see EDIT3), either by specifying them in a [tool.pyright] section in your pyproject.toml file, or by creating a pyrightconfig.json like above in your top-level project directory.

EDIT2:

In the comments the OP raised the question how to find the mypy error-codes that correspond to a pyright config option. Unfortunately there’s no easy way besides reading the docs thoroughly along with some understanding of the language; e.g. in the case of from os import name, Python is actually importing the attribute os.name of the module object os into the current namespace. The following interactive session should make this clear:

In [1]: import os

In [2]: type(os)
Out[2]: module

In [3]: locals()["curdir"]
-------------------------------------------------------------------
KeyError                          Traceback (most recent call last)
<ipython-input-3-a31c5782bef1> in <module>
----> 1 locals()["curdir"]

KeyError: 'curdir'

In [4]: from os import curdir

In [5]: locals()["curdir"]
Out[5]: '.'

In [6]: os.curdir == curdir
Out[6]: True

EDIT3:

Pyright also seems to support file-level, and line-level directives, the documentation is hidden under "comments". In short, you can ask pyright to ignore a line or ignore specific pyright errors like this:

import missing_module import name # pyright: ignore
import missing_module import name # pyright: ignore[reportMissingImports]

You can find the list of errors in the configuration docs.

Answered By: suvayu
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.