Skip to content

Commit e2967a8

Browse files
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>
1 parent 3c0f2ac commit e2967a8

File tree

16 files changed

+34
-37
lines changed

16 files changed

+34
-37
lines changed

stdlib/_heapq.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ from typing import Any, Callable, Iterable, List, Optional, TypeVar
33

44
_T = TypeVar("_T")
55

6-
def heapify(__heap: List[_T]) -> None: ...
6+
def heapify(__heap: List[Any]) -> None: ...
77
def heappop(__heap: List[_T]) -> _T: ...
88
def heappush(__heap: List[_T], __item: _T) -> None: ...
99
def heappushpop(__heap: List[_T], __item: _T) -> _T: ...

stdlib/_weakrefset.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class WeakSet(MutableSet[_T], Generic[_T]):
2323
def __ior__(self, other: Iterable[_S]) -> WeakSet[Union[_S, _T]]: ...
2424
def difference(self: _SelfT, other: Iterable[_T]) -> _SelfT: ...
2525
def __sub__(self: _SelfT, other: Iterable[_T]) -> _SelfT: ...
26-
def difference_update(self: _SelfT, other: Iterable[_T]) -> None: ...
26+
def difference_update(self, other: Iterable[_T]) -> None: ...
2727
def __isub__(self: _SelfT, other: Iterable[_T]) -> _SelfT: ...
2828
def intersection(self: _SelfT, other: Iterable[_T]) -> _SelfT: ...
2929
def __and__(self: _SelfT, other: Iterable[_T]) -> _SelfT: ...
@@ -38,7 +38,7 @@ class WeakSet(MutableSet[_T], Generic[_T]):
3838
def __eq__(self, other: object) -> bool: ...
3939
def symmetric_difference(self, other: Iterable[_S]) -> WeakSet[Union[_S, _T]]: ...
4040
def __xor__(self, other: Iterable[_S]) -> WeakSet[Union[_S, _T]]: ...
41-
def symmetric_difference_update(self, other: Iterable[_S]) -> None: ...
41+
def symmetric_difference_update(self, other: Iterable[Any]) -> None: ...
4242
def __ixor__(self, other: Iterable[_S]) -> WeakSet[Union[_S, _T]]: ...
4343
def union(self, other: Iterable[_S]) -> WeakSet[Union[_S, _T]]: ...
4444
def __or__(self, other: Iterable[_S]) -> WeakSet[Union[_S, _T]]: ...

stdlib/builtins.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1200,9 +1200,9 @@ def reversed(__sequence: Sequence[_T]) -> Iterator[_T]: ...
12001200
def reversed(__sequence: Reversible[_T]) -> Iterator[_T]: ...
12011201
def repr(__obj: object) -> str: ...
12021202
@overload
1203-
def round(number: SupportsRound[_T]) -> int: ...
1203+
def round(number: SupportsRound[Any]) -> int: ...
12041204
@overload
1205-
def round(number: SupportsRound[_T], ndigits: None) -> int: ...
1205+
def round(number: SupportsRound[Any], ndigits: None) -> int: ...
12061206
@overload
12071207
def round(number: SupportsRound[_T], ndigits: int) -> _T: ...
12081208
def setattr(__obj: Any, __name: str, __value: Any) -> None: ...

stdlib/enum.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ _S = TypeVar("_S", bound=Type[Enum])
1313
class EnumMeta(ABCMeta):
1414
def __iter__(self: Type[_T]) -> Iterator[_T]: ...
1515
def __reversed__(self: Type[_T]) -> Iterator[_T]: ...
16-
def __contains__(self: Type[_T], member: object) -> bool: ...
16+
def __contains__(self: Type[Any], member: object) -> bool: ...
1717
def __getitem__(self: Type[_T], name: str) -> _T: ...
1818
@property
1919
def __members__(self: Type[_T]) -> Mapping[str, _T]: ...

stdlib/functools.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ if sys.version_info >= (3, 8):
123123
@overload
124124
def __get__(self, instance: None, owner: Optional[Type[Any]] = ...) -> cached_property[_T]: ...
125125
@overload
126-
def __get__(self, instance: _S, owner: Optional[Type[Any]] = ...) -> _T: ...
126+
def __get__(self, instance: object, owner: Optional[Type[Any]] = ...) -> _T: ...
127127
def __set_name__(self, owner: Type[Any], name: str) -> None: ...
128128
if sys.version_info >= (3, 9):
129129
def __class_getitem__(cls, item: Any) -> GenericAlias: ...

stdlib/gettext.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def translation(
5252
domain: str,
5353
localedir: Optional[StrPath] = ...,
5454
languages: Optional[Iterable[str]] = ...,
55-
class_: Type[_T] = ...,
55+
class_: Type[Any] = ...,
5656
fallback: Literal[True] = ...,
5757
codeset: Optional[str] = ...,
5858
) -> Any: ...

stdlib/heapq.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ _T = TypeVar("_T")
66
def heappush(__heap: List[_T], __item: _T) -> None: ...
77
def heappop(__heap: List[_T]) -> _T: ...
88
def heappushpop(__heap: List[_T], __item: _T) -> _T: ...
9-
def heapify(__heap: List[_T]) -> None: ...
9+
def heapify(__heap: List[Any]) -> None: ...
1010
def heapreplace(__heap: List[_T], __item: _T) -> _T: ...
1111
def merge(*iterables: Iterable[_T], key: Optional[Callable[[_T], Any]] = ..., reverse: bool = ...) -> Iterable[_T]: ...
1212
def nlargest(n: int, iterable: Iterable[_T], key: Optional[Callable[[_T], SupportsLessThan]] = ...) -> List[_T]: ...
1313
def nsmallest(n: int, iterable: Iterable[_T], key: Optional[Callable[[_T], SupportsLessThan]] = ...) -> List[_T]: ...
14-
def _heapify_max(__x: List[_T]) -> None: ... # undocumented
14+
def _heapify_max(__x: List[Any]) -> None: ... # undocumented

stdlib/operator.pyi

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,17 +86,17 @@ def contains(__a: Container[Any], __b: Any) -> bool: ...
8686
def __contains__(a: Container[Any], b: Any) -> bool: ...
8787
def countOf(__a: Container[Any], __b: Any) -> int: ...
8888
@overload
89-
def delitem(__a: MutableSequence[_T], __b: int) -> None: ...
89+
def delitem(__a: MutableSequence[Any], __b: int) -> None: ...
9090
@overload
91-
def delitem(__a: MutableSequence[_T], __b: slice) -> None: ...
91+
def delitem(__a: MutableSequence[Any], __b: slice) -> None: ...
9292
@overload
93-
def delitem(__a: MutableMapping[_K, _V], __b: _K) -> None: ...
93+
def delitem(__a: MutableMapping[_K, Any], __b: _K) -> None: ...
9494
@overload
95-
def __delitem__(a: MutableSequence[_T], b: int) -> None: ...
95+
def __delitem__(a: MutableSequence[Any], b: int) -> None: ...
9696
@overload
97-
def __delitem__(a: MutableSequence[_T], b: slice) -> None: ...
97+
def __delitem__(a: MutableSequence[Any], b: slice) -> None: ...
9898
@overload
99-
def __delitem__(a: MutableMapping[_K, _V], b: _K) -> None: ...
99+
def __delitem__(a: MutableMapping[_K, Any], b: _K) -> None: ...
100100

101101
if sys.version_info < (3,):
102102
def delslice(a: MutableSequence[Any], b: int, c: int) -> None: ...

stdlib/typing.pyi

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ class MutableSet(AbstractSet[_T], Generic[_T]):
362362
def __isub__(self, s: AbstractSet[Any]) -> MutableSet[_T]: ...
363363

364364
class MappingView(Sized):
365-
def __init__(self, mapping: Mapping[_KT_co, _VT_co]) -> None: ... # undocumented
365+
def __init__(self, mapping: Mapping[Any, Any]) -> None: ... # undocumented
366366
def __len__(self) -> int: ...
367367

368368
class ItemsView(MappingView, AbstractSet[Tuple[_KT_co, _VT_co]], Generic[_KT_co, _VT_co]):
@@ -381,7 +381,7 @@ class ItemsView(MappingView, AbstractSet[Tuple[_KT_co, _VT_co]], Generic[_KT_co,
381381
def __rxor__(self, o: Iterable[_T]) -> Set[Union[Tuple[_KT_co, _VT_co], _T]]: ...
382382

383383
class KeysView(MappingView, AbstractSet[_KT_co], Generic[_KT_co]):
384-
def __init__(self, mapping: Mapping[_KT_co, _VT_co]) -> None: ... # undocumented
384+
def __init__(self, mapping: Mapping[_KT_co, Any]) -> None: ... # undocumented
385385
def __and__(self, o: Iterable[Any]) -> Set[_KT_co]: ...
386386
def __rand__(self, o: Iterable[_T]) -> Set[_T]: ...
387387
def __contains__(self, o: object) -> bool: ...
@@ -396,7 +396,7 @@ class KeysView(MappingView, AbstractSet[_KT_co], Generic[_KT_co]):
396396
def __rxor__(self, o: Iterable[_T]) -> Set[Union[_KT_co, _T]]: ...
397397

398398
class ValuesView(MappingView, Iterable[_VT_co], Generic[_VT_co]):
399-
def __init__(self, mapping: Mapping[_KT_co, _VT_co]) -> None: ... # undocumented
399+
def __init__(self, mapping: Mapping[Any, _VT_co]) -> None: ... # undocumented
400400
def __contains__(self, o: object) -> bool: ...
401401
def __iter__(self) -> Iterator[_VT_co]: ...
402402
if sys.version_info >= (3, 8):
@@ -661,7 +661,7 @@ class _TypedDict(Mapping[str, object], metaclass=ABCMeta):
661661
# can go through.
662662
def setdefault(self, k: NoReturn, default: object) -> object: ...
663663
# Mypy plugin hook for 'pop' expects that 'default' has a type variable type.
664-
def pop(self, k: NoReturn, default: _T = ...) -> object: ...
664+
def pop(self, k: NoReturn, default: _T = ...) -> object: ... # type: ignore
665665
def update(self: _T, __m: _T) -> None: ...
666666
def __delitem__(self, k: NoReturn) -> None: ...
667667
def items(self) -> ItemsView[str, object]: ...

stubs/aiofiles/aiofiles/threadpool/__init__.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
from _typeshed import AnyPath, OpenBinaryMode, OpenBinaryModeReading, OpenBinaryModeUpdating, OpenBinaryModeWriting, OpenTextMode
22
from asyncio import AbstractEventLoop
3-
from typing import Any, Callable, Optional, TypeVar, Union, overload
3+
from typing import Any, Callable, Optional, Union, overload
44
from typing_extensions import Literal
55

66
from ..base import AiofilesContextManager
77
from .binary import AsyncBufferedIOBase, AsyncBufferedReader, AsyncFileIO, _UnknownAsyncBinaryIO
88
from .text import AsyncTextIOWrapper
99

10-
_OpenFile = TypeVar("_OpenFile", bound=Union[AnyPath, int])
10+
_OpenFile = Union[AnyPath, int]
1111
_Opener = Callable[[str, int], int]
1212

1313
# Text mode: always returns AsyncTextIOWrapper

0 commit comments

Comments
 (0)