From 11ec1a10fdee3d50889e4bab63ac7a52d02a5e96 Mon Sep 17 00:00:00 2001 From: Stephen Morton Date: Wed, 27 Nov 2024 14:21:48 -0800 Subject: [PATCH] Improve the `SyntaxError` constructor; add `SyntaxError.print_file_and_line` (#13141) --- stdlib/@tests/stubtest_allowlists/common.txt | 1 - stdlib/builtins.pyi | 21 +++++++++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/stdlib/@tests/stubtest_allowlists/common.txt b/stdlib/@tests/stubtest_allowlists/common.txt index 323062b22..68d9e0ed4 100644 --- a/stdlib/@tests/stubtest_allowlists/common.txt +++ b/stdlib/@tests/stubtest_allowlists/common.txt @@ -68,7 +68,6 @@ xml.sax.expatreader _thread.RLock _threading_local._localimpl.localargs _threading_local._localimpl.locallock -builtins.SyntaxError.print_file_and_line bz2.BZ2File.peek codecs.StreamReader.charbuffertype codecs.StreamReader.seek diff --git a/stdlib/builtins.pyi b/stdlib/builtins.pyi index 5b387e8c2..4c7467fc8 100644 --- a/stdlib/builtins.pyi +++ b/stdlib/builtins.pyi @@ -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): ...