From 1d6e3f492ea5f5e6f52a4c95bc167fd0d2aff086 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Thu, 27 Apr 2017 08:47:59 -0700 Subject: [PATCH] Fix incorrect usage of AnyStr (#1215) * Fix incorrect usage of AnyStr - sqlite3 was using Union[bytes, AnyStr], which doesn't make sense - The urllib functions I changed accept either bytes or str for their "safe" argument - Also added supports for PathLike to pstats - Remove some unused imports of AnyStr * pstats: python 2 accepts unicode --- stdlib/2/subprocess.pyi | 2 +- stdlib/2and3/pstats.pyi | 10 ++++++++-- stdlib/3/sqlite3/dbapi2.pyi | 10 +++++----- stdlib/3/string.pyi | 2 +- stdlib/3/subprocess.pyi | 2 +- stdlib/3/urllib/parse.pyi | 14 ++++++++------ 6 files changed, 24 insertions(+), 16 deletions(-) diff --git a/stdlib/2/subprocess.pyi b/stdlib/2/subprocess.pyi index 448df12f1..06934c65d 100644 --- a/stdlib/2/subprocess.pyi +++ b/stdlib/2/subprocess.pyi @@ -2,7 +2,7 @@ # Based on http://docs.python.org/2/library/subprocess.html and Python 3 stub -from typing import Sequence, Any, AnyStr, Mapping, Callable, Tuple, IO, Union, Optional, List, Text +from typing import Sequence, Any, Mapping, Callable, Tuple, IO, Union, Optional, List, Text _FILE = Union[int, IO[Any]] _TXT = Union[bytes, Text] diff --git a/stdlib/2and3/pstats.pyi b/stdlib/2and3/pstats.pyi index 2d70f8859..232139188 100644 --- a/stdlib/2and3/pstats.pyi +++ b/stdlib/2and3/pstats.pyi @@ -1,9 +1,15 @@ from profile import Profile from cProfile import Profile as cProfile -from typing import Any, AnyStr, Dict, IO, Iterable, List, Text, Tuple, TypeVar, Union, overload +import os +import sys +from typing import Any, Dict, IO, Iterable, List, Text, Tuple, TypeVar, Union, overload _Selector = Union[str, float, int] _T = TypeVar('_T', bound='Stats') +if sys.version_info >= (3, 6): + _Path = Union[bytes, Text, os.PathLike[Any]] +else: + _Path = Union[bytes, Text] class Stats: def __init__(self: _T, __arg: Union[None, str, Text, Profile, cProfile] = ..., @@ -13,7 +19,7 @@ class Stats: def load_stats(self, arg: Union[None, str, Text, Profile, cProfile]) -> None: ... def get_top_level_stats(self) -> None: ... def add(self: _T, *arg_list: Union[None, str, Text, Profile, cProfile, _T]) -> _T: ... - def dump_stats(self, filename: AnyStr) -> None: ... + def dump_stats(self, filename: _Path) -> None: ... def get_sort_arg_defs(self) -> Dict[str, Tuple[Tuple[Tuple[int, int], ...], str]]: ... @overload def sort_stats(self: _T, field: int) -> _T: ... diff --git a/stdlib/3/sqlite3/dbapi2.pyi b/stdlib/3/sqlite3/dbapi2.pyi index f9858ab76..2c47843e5 100644 --- a/stdlib/3/sqlite3/dbapi2.pyi +++ b/stdlib/3/sqlite3/dbapi2.pyi @@ -1,7 +1,7 @@ # Filip Hron # based heavily on Andrey Vlasovskikh's python-skeletons https://github.com/JetBrains/python-skeletons/blob/master/sqlite3.py -from typing import Any, Union, List, AnyStr, Iterator, Optional +from typing import Any, Union, List, Iterator, Optional from numbers import Integral from datetime import time, datetime from collections import Iterable @@ -69,9 +69,9 @@ version = ... # type: str def adapt(obj, protocol, alternate): ... def complete_statement(sql: str) -> bool: ... if sys.version_info >= (3, 4): - def connect(database: Union[bytes, AnyStr], timeout: float = ..., detect_types: int = ..., isolation_level: Union[str, None] = ..., check_same_thread: bool = ..., factory: Union[Connection, None] = ..., cached_statements: int = ..., uri: bool = ...) -> Connection: ... + def connect(database: Union[bytes, str], timeout: float = ..., detect_types: int = ..., isolation_level: Union[str, None] = ..., check_same_thread: bool = ..., factory: Union[Connection, None] = ..., cached_statements: int = ..., uri: bool = ...) -> Connection: ... else: - def connect(database: Union[bytes, AnyStr], timeout: float = ..., detect_types: int = ..., isolation_level: Union[str, None] = ..., check_same_thread: bool = ..., factory: Union[Connection, None] = ..., cached_statements: int = ...) -> Connection: ... + def connect(database: Union[bytes, str], timeout: float = ..., detect_types: int = ..., isolation_level: Union[str, None] = ..., check_same_thread: bool = ..., factory: Union[Connection, None] = ..., cached_statements: int = ...) -> Connection: ... def enable_callback_tracebacks(flag: bool) -> None: ... def enable_shared_cache(do_enable: int) -> None: ... def register_adapter(type: type, callable: Any) -> None: ... @@ -109,7 +109,7 @@ class Connection: def execute(self, sql: str, parameters: Iterable = ...) -> Cursor: ... # TODO: please check in executemany() if seq_of_parameters type is possible like this def executemany(self, sql: str, seq_of_parameters: Iterable[Iterable]) -> Cursor: ... - def executescript(self, sql_script: Union[bytes, AnyStr]) -> Cursor: ... + def executescript(self, sql_script: Union[bytes, str]) -> Cursor: ... def interrupt(self, *args, **kwargs) -> None: ... def iterdump(self, *args, **kwargs) -> None: ... def rollback(self, *args, **kwargs) -> None: ... @@ -138,7 +138,7 @@ class Cursor(Iterator[Any]): def close(self, *args, **kwargs): ... def execute(self, sql: str, parameters: Iterable = ...) -> Cursor: ... def executemany(self, sql: str, seq_of_parameters: Iterable[Iterable]): ... - def executescript(self, sql_script: Union[bytes, AnyStr]) -> Cursor: ... + def executescript(self, sql_script: Union[bytes, str]) -> Cursor: ... def fetchall(self) -> List[Any]: ... def fetchmany(self, size: Integral = ...) -> List[Any]: ... def fetchone(self) -> Any: ... diff --git a/stdlib/3/string.pyi b/stdlib/3/string.pyi index cff033623..c07c186d1 100644 --- a/stdlib/3/string.pyi +++ b/stdlib/3/string.pyi @@ -2,7 +2,7 @@ # Based on http://docs.python.org/3.2/library/string.html -from typing import Mapping, Sequence, Any, Optional, Union, List, Tuple, Iterable, AnyStr +from typing import Mapping, Sequence, Any, Optional, Union, List, Tuple, Iterable ascii_letters = ... # type: str ascii_lowercase = ... # type: str diff --git a/stdlib/3/subprocess.pyi b/stdlib/3/subprocess.pyi index a64162105..36e5c5c9e 100644 --- a/stdlib/3/subprocess.pyi +++ b/stdlib/3/subprocess.pyi @@ -2,7 +2,7 @@ # Based on http://docs.python.org/3.6/library/subprocess.html import sys -from typing import Sequence, Any, AnyStr, Mapping, Callable, Tuple, IO, Optional, Union, List, Type, Text +from typing import Sequence, Any, Mapping, Callable, Tuple, IO, Optional, Union, List, Type, Text from types import TracebackType _FILE = Union[int, IO[Any]] diff --git a/stdlib/3/urllib/parse.pyi b/stdlib/3/urllib/parse.pyi index 05565e652..637fa5fa6 100644 --- a/stdlib/3/urllib/parse.pyi +++ b/stdlib/3/urllib/parse.pyi @@ -2,6 +2,8 @@ from typing import Any, List, Dict, Tuple, AnyStr, Generic, overload, Sequence, Mapping, Union, NamedTuple, Callable import sys +_Str = Union[bytes, str] + __all__ = ( 'urlparse', 'urlunparse', @@ -103,20 +105,20 @@ def parse_qsl(qs: AnyStr, keep_blank_values: bool = ..., strict_parsing: bool = @overload -def quote(string: str, safe: AnyStr = ..., encoding: str = ..., errors: str = ...) -> str: ... +def quote(string: str, safe: _Str = ..., encoding: str = ..., errors: str = ...) -> str: ... @overload -def quote(string: bytes, safe: AnyStr = ...) -> str: ... +def quote(string: bytes, safe: _Str = ...) -> str: ... -def quote_from_bytes(bs: bytes, safe: AnyStr = ...) -> str: ... +def quote_from_bytes(bs: bytes, safe: _Str = ...) -> str: ... @overload -def quote_plus(string: str, safe: AnyStr = ..., encoding: str = ..., errors: str = ...) -> str: ... +def quote_plus(string: str, safe: _Str = ..., encoding: str = ..., errors: str = ...) -> str: ... @overload -def quote_plus(string: bytes, safe: AnyStr = ...) -> str: ... +def quote_plus(string: bytes, safe: _Str = ...) -> str: ... def unquote(string: str, encoding: str = ..., errors: str = ...) -> str: ... -def unquote_to_bytes(string: AnyStr) -> bytes: ... +def unquote_to_bytes(string: _Str) -> bytes: ... def unquote_plus(string: str, encoding: str = ..., errors: str = ...) -> str: ...