From 81bea274f3148d66c994d3aa8a4cbcdd703de816 Mon Sep 17 00:00:00 2001 From: Pavel Karateev Date: Mon, 31 May 2021 18:25:10 +0300 Subject: [PATCH 1/3] Add encoding & errors params to fileinput `fileinput.input()`, `fileinput.FileInput()`, and `fileinput.hook_compressed()` have two optional extra parameters `encoding` and `errors` in Python 3.10 --- stdlib/fileinput.pyi | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/stdlib/fileinput.pyi b/stdlib/fileinput.pyi index ed07ebecde86..0ce06e9a1114 100644 --- a/stdlib/fileinput.pyi +++ b/stdlib/fileinput.pyi @@ -2,7 +2,19 @@ import sys from _typeshed import StrOrBytesPath from typing import IO, Any, AnyStr, Callable, Generic, Iterable, Iterator, Optional, Union -if sys.version_info >= (3, 8): +if sys.version_info >= (3, 10): + def input( + files: Union[StrOrBytesPath, Iterable[StrOrBytesPath], None] = ..., + inplace: bool = ..., + backup: str = ..., + *, + mode: str = ..., + openhook: Callable[[StrOrBytesPath, str], IO[AnyStr]] = ..., + encoding: Optional[str] = ..., + errors: Optional[str] = ..., + ) -> FileInput[AnyStr]: ... + +elif sys.version_info >= (3, 8): def input( files: Union[StrOrBytesPath, Iterable[StrOrBytesPath], None] = ..., inplace: bool = ..., @@ -32,7 +44,19 @@ def isfirstline() -> bool: ... def isstdin() -> bool: ... class FileInput(Iterable[AnyStr], Generic[AnyStr]): - if sys.version_info >= (3, 8): + if sys.version_info >= (3, 10): + def __init__( + self, + files: Union[None, StrOrBytesPath, Iterable[StrOrBytesPath]] = ..., + inplace: bool = ..., + backup: str = ..., + *, + mode: str = ..., + openhook: Callable[[StrOrBytesPath, str], IO[AnyStr]] = ..., + encoding: Optional[str] = ..., + errors: Optional[str] = ..., + ) -> None: ... + elif sys.version_info >= (3, 8): def __init__( self, files: Union[None, StrOrBytesPath, Iterable[StrOrBytesPath]] = ..., @@ -68,5 +92,12 @@ class FileInput(Iterable[AnyStr], Generic[AnyStr]): def isfirstline(self) -> bool: ... def isstdin(self) -> bool: ... -def hook_compressed(filename: StrOrBytesPath, mode: str) -> IO[Any]: ... +if sys.version_info >= (3, 10): + def hook_compressed( + filename: StrOrBytesPath, mode: str, encoding: Optional[str] = ..., errors: Optional[str] = ... + ) -> IO[Any]: ... + +else: + def hook_compressed(filename: StrOrBytesPath, mode: str) -> IO[Any]: ... + def hook_encoded(encoding: str, errors: Optional[str] = ...) -> Callable[[StrOrBytesPath, str], IO[Any]]: ... From 5e3df7f11c500cbfa871d30225fb9e535a87d7ac Mon Sep 17 00:00:00 2001 From: Pavel Karateev Date: Mon, 31 May 2021 19:20:45 +0300 Subject: [PATCH 2/3] Use PEP 604 instead of Optional --- stdlib/fileinput.pyi | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/stdlib/fileinput.pyi b/stdlib/fileinput.pyi index 0ce06e9a1114..5b9049c1cd3e 100644 --- a/stdlib/fileinput.pyi +++ b/stdlib/fileinput.pyi @@ -1,6 +1,6 @@ import sys from _typeshed import StrOrBytesPath -from typing import IO, Any, AnyStr, Callable, Generic, Iterable, Iterator, Optional, Union +from typing import IO, Any, AnyStr, Callable, Generic, Iterable, Iterator, Union if sys.version_info >= (3, 10): def input( @@ -10,8 +10,8 @@ if sys.version_info >= (3, 10): *, mode: str = ..., openhook: Callable[[StrOrBytesPath, str], IO[AnyStr]] = ..., - encoding: Optional[str] = ..., - errors: Optional[str] = ..., + encoding: str | None = ..., + errors: str | None = ..., ) -> FileInput[AnyStr]: ... elif sys.version_info >= (3, 8): @@ -53,8 +53,8 @@ class FileInput(Iterable[AnyStr], Generic[AnyStr]): *, mode: str = ..., openhook: Callable[[StrOrBytesPath, str], IO[AnyStr]] = ..., - encoding: Optional[str] = ..., - errors: Optional[str] = ..., + encoding: str | None = ..., + errors: str | None = ..., ) -> None: ... elif sys.version_info >= (3, 8): def __init__( @@ -93,11 +93,9 @@ class FileInput(Iterable[AnyStr], Generic[AnyStr]): def isstdin(self) -> bool: ... if sys.version_info >= (3, 10): - def hook_compressed( - filename: StrOrBytesPath, mode: str, encoding: Optional[str] = ..., errors: Optional[str] = ... - ) -> IO[Any]: ... + def hook_compressed(filename: StrOrBytesPath, mode: str, encoding: str | None = ..., errors: str | None = ...) -> IO[Any]: ... else: def hook_compressed(filename: StrOrBytesPath, mode: str) -> IO[Any]: ... -def hook_encoded(encoding: str, errors: Optional[str] = ...) -> Callable[[StrOrBytesPath, str], IO[Any]]: ... +def hook_encoded(encoding: str, errors: str | None = ...) -> Callable[[StrOrBytesPath, str], IO[Any]]: ... From c99d0840831a66e081ba244bc93e84135d8e0e61 Mon Sep 17 00:00:00 2001 From: Pavel Karateev Date: Mon, 31 May 2021 19:22:36 +0300 Subject: [PATCH 3/3] Mark keyword-only `hook_compressed()` params --- stdlib/fileinput.pyi | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/stdlib/fileinput.pyi b/stdlib/fileinput.pyi index 5b9049c1cd3e..0fd7937e1d22 100644 --- a/stdlib/fileinput.pyi +++ b/stdlib/fileinput.pyi @@ -93,7 +93,9 @@ class FileInput(Iterable[AnyStr], Generic[AnyStr]): def isstdin(self) -> bool: ... if sys.version_info >= (3, 10): - def hook_compressed(filename: StrOrBytesPath, mode: str, encoding: str | None = ..., errors: str | None = ...) -> IO[Any]: ... + def hook_compressed( + filename: StrOrBytesPath, mode: str, *, encoding: str | None = ..., errors: str | None = ... + ) -> IO[Any]: ... else: def hook_compressed(filename: StrOrBytesPath, mode: str) -> IO[Any]: ...