Is it possible to ignore pyright checking for one line?
Asked Answered
F

2

63

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

Flood answered 3/8, 2019 at 5:12 Comment(1)
Pyright isn't something I know much about, but github.com/Microsoft/pyright/issues/108 seems to have some interesting proposed ideas. – Lavoie
G
84

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

try:
    return int(maybe_digits_string)    # type: ignore
except Exception:
    return None
Goatfish answered 12/12, 2019 at 16:37 Comment(3)
Also, # type: ignore is standard and other equivalent checkers will also respect it, so it's definitely the way to go. πŸ‘πŸ» – Gowon
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. – Rubble
is there anything that can be applied to a block of code rather than a single line? – Ethiopia
L
66

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

Lowery answered 28/12, 2021 at 22:8 Comment(4)
Couldn't I use this in the pyrightconfig.json file? – Hagy
I think so. See the pyright docs on configuration, specifically the ignore option. – Lowery
actually I checked out their gh issues and found out that atm they do not intend to implement this – Hagy
weirdly, this ignores the whole file :/ – Damal

© 2022 - 2024 β€” McMap. All rights reserved.