Skip to content

Use a protocol for shlex.instream et al. #11452

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions stdlib/shlex.pyi
Original file line number Diff line number Diff line change
@@ -1,23 +1,32 @@
import sys
from collections import deque
from collections.abc import Iterable
from typing import TextIO, overload
from io import TextIOWrapper
from typing import Literal, Protocol, overload, type_check_only
from typing_extensions import Self, deprecated

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

@type_check_only
class _ShlexInstream(Protocol):
def read(self, size: Literal[1], /) -> str: ...
def readline(self) -> object: ...
def close(self) -> object: ...

if sys.version_info >= (3, 12):
def split(s: str | TextIO, comments: bool = False, posix: bool = True) -> list[str]: ...
def split(s: str | _ShlexInstream, comments: bool = False, posix: bool = True) -> list[str]: ...

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

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

# TODO: Make generic over infile once PEP 696 is implemented.
class shlex(Iterable[str]):
commenters: str
wordchars: str
Expand All @@ -27,26 +36,27 @@ class shlex(Iterable[str]):
escapedquotes: str
whitespace_split: bool
infile: str | None
instream: TextIO
instream: _ShlexInstream
source: str
debug: int
lineno: int
token: str
filestack: deque[tuple[str | None, _ShlexInstream, int]]
eof: str | None
@property
def punctuation_chars(self) -> str: ...
def __init__(
self,
instream: str | TextIO | None = None,
instream: str | _ShlexInstream | None = None,
infile: str | None = None,
posix: bool = False,
punctuation_chars: bool | str = False,
) -> None: ...
def get_token(self) -> str | None: ...
def push_token(self, tok: str) -> None: ...
def read_token(self) -> str | None: ...
def sourcehook(self, newfile: str) -> tuple[str, TextIO] | None: ...
def push_source(self, newstream: str | TextIO, newfile: str | None = None) -> None: ...
def sourcehook(self, newfile: str) -> tuple[str, TextIOWrapper] | None: ...
def push_source(self, newstream: str | _ShlexInstream, newfile: str | None = None) -> None: ...
def pop_source(self) -> None: ...
def error_leader(self, infile: str | None = None, lineno: int | None = None) -> str: ...
def __iter__(self) -> Self: ...
Expand Down