From d0e66990ce5e8aa357a8af7e9d433fb93ccfef2e Mon Sep 17 00:00:00 2001 From: Kevin Kirsche Date: Thu, 18 Aug 2022 08:38:32 -0400 Subject: [PATCH 1/4] fix: Update sys.exit and SystemExit exception to have the same types fix: #8513 --- stdlib/builtins.pyi | 2 +- stdlib/sys.pyi | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/stdlib/builtins.pyi b/stdlib/builtins.pyi index 1bcd4746c7a3..354d42b41ac5 100644 --- a/stdlib/builtins.pyi +++ b/stdlib/builtins.pyi @@ -1798,7 +1798,7 @@ class GeneratorExit(BaseException): ... class KeyboardInterrupt(BaseException): ... class SystemExit(BaseException): - code: int + code: str | int | None class Exception(BaseException): ... diff --git a/stdlib/sys.pyi b/stdlib/sys.pyi index a1c875561a87..a3f987364fd9 100644 --- a/stdlib/sys.pyi +++ b/stdlib/sys.pyi @@ -222,7 +222,7 @@ if sys.version_info >= (3, 11): def exception() -> BaseException | None: ... # sys.exit() accepts an optional argument of anything printable -def exit(__status: object = ...) -> NoReturn: ... +def exit(__status: str | int | None = ...) -> NoReturn: ... def getallocatedblocks() -> int: ... def getdefaultencoding() -> str: ... From dd8fa7b79ef0b6b2339233e978f8e9d4c51b614e Mon Sep 17 00:00:00 2001 From: Kevin Kirsche Date: Thu, 18 Aug 2022 12:19:03 -0400 Subject: [PATCH 2/4] fix: add missing methods and use a type alias for exit code --- stdlib/builtins.pyi | 6 +++--- stdlib/sys.pyi | 3 ++- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/stdlib/builtins.pyi b/stdlib/builtins.pyi index 354d42b41ac5..04e94f2222a4 100644 --- a/stdlib/builtins.pyi +++ b/stdlib/builtins.pyi @@ -1281,7 +1281,7 @@ else: __locals: Mapping[str, object] | None = ..., ) -> None: ... -def exit(code: object = ...) -> NoReturn: ... +def exit(code: sys._ExitCode = ...) -> NoReturn: ... class filter(Iterator[_T], Generic[_T]): @overload @@ -1620,7 +1620,7 @@ else: @overload def pow(__base: _SupportsSomeKindOfPow, __exp: complex, __mod: None = ...) -> complex: ... -def quit(code: object = ...) -> NoReturn: ... +def quit(code: sys._ExitCode = ...) -> NoReturn: ... class reversed(Iterator[_T], Generic[_T]): @overload @@ -1798,7 +1798,7 @@ class GeneratorExit(BaseException): ... class KeyboardInterrupt(BaseException): ... class SystemExit(BaseException): - code: str | int | None + code: sys._ExitCode class Exception(BaseException): ... diff --git a/stdlib/sys.pyi b/stdlib/sys.pyi index a3f987364fd9..058e16a14d33 100644 --- a/stdlib/sys.pyi +++ b/stdlib/sys.pyi @@ -11,6 +11,7 @@ from typing_extensions import Literal, TypeAlias, final _T = TypeVar("_T") +_ExitCode: TypeAlias = str | int | None _OptExcInfo: TypeAlias = OptExcInfo # noqa: Y047 # TODO: obsolete, remove fall 2022 or later # Intentionally omits one deprecated and one optional method of `importlib.abc.MetaPathFinder` @@ -222,7 +223,7 @@ if sys.version_info >= (3, 11): def exception() -> BaseException | None: ... # sys.exit() accepts an optional argument of anything printable -def exit(__status: str | int | None = ...) -> NoReturn: ... +def exit(__status: _ExitCode = ...) -> NoReturn: ... def getallocatedblocks() -> int: ... def getdefaultencoding() -> str: ... From c6fc39ae8d46d0e17e4fa5fbe56c72d004780bd6 Mon Sep 17 00:00:00 2001 From: Kevin Kirsche Date: Thu, 18 Aug 2022 12:22:56 -0400 Subject: [PATCH 3/4] fix: delete outdated comment --- stdlib/sys.pyi | 1 - 1 file changed, 1 deletion(-) diff --git a/stdlib/sys.pyi b/stdlib/sys.pyi index 058e16a14d33..59f408ace107 100644 --- a/stdlib/sys.pyi +++ b/stdlib/sys.pyi @@ -222,7 +222,6 @@ def exc_info() -> OptExcInfo: ... if sys.version_info >= (3, 11): def exception() -> BaseException | None: ... -# sys.exit() accepts an optional argument of anything printable def exit(__status: _ExitCode = ...) -> NoReturn: ... def getallocatedblocks() -> int: ... def getdefaultencoding() -> str: ... From b7a628202d3fb960ad5d423e195e02c7833dd61a Mon Sep 17 00:00:00 2001 From: Kevin Kirsche Date: Thu, 18 Aug 2022 12:24:46 -0400 Subject: [PATCH 4/4] fix: add rationale comment --- stdlib/sys.pyi | 1 + 1 file changed, 1 insertion(+) diff --git a/stdlib/sys.pyi b/stdlib/sys.pyi index 59f408ace107..d8ef0e386107 100644 --- a/stdlib/sys.pyi +++ b/stdlib/sys.pyi @@ -11,6 +11,7 @@ from typing_extensions import Literal, TypeAlias, final _T = TypeVar("_T") +# see https://github.com/python/typeshed/issues/8513#issue-1333671093 for the rationale behind this alias _ExitCode: TypeAlias = str | int | None _OptExcInfo: TypeAlias = OptExcInfo # noqa: Y047 # TODO: obsolete, remove fall 2022 or later