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

2

15

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.

Necessarily answered 17/8, 2021 at 18:53 Comment(5)
Did you check out the configuration documentation?Vinaigrette
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.Baggott
@Vinaigrette it does cover the config file at the root of your project, but not my two other bullets as far as I could tell.Necessarily
Thanks, @suvayu, could you post an example of the exact syntax for the mypy directives?Necessarily
I added my comment along with the example you asked for as an answer. Hope this helps.Baggott
B
15

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.

Baggott answered 19/8, 2021 at 12:13 Comment(6)
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.Necessarily
@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.Baggott
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?Necessarily
@DouglasLassance unfortunately no easy way, I added some explanation in my answer, hopefully that makes it clear how I arrived at that conclusion.Baggott
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 and this related StackOverflow answers.Rodrickrodrigez
@CecilCurry I have updated my answer, can we tone down the hyperbole now?Baggott
M
1

You can ignore the types for an entire file by placing #type: ignore at the start of the file.

Matrix answered 10/1 at 0:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.