From 08b26b9e8eeae144cd55d2a7e04f44be7239fde9 Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Wed, 24 Feb 2021 23:46:03 +0100 Subject: [PATCH] Split and restore correct Python 2 version of cgi.pyi (#5064) * Split cgi.pyi into Py2 and 3 versions * Restore correct Python 2 version of cgi.pyi * Remove unnecessary version checks in cgi.pyi * Use collections.abc * Use List in Python 2 stub --- stdlib/@python2/cgi.pyi | 109 ++++++++++++++++++++++++++++++++++++++++ stdlib/VERSIONS | 2 +- stdlib/cgi.pyi | 100 ++++++++---------------------------- 3 files changed, 132 insertions(+), 79 deletions(-) create mode 100644 stdlib/@python2/cgi.pyi diff --git a/stdlib/@python2/cgi.pyi b/stdlib/@python2/cgi.pyi new file mode 100644 index 000000000..c3519bba9 --- /dev/null +++ b/stdlib/@python2/cgi.pyi @@ -0,0 +1,109 @@ +from _typeshed import SupportsGetItem, SupportsItemAccess +from builtins import type as _type +from typing import IO, Any, AnyStr, Iterable, Iterator, List, Mapping, Optional, Protocol, TypeVar, Union +from UserDict import UserDict + +_T = TypeVar("_T", bound=FieldStorage) + +def parse( + fp: Optional[IO[Any]] = ..., + environ: SupportsItemAccess[str, str] = ..., + keep_blank_values: bool = ..., + strict_parsing: bool = ..., +) -> dict[str, List[str]]: ... +def parse_qs(qs: str, keep_blank_values: bool = ..., strict_parsing: bool = ...) -> dict[str, List[str]]: ... +def parse_qsl(qs: str, keep_blank_values: bool = ..., strict_parsing: bool = ...) -> List[tuple[str, str]]: ... +def parse_multipart(fp: IO[Any], pdict: SupportsGetItem[str, bytes]) -> dict[str, List[bytes]]: ... + +class _Environ(Protocol): + def __getitem__(self, __k: str) -> str: ... + def keys(self) -> Iterable[str]: ... + +def parse_header(line: str) -> tuple[str, dict[str, str]]: ... +def test(environ: _Environ = ...) -> None: ... +def print_environ(environ: _Environ = ...) -> None: ... +def print_form(form: dict[str, Any]) -> None: ... +def print_directory() -> None: ... +def print_environ_usage() -> None: ... +def escape(s: AnyStr, quote: bool = ...) -> AnyStr: ... + +class MiniFieldStorage: + # The first five "Any" attributes here are always None, but mypy doesn't support that + filename: Any + list: Any + type: Any + file: Optional[IO[bytes]] + type_options: dict[Any, Any] + disposition: Any + disposition_options: dict[Any, Any] + headers: dict[Any, Any] + name: Any + value: Any + def __init__(self, name: Any, value: Any) -> None: ... + def __repr__(self) -> str: ... + +class FieldStorage(object): + FieldStorageClass: Optional[_type] + keep_blank_values: int + strict_parsing: int + qs_on_post: Optional[str] + headers: Mapping[str, str] + fp: IO[bytes] + encoding: str + errors: str + outerboundary: bytes + bytes_read: int + limit: Optional[int] + disposition: str + disposition_options: dict[str, str] + filename: Optional[str] + file: Optional[IO[bytes]] + type: str + type_options: dict[str, str] + innerboundary: bytes + length: int + done: int + list: Optional[List[Any]] + value: Union[None, bytes, List[Any]] + def __init__( + self, + fp: IO[Any] = ..., + headers: Mapping[str, str] = ..., + outerboundary: bytes = ..., + environ: SupportsGetItem[str, str] = ..., + keep_blank_values: int = ..., + strict_parsing: int = ..., + ) -> None: ... + def __repr__(self) -> str: ... + def __iter__(self) -> Iterator[str]: ... + def __getitem__(self, key: str) -> Any: ... + def getvalue(self, key: str, default: Any = ...) -> Any: ... + def getfirst(self, key: str, default: Any = ...) -> Any: ... + def getlist(self, key: str) -> List[Any]: ... + def keys(self) -> List[str]: ... + def has_key(self, key: str) -> bool: ... + def __contains__(self, key: str) -> bool: ... + def __len__(self) -> int: ... + def __nonzero__(self) -> bool: ... + # In Python 2 it always returns bytes and ignores the "binary" flag + def make_file(self, binary: Any = ...) -> IO[bytes]: ... + +class FormContentDict(UserDict[str, List[str]]): + query_string: str + def __init__(self, environ: Mapping[str, str] = ..., keep_blank_values: int = ..., strict_parsing: int = ...) -> None: ... + +class SvFormContentDict(FormContentDict): + def getlist(self, key: Any) -> Any: ... + +class InterpFormContentDict(SvFormContentDict): ... + +class FormContent(FormContentDict): + # TODO this should have + # def values(self, key: Any) -> Any: ... + # but this is incompatible with the supertype, and adding '# type: ignore' triggers + # a parse error in pytype (https://github.com/google/pytype/issues/53) + def indexed_value(self, key: Any, location: int) -> Any: ... + def value(self, key: Any) -> Any: ... + def length(self, key: Any) -> int: ... + def stripped(self, key: Any) -> Any: ... + def pars(self) -> dict[Any, Any]: ... diff --git a/stdlib/VERSIONS b/stdlib/VERSIONS index b003f85fa..49badfa47 100644 --- a/stdlib/VERSIONS +++ b/stdlib/VERSIONS @@ -54,7 +54,7 @@ builtins: 3.6 bz2: 2.7 cProfile: 2.7 calendar: 2.7 -cgi: 2.7 +cgi: 3.6 cgitb: 2.7 chunk: 2.7 cmath: 2.7 diff --git a/stdlib/cgi.pyi b/stdlib/cgi.pyi index 190fc8fec..da3a4d43c 100644 --- a/stdlib/cgi.pyi +++ b/stdlib/cgi.pyi @@ -1,7 +1,8 @@ import sys from _typeshed import SupportsGetItem, SupportsItemAccess from builtins import type as _type -from typing import IO, Any, AnyStr, Iterable, Iterator, Mapping, Optional, Protocol, TypeVar, Union +from collections.abc import Iterable, Iterator, Mapping +from typing import IO, Any, Optional, Protocol, TypeVar, Union _T = TypeVar("_T", bound=FieldStorage) @@ -36,10 +37,7 @@ def print_form(form: dict[str, Any]) -> None: ... def print_directory() -> None: ... def print_environ_usage() -> None: ... -if sys.version_info < (3,): - def escape(s: AnyStr, quote: bool = ...) -> AnyStr: ... - -elif sys.version_info < (3, 8): +if sys.version_info < (3, 8): def escape(s: str, quote: Optional[bool] = ...) -> str: ... class MiniFieldStorage: @@ -82,48 +80,22 @@ class FieldStorage(object): done: int list: Optional[_list[Any]] value: Union[None, bytes, _list[Any]] - - if sys.version_info >= (3, 6): - def __init__( - self, - fp: Optional[IO[Any]] = ..., - headers: Optional[Mapping[str, str]] = ..., - outerboundary: bytes = ..., - environ: SupportsGetItem[str, str] = ..., - keep_blank_values: int = ..., - strict_parsing: int = ..., - limit: Optional[int] = ..., - encoding: str = ..., - errors: str = ..., - max_num_fields: Optional[int] = ..., - separator: str = ..., - ) -> None: ... - elif sys.version_info >= (3, 0): - def __init__( - self, - fp: Optional[IO[Any]] = ..., - headers: Optional[Mapping[str, str]] = ..., - outerboundary: bytes = ..., - environ: SupportsGetItem[str, str] = ..., - keep_blank_values: int = ..., - strict_parsing: int = ..., - limit: Optional[int] = ..., - encoding: str = ..., - errors: str = ..., - ) -> None: ... - else: - def __init__( - self, - fp: IO[Any] = ..., - headers: Mapping[str, str] = ..., - outerboundary: bytes = ..., - environ: SupportsGetItem[str, str] = ..., - keep_blank_values: int = ..., - strict_parsing: int = ..., - ) -> None: ... - if sys.version_info >= (3, 0): - def __enter__(self: _T) -> _T: ... - def __exit__(self, *args: Any) -> None: ... + def __init__( + self, + fp: Optional[IO[Any]] = ..., + headers: Optional[Mapping[str, str]] = ..., + outerboundary: bytes = ..., + environ: SupportsGetItem[str, str] = ..., + keep_blank_values: int = ..., + strict_parsing: int = ..., + limit: Optional[int] = ..., + encoding: str = ..., + errors: str = ..., + max_num_fields: Optional[int] = ..., + separator: str = ..., + ) -> None: ... + def __enter__(self: _T) -> _T: ... + def __exit__(self, *args: Any) -> None: ... def __repr__(self) -> str: ... def __iter__(self) -> Iterator[str]: ... def __getitem__(self, key: str) -> Any: ... @@ -131,36 +103,8 @@ class FieldStorage(object): def getfirst(self, key: str, default: Any = ...) -> Any: ... def getlist(self, key: str) -> _list[Any]: ... def keys(self) -> _list[str]: ... - if sys.version_info < (3, 0): - def has_key(self, key: str) -> bool: ... def __contains__(self, key: str) -> bool: ... def __len__(self) -> int: ... - if sys.version_info >= (3, 0): - def __bool__(self) -> bool: ... - else: - def __nonzero__(self) -> bool: ... - if sys.version_info >= (3, 0): - # In Python 3 it returns bytes or str IO depending on an internal flag - def make_file(self) -> IO[Any]: ... - else: - # In Python 2 it always returns bytes and ignores the "binary" flag - def make_file(self, binary: Any = ...) -> IO[bytes]: ... - -if sys.version_info < (3, 0): - from UserDict import UserDict - class FormContentDict(UserDict[str, list[str]]): - query_string: str - def __init__(self, environ: Mapping[str, str] = ..., keep_blank_values: int = ..., strict_parsing: int = ...) -> None: ... - class SvFormContentDict(FormContentDict): - def getlist(self, key: Any) -> Any: ... - class InterpFormContentDict(SvFormContentDict): ... - class FormContent(FormContentDict): - # TODO this should have - # def values(self, key: Any) -> Any: ... - # but this is incompatible with the supertype, and adding '# type: ignore' triggers - # a parse error in pytype (https://github.com/google/pytype/issues/53) - def indexed_value(self, key: Any, location: int) -> Any: ... - def value(self, key: Any) -> Any: ... - def length(self, key: Any) -> int: ... - def stripped(self, key: Any) -> Any: ... - def pars(self) -> dict[Any, Any]: ... + def __bool__(self) -> bool: ... + # In Python 3 it returns bytes or str IO depending on an internal flag + def make_file(self) -> IO[Any]: ...