Skip to content

Commit 6074a8f

Browse files
authored
[tty] py312: Fix return types of set(raw|cbreak) (#10785)
Also add `termios._AttrReturn` type alias to be used in tty
1 parent 4ea52b3 commit 6074a8f

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed

stdlib/termios.pyi

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ from _typeshed import FileDescriptorLike
33
from typing import Any
44
from typing_extensions import TypeAlias
55

6-
if sys.platform != "win32":
7-
# Must be a list of length 7, containing 6 ints and a list of NCCS 1-character bytes or ints.
8-
_Attr: TypeAlias = list[int | list[bytes | int]]
6+
# Must be a list of length 7, containing 6 ints and a list of NCCS 1-character bytes or ints.
7+
_Attr: TypeAlias = list[int | list[bytes | int]] | list[int | list[bytes]] | list[int | list[int]]
8+
# Same as _Attr for return types; we use Any to avoid a union.
9+
_AttrReturn: TypeAlias = list[Any]
910

11+
if sys.platform != "win32":
1012
B0: int
1113
B1000000: int
1214
B110: int
@@ -252,7 +254,7 @@ if sys.platform != "win32":
252254
XCASE: int
253255
XTABS: int
254256

255-
def tcgetattr(__fd: FileDescriptorLike) -> list[Any]: ... # Returns _Attr; we use Any to avoid a union in the return type
257+
def tcgetattr(__fd: FileDescriptorLike) -> _AttrReturn: ...
256258
def tcsetattr(__fd: FileDescriptorLike, __when: int, __attributes: _Attr) -> None: ...
257259
def tcsendbreak(__fd: FileDescriptorLike, __duration: int) -> None: ...
258260
def tcdrain(__fd: FileDescriptorLike) -> None: ...

stdlib/tty.pyi

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
import sys
2-
from typing import IO, Any
2+
import termios
3+
from typing import IO
34
from typing_extensions import TypeAlias
45

56
if sys.platform != "win32":
67
__all__ = ["setraw", "setcbreak"]
78
if sys.version_info >= (3, 12):
89
__all__ += ["cfmakeraw", "cfmakecbreak"]
910

11+
_ModeSetterReturn: TypeAlias = termios._AttrReturn
12+
else:
13+
_ModeSetterReturn: TypeAlias = None
14+
1015
_FD: TypeAlias = int | IO[str]
1116

1217
# XXX: Undocumented integer constants
@@ -17,12 +22,9 @@ if sys.platform != "win32":
1722
ISPEED: int
1823
OSPEED: int
1924
CC: int
20-
def setraw(fd: _FD, when: int = 2) -> None: ...
21-
def setcbreak(fd: _FD, when: int = 2) -> None: ...
25+
def setraw(fd: _FD, when: int = 2) -> _ModeSetterReturn: ...
26+
def setcbreak(fd: _FD, when: int = 2) -> _ModeSetterReturn: ...
2227

2328
if sys.version_info >= (3, 12):
24-
# It is: `list[int, int, int, int, int, int, list[str]]
25-
_Mode: TypeAlias = list[Any]
26-
27-
def cfmakeraw(mode: _Mode) -> None: ...
28-
def cfmakecbreak(mode: _Mode) -> None: ...
29+
def cfmakeraw(mode: termios._Attr) -> None: ...
30+
def cfmakecbreak(mode: termios._Attr) -> None: ...

0 commit comments

Comments
 (0)