Eliminated the use of "bare" TypeVars in stdlib stubs (#5041)

Eliminated the use of "bare" TypeVars (i.e. a TypeVar that appears only once) within generic methods. While not considered an error in PEP 484, these are a common source of bugs in code, and some type checkers (including pytype and pyright) flag them as errors.

Co-authored-by: Eric Traut <erictr@microsoft.com>
This commit is contained in:
Eric Traut
2021-02-27 20:43:45 -07:00
committed by GitHub
parent 3c0f2acdf0
commit e2967a8bee
16 changed files with 34 additions and 37 deletions

View File

@@ -1,13 +1,13 @@
from _typeshed import AnyPath, OpenBinaryMode, OpenBinaryModeReading, OpenBinaryModeUpdating, OpenBinaryModeWriting, OpenTextMode
from asyncio import AbstractEventLoop
from typing import Any, Callable, Optional, TypeVar, Union, overload
from typing import Any, Callable, Optional, Union, overload
from typing_extensions import Literal
from ..base import AiofilesContextManager
from .binary import AsyncBufferedIOBase, AsyncBufferedReader, AsyncFileIO, _UnknownAsyncBinaryIO
from .text import AsyncTextIOWrapper
_OpenFile = TypeVar("_OpenFile", bound=Union[AnyPath, int])
_OpenFile = Union[AnyPath, int]
_Opener = Callable[[str, int], int]
# Text mode: always returns AsyncTextIOWrapper

View File

@@ -1,16 +1,13 @@
from typing import Any, Callable, ContextManager, MutableMapping, Optional, TypeVar
_KT = TypeVar("_KT")
_VT = TypeVar("_VT")
_T = TypeVar("_T", bound=Callable[..., Any])
_T_co = TypeVar("_T_co", covariant=True)
_T_self = TypeVar("_T_self")
def cached(
cache: Optional[MutableMapping[_KT, _VT]], key: Callable[..., _KT] = ..., lock: Optional[ContextManager[_T_co]] = ...
cache: Optional[MutableMapping[_KT, Any]], key: Callable[..., _KT] = ..., lock: Optional[ContextManager[Any]] = ...
) -> Callable[[_T], _T]: ...
def cachedmethod(
cache: Callable[[_T_self], Optional[MutableMapping[_KT, _VT]]],
cache: Callable[[Any], Optional[MutableMapping[_KT, Any]]],
key: Callable[..., _KT] = ...,
lock: Optional[ContextManager[_T_co]] = ...,
lock: Optional[ContextManager[Any]] = ...,
) -> Callable[[_T], _T]: ...

View File

@@ -51,7 +51,7 @@ def progressbar(
show_eta: bool = ...,
show_percent: Optional[bool] = ...,
show_pos: bool = ...,
item_show_func: Optional[Callable[[_T], str]] = ...,
item_show_func: Optional[Callable[[Any], str]] = ...,
fill_char: str = ...,
empty_char: str = ...,
bar_template: str = ...,

View File

@@ -13,7 +13,7 @@ _S = TypeVar("_S", bound=Type[Enum])
class EnumMeta(ABCMeta):
def __iter__(self: Type[_T]) -> Iterator[_T]: ...
def __reversed__(self: Type[_T]) -> Iterator[_T]: ...
def __contains__(self: Type[_T], member: object) -> bool: ...
def __contains__(self, member: object) -> bool: ...
def __getitem__(self: Type[_T], name: str) -> _T: ...
@property
def __members__(self: Type[_T]) -> Mapping[str, _T]: ...

View File

@@ -12,7 +12,7 @@ class _TypedDict(Mapping[str, object], metaclass=abc.ABCMeta):
# can go through.
def setdefault(self, k: NoReturn, default: object) -> object: ...
# Mypy plugin hook for 'pop' expects that 'default' has a type variable type.
def pop(self, k: NoReturn, default: _T = ...) -> object: ...
def pop(self, k: NoReturn, default: _T = ...) -> object: ... # type: ignore
def update(self: _T, __m: _T) -> None: ...
if sys.version_info < (3, 0):
def has_key(self, k: str) -> bool: ...
@@ -25,7 +25,7 @@ class _TypedDict(Mapping[str, object], metaclass=abc.ABCMeta):
def values(self) -> ValuesView[object]: ...
def __delitem__(self, k: NoReturn) -> None: ...
def TypedDict(typename: str, fields: Dict[str, Type[_T]], total: bool = ...) -> Type[Dict[str, Any]]: ...
def TypedDict(typename: str, fields: Dict[str, Type[Any]], total: bool = ...) -> Type[Dict[str, Any]]: ...
def Arg(type: _T = ..., name: Optional[str] = ...) -> _T: ...
def DefaultArg(type: _T = ..., name: Optional[str] = ...) -> _T: ...
def NamedArg(type: _T = ..., name: Optional[str] = ...) -> _T: ...

View File

@@ -61,14 +61,14 @@ def get_function_closure(fun: types.FunctionType) -> Optional[Tuple[types._Cell,
def get_function_code(fun: types.FunctionType) -> types.CodeType: ...
def get_function_defaults(fun: types.FunctionType) -> Optional[Tuple[Any, ...]]: ...
def get_function_globals(fun: types.FunctionType) -> Dict[str, Any]: ...
def iterkeys(d: Mapping[_K, _V]) -> typing.Iterator[_K]: ...
def itervalues(d: Mapping[_K, _V]) -> typing.Iterator[_V]: ...
def iterkeys(d: Mapping[_K, Any]) -> typing.Iterator[_K]: ...
def itervalues(d: Mapping[Any, _V]) -> typing.Iterator[_V]: ...
def iteritems(d: Mapping[_K, _V]) -> typing.Iterator[Tuple[_K, _V]]: ...
# def iterlists
def viewkeys(d: Mapping[_K, _V]) -> KeysView[_K]: ...
def viewvalues(d: Mapping[_K, _V]) -> ValuesView[_V]: ...
def viewkeys(d: Mapping[_K, Any]) -> KeysView[_K]: ...
def viewvalues(d: Mapping[Any, _V]) -> ValuesView[_V]: ...
def viewitems(d: Mapping[_K, _V]) -> ItemsView[_K, _V]: ...
def b(s: str) -> binary_type: ...
def u(s: str) -> text_type: ...

View File

@@ -51,7 +51,7 @@ class _TypedDict(Mapping[str, object], metaclass=abc.ABCMeta):
# can go through.
def setdefault(self, k: NoReturn, default: object) -> object: ...
# Mypy plugin hook for 'pop' expects that 'default' has a type variable type.
def pop(self, k: NoReturn, default: _T = ...) -> object: ...
def pop(self, k: NoReturn, default: _T = ...) -> object: ... # type: ignore
def update(self: _T, __m: _T) -> None: ...
if sys.version_info < (3, 0):
def has_key(self, k: str) -> bool: ...