Skip to content

Commit df409a2

Browse files
authored
Use a protocol for shlex.instream et al. (#11452)
1 parent bb6613f commit df409a2

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

stdlib/shlex.pyi

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,32 @@
11
import sys
2+
from collections import deque
23
from collections.abc import Iterable
3-
from typing import TextIO, overload
4+
from io import TextIOWrapper
5+
from typing import Literal, Protocol, overload, type_check_only
46
from typing_extensions import Self, deprecated
57

68
__all__ = ["shlex", "split", "quote", "join"]
79

10+
@type_check_only
11+
class _ShlexInstream(Protocol):
12+
def read(self, size: Literal[1], /) -> str: ...
13+
def readline(self) -> object: ...
14+
def close(self) -> object: ...
15+
816
if sys.version_info >= (3, 12):
9-
def split(s: str | TextIO, comments: bool = False, posix: bool = True) -> list[str]: ...
17+
def split(s: str | _ShlexInstream, comments: bool = False, posix: bool = True) -> list[str]: ...
1018

1119
else:
1220
@overload
13-
def split(s: str | TextIO, comments: bool = False, posix: bool = True) -> list[str]: ...
21+
def split(s: str | _ShlexInstream, comments: bool = False, posix: bool = True) -> list[str]: ...
1422
@overload
1523
@deprecated("Passing None for 's' to shlex.split() is deprecated and will raise an error in Python 3.12.")
1624
def split(s: None, comments: bool = False, posix: bool = True) -> list[str]: ...
1725

1826
def join(split_command: Iterable[str]) -> str: ...
1927
def quote(s: str) -> str: ...
2028

29+
# TODO: Make generic over infile once PEP 696 is implemented.
2130
class shlex(Iterable[str]):
2231
commenters: str
2332
wordchars: str
@@ -27,26 +36,27 @@ class shlex(Iterable[str]):
2736
escapedquotes: str
2837
whitespace_split: bool
2938
infile: str | None
30-
instream: TextIO
39+
instream: _ShlexInstream
3140
source: str
3241
debug: int
3342
lineno: int
3443
token: str
44+
filestack: deque[tuple[str | None, _ShlexInstream, int]]
3545
eof: str | None
3646
@property
3747
def punctuation_chars(self) -> str: ...
3848
def __init__(
3949
self,
40-
instream: str | TextIO | None = None,
50+
instream: str | _ShlexInstream | None = None,
4151
infile: str | None = None,
4252
posix: bool = False,
4353
punctuation_chars: bool | str = False,
4454
) -> None: ...
4555
def get_token(self) -> str | None: ...
4656
def push_token(self, tok: str) -> None: ...
4757
def read_token(self) -> str | None: ...
48-
def sourcehook(self, newfile: str) -> tuple[str, TextIO] | None: ...
49-
def push_source(self, newstream: str | TextIO, newfile: str | None = None) -> None: ...
58+
def sourcehook(self, newfile: str) -> tuple[str, TextIOWrapper] | None: ...
59+
def push_source(self, newstream: str | _ShlexInstream, newfile: str | None = None) -> None: ...
5060
def pop_source(self) -> None: ...
5161
def error_leader(self, infile: str | None = None, lineno: int | None = None) -> str: ...
5262
def __iter__(self) -> Self: ...

0 commit comments

Comments
 (0)