Is it possible to ignore pyright checking for one line?
Question:
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
Answers:
Yes it is with “# type: ignore”, for example:
try:
return int(maybe_digits_string) # type: ignore
except Exception:
return None
As mentioned in the accepted answer, using a # type: ignore
comment is effective.
foo: int = "123" # type: ignore
As mentioned in the accepted 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
This comment can be followed by a comma-delimited list of 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."
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
Yes it is with “# type: ignore”, for example:
try:
return int(maybe_digits_string) # type: ignore
except Exception:
return None
As mentioned in the accepted answer, using a # type: ignore
comment is effective.
foo: int = "123" # type: ignore
As mentioned in the accepted 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
This comment can be followed by a comma-delimited list of 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."