Improve the SyntaxError constructor; add SyntaxError.print_file_and_line (#13141)

This commit is contained in:
Stephen Morton
2024-11-27 14:21:48 -08:00
committed by GitHub
parent 696e631330
commit 11ec1a10fd
2 changed files with 20 additions and 2 deletions

View File

@@ -1963,14 +1963,33 @@ class StopAsyncIteration(Exception):
class SyntaxError(Exception):
msg: str
filename: str | None
lineno: int | None
offset: int | None
text: str | None
filename: str | None
# Errors are displayed differently if this attribute exists on the exception.
# The value is always None.
print_file_and_line: None
if sys.version_info >= (3, 10):
end_lineno: int | None
end_offset: int | None
@overload
def __init__(self) -> None: ...
@overload
def __init__(self, msg: object, /) -> None: ...
# Second argument is the tuple (filename, lineno, offset, text)
@overload
def __init__(self, msg: str, info: tuple[str | None, int | None, int | None, str | None], /) -> None: ...
if sys.version_info >= (3, 10):
# end_lineno and end_offset must both be provided if one is.
@overload
def __init__(
self, msg: str, info: tuple[str | None, int | None, int | None, str | None, int | None, int | None], /
) -> None: ...
# If you provide more than two arguments, it still creates the SyntaxError, but
# the arguments from the info tuple are not parsed. This form is omitted.
class SystemError(Exception): ...
class TypeError(Exception): ...
class ValueError(Exception): ...