11

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.

  • Did you check out the configuration documentation? – gshpychka Aug 17 '21 at 19:01
  • The usual `mypy` in-line comments should work, and for `pyright` specific config, you can put a `pyrightconfig.json` in your project root. You can find available config options [here](https://github.com/microsoft/pyright/blob/main/docs/configuration.md#type-check-diagnostics-settings). – suvayu Aug 18 '21 at 11:39
  • @gshpychka it does cover the config file at the root of your project, but not my two other bullets as far as I could tell. – Douglas Lassance Aug 18 '21 at 17:55
  • Thanks, @suvayu, could you post an example of the exact syntax for the mypy directives? – Douglas Lassance Aug 18 '21 at 17:55
  • I added my comment along with the example you asked for as an answer. Hope this helps. – suvayu Aug 19 '21 at 12:14

1 Answers1

10

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.

suvayu
  • 4,271
  • 2
  • 29
  • 35
  • Thanks, @suvayu. I tried the suggested template for single-line ignores. The line `from absent_package import absent_module # reportMissingImports: ignore` won't work for me. Context SublimeText 4 with LSP-pyright extension. – Douglas Lassance Aug 21 '21 at 00:38
  • @DouglasLassance You misunderstood what I wrote. for in-source comments, you have to use type-ignore statements as supported by `mypy` (the 1st link), and pyright specific options can only be configured project wide in the configuration file, the available options for which are in the 2nd link. – suvayu Aug 21 '21 at 11:01
  • Thanks, @suvayu. It was not clear out the bat that for suppressing Pyright messages I had to use Mypy codes. Is there a reliable way to find the equivalence. How does one go from `reportMissingImports` to `attr-defined`? – Douglas Lassance Aug 23 '21 at 17:39
  • @DouglasLassance unfortunately no easy way, I added some explanation in my answer, hopefully that makes it clear how I arrived at that conclusion. – suvayu Aug 23 '21 at 19:08
  • 1
    **This answer is *absolutely* untrue.** Pyright provides a pyright-specific `# pyright: ignore[...]` comment pragma for ignoring pyright-specific error codes on a line-by-line basis (e.g., `# pyright: ignore[reportOptionalMemberAccess]`). See also [this](https://stackoverflow.com/a/70513059/2809027) and [this related StackOverflow answers](https://stackoverflow.com/a/72322304/2809027). – Cecil Curry Jun 16 '22 at 07:25
  • @CecilCurry I have updated my answer, can we tone down the hyperbole now? – suvayu Jun 16 '22 at 14:28