From 09098a8011c2b2d33b0ea542a4d3934ccad34ff0 Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Wed, 27 Sep 2023 10:58:58 +0200 Subject: [PATCH 1/6] [tty] py312: Fix return types of set(raw|cbreak) Also add `termios._AttrReturn` type alias to be used in tty --- stdlib/termios.pyi | 10 ++++++---- stdlib/tty.pyi | 10 ++++++++-- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/stdlib/termios.pyi b/stdlib/termios.pyi index bf8d7bee2473..4570e4e46ebf 100644 --- a/stdlib/termios.pyi +++ b/stdlib/termios.pyi @@ -3,10 +3,12 @@ from _typeshed import FileDescriptorLike from typing import Any from typing_extensions import TypeAlias -if sys.platform != "win32": - # Must be a list of length 7, containing 6 ints and a list of NCCS 1-character bytes or ints. - _Attr: TypeAlias = list[int | list[bytes | int]] +# Must be a list of length 7, containing 6 ints and a list of NCCS 1-character bytes or ints. +_Attr: TypeAlias = list[int | list[bytes | int]] +# Same as _Attr for return types; we use Any to avoid a union. +_AttrReturn: TypeAlias = list[Any] +if sys.platform != "win32": B0: int B1000000: int B110: int @@ -252,7 +254,7 @@ if sys.platform != "win32": XCASE: int XTABS: int - def tcgetattr(__fd: FileDescriptorLike) -> list[Any]: ... # Returns _Attr; we use Any to avoid a union in the return type + def tcgetattr(__fd: FileDescriptorLike) -> _AttrReturn: ... def tcsetattr(__fd: FileDescriptorLike, __when: int, __attributes: _Attr) -> None: ... def tcsendbreak(__fd: FileDescriptorLike, __duration: int) -> None: ... def tcdrain(__fd: FileDescriptorLike) -> None: ... diff --git a/stdlib/tty.pyi b/stdlib/tty.pyi index 43f2e1cf9087..c87f87c1f733 100644 --- a/stdlib/tty.pyi +++ b/stdlib/tty.pyi @@ -1,10 +1,16 @@ import sys +from termios import _AttrReturn from typing import IO from typing_extensions import TypeAlias if sys.platform != "win32": __all__ = ["setraw", "setcbreak"] + if sys.version_info >= (3, 12): + _SetReturn: TypeAlias = _AttrReturn + else: + _SetReturn: TypeAlias = None + _FD: TypeAlias = int | IO[str] # XXX: Undocumented integer constants @@ -15,5 +21,5 @@ if sys.platform != "win32": ISPEED: int OSPEED: int CC: int - def setraw(fd: _FD, when: int = 2) -> None: ... - def setcbreak(fd: _FD, when: int = 2) -> None: ... + def setraw(fd: _FD, when: int = 2) -> _SetReturn: ... + def setcbreak(fd: _FD, when: int = 2) -> _SetReturn: ... From 423cc2ba92dc6f759dafb70bbd1cf70a6e893638 Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Wed, 27 Sep 2023 11:24:55 +0200 Subject: [PATCH 2/6] Fix variance problems --- stdlib/termios.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/termios.pyi b/stdlib/termios.pyi index 4570e4e46ebf..776396cce407 100644 --- a/stdlib/termios.pyi +++ b/stdlib/termios.pyi @@ -4,7 +4,7 @@ from typing import Any from typing_extensions import TypeAlias # Must be a list of length 7, containing 6 ints and a list of NCCS 1-character bytes or ints. -_Attr: TypeAlias = list[int | list[bytes | int]] +_Attr: TypeAlias = list[int | list[bytes | int]] | list[int | list[bytes]] | list[int | list[int]] # Same as _Attr for return types; we use Any to avoid a union. _AttrReturn: TypeAlias = list[Any] From 73e788687cc9ca08613c5d0ce7c91f863f5bfef0 Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Wed, 27 Sep 2023 11:28:58 +0200 Subject: [PATCH 3/6] Change type alias name --- stdlib/tty.pyi | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/stdlib/tty.pyi b/stdlib/tty.pyi index 3fbc591208c2..0882b01dcffb 100644 --- a/stdlib/tty.pyi +++ b/stdlib/tty.pyi @@ -7,9 +7,10 @@ if sys.platform != "win32": __all__ = ["setraw", "setcbreak"] if sys.version_info >= (3, 12): __all__ += ["cfmakeraw", "cfmakecbreak"] - _SetReturn: TypeAlias = _AttrReturn + + _ModeSetterReturn: TypeAlias = _AttrReturn else: - _SetReturn: TypeAlias = None + _ModeSetterReturn: TypeAlias = None _FD: TypeAlias = int | IO[str] @@ -21,8 +22,8 @@ if sys.platform != "win32": ISPEED: int OSPEED: int CC: int - def setraw(fd: _FD, when: int = 2) -> _SetReturn: ... - def setcbreak(fd: _FD, when: int = 2) -> _SetReturn: ... + def setraw(fd: _FD, when: int = 2) -> _ModeSetterReturn: ... + def setcbreak(fd: _FD, when: int = 2) -> _ModeSetterReturn: ... if sys.version_info >= (3, 12): # It is: `list[int, int, int, int, int, int, list[str]] From 7413b8a0e869928e79b7910b66239c0035b67eb1 Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Wed, 27 Sep 2023 11:31:44 +0200 Subject: [PATCH 4/6] Return `termio._Attr` --- stdlib/tty.pyi | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/stdlib/tty.pyi b/stdlib/tty.pyi index 0882b01dcffb..d9d3618cc5bd 100644 --- a/stdlib/tty.pyi +++ b/stdlib/tty.pyi @@ -1,5 +1,5 @@ import sys -from termios import _AttrReturn +from termios import _Attr, _AttrReturn from typing import IO, Any from typing_extensions import TypeAlias @@ -26,8 +26,5 @@ if sys.platform != "win32": def setcbreak(fd: _FD, when: int = 2) -> _ModeSetterReturn: ... if sys.version_info >= (3, 12): - # It is: `list[int, int, int, int, int, int, list[str]] - _Mode: TypeAlias = list[Any] - - def cfmakeraw(mode: _Mode) -> None: ... - def cfmakecbreak(mode: _Mode) -> None: ... + def cfmakeraw(mode: _Attr) -> None: ... + def cfmakecbreak(mode: _Attr) -> None: ... From cd2b46229aa890a513228edf5d6813753ce0e1d1 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 27 Sep 2023 09:32:31 +0000 Subject: [PATCH 5/6] [pre-commit.ci] auto fixes from pre-commit.com hooks --- stdlib/tty.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/tty.pyi b/stdlib/tty.pyi index d9d3618cc5bd..9afb057ad390 100644 --- a/stdlib/tty.pyi +++ b/stdlib/tty.pyi @@ -1,6 +1,6 @@ import sys from termios import _Attr, _AttrReturn -from typing import IO, Any +from typing import IO from typing_extensions import TypeAlias if sys.platform != "win32": From 57ce752d47f5dabee4f1c27a1672a5ea56f4bede Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Wed, 27 Sep 2023 11:40:56 +0200 Subject: [PATCH 6/6] Update imports --- stdlib/tty.pyi | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/stdlib/tty.pyi b/stdlib/tty.pyi index 9afb057ad390..add0d57a8d4b 100644 --- a/stdlib/tty.pyi +++ b/stdlib/tty.pyi @@ -1,5 +1,5 @@ import sys -from termios import _Attr, _AttrReturn +import termios from typing import IO from typing_extensions import TypeAlias @@ -8,7 +8,7 @@ if sys.platform != "win32": if sys.version_info >= (3, 12): __all__ += ["cfmakeraw", "cfmakecbreak"] - _ModeSetterReturn: TypeAlias = _AttrReturn + _ModeSetterReturn: TypeAlias = termios._AttrReturn else: _ModeSetterReturn: TypeAlias = None @@ -26,5 +26,5 @@ if sys.platform != "win32": def setcbreak(fd: _FD, when: int = 2) -> _ModeSetterReturn: ... if sys.version_info >= (3, 12): - def cfmakeraw(mode: _Attr) -> None: ... - def cfmakecbreak(mode: _Attr) -> None: ... + def cfmakeraw(mode: termios._Attr) -> None: ... + def cfmakecbreak(mode: termios._Attr) -> None: ...