48

I need to ignore pyright checking for one line. Is there any special comment for it?

def create_slog(group: SLogGroup, data: Optional[dict] = None):
    SLog.insert_one(SLog(group=group, data=data))  # pyright: disable

# pyright: disable -- doesn't work

Max Block
  • 1,134
  • 3
  • 16
  • 23
  • Pyright isn't something I know much about, but https://github.com/Microsoft/pyright/issues/108 seems to have some interesting proposed ideas. – aschultz Aug 03 '19 at 05:40

2 Answers2

64

Yes it is with "# type: ignore", for example:

try:
    return int(maybe_digits_string)    # type: ignore
except Exception:
    return None
hi2meuk
  • 1,080
  • 11
  • 9
  • 3
    Also, `# type: ignore` is standard and other equivalent checkers will also respect it, so it's definitely the way to go. – WhyNotHugo Jun 14 '20 at 15:52
  • 20
    I have a situation where `mypy` understands my annotation correctly, but `pyright` doesn't. Is there a way to have a directive only for `pyright`? I guess the real resolution is to file a bug report for pyright. – suvayu Apr 15 '21 at 11:07
  • 1
    is there anything that can be applied to a block of code rather than a single line? – pcko1 Oct 14 '21 at 19:21
36

As mentioned in the accepted answer, using a # type: ignore comment is effective.

foo: int = "123"  # type: ignore

But, as mentioned in that answer's comments, using # type: ignore can collide with other type checkers (such as mypy). To work around this, Pyright now supports # pyright: ignore comments (which mypy will not pick up on). This is documented here.

foo: int = "123"  # pyright: ignore

A pyright: ignore comment can be followed by a comma-delimited list of specific pyright rules that should be ignored:

foo: int = "123"  # pyright: ignore [reportPrivateUsage, reportGeneralTypeIssues]

Meanwhile, adding the following comment to the top of your module will disable checking of the listed rules for the whole file:

# pyright: reportUndefinedVariable=false, reportGeneralTypeIssues=false

The pyright docs on comments say "typically this comment is placed at or near the top of a code file on its own line."

Jasha
  • 5,507
  • 2
  • 33
  • 44
  • Couldn't I use this in the `pyrightconfig.json` file? – baggiponte Oct 10 '22 at 08:27
  • I think so. See [the pyright docs on configuration](https://github.com/microsoft/pyright/blob/1f42e17eb3a036271dd17b79a7eb5593c10aae46/docs/configuration.md), specifically the `ignore` option. – Jasha Oct 10 '22 at 19:56
  • actually I checked out their gh issues and found out that atm they [do not intend](https://github.com/microsoft/pyright/issues/3198#issuecomment-1068475439) to implement this – baggiponte Oct 11 '22 at 08:32
  • weirdly, this ignores the whole file :/ – Luís Soares Nov 16 '22 at 17:04