52

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

1

2 Answers 2

69

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

try:
    return int(maybe_digits_string)    # type: ignore
except Exception:
    return None
3
  • 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, 2020 at 15:52
  • 21
    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, 2021 at 11:07
  • 1
    is there anything that can be applied to a block of code rather than a single line?
    – pcko1
    Oct 14, 2021 at 19:21
40

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."

4
  • Couldn't I use this in the pyrightconfig.json file?
    – baggiponte
    Oct 10, 2022 at 8:27
  • I think so. See the pyright docs on configuration, specifically the ignore option.
    – Jasha
    Oct 10, 2022 at 19:56
  • actually I checked out their gh issues and found out that atm they do not intend to implement this
    – baggiponte
    Oct 11, 2022 at 8:32
  • weirdly, this ignores the whole file :/ Nov 16, 2022 at 17:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.