Skip to content

Commit 1dec643

Browse files
authored
commit 3
1 parent e984453 commit 1dec643

1 file changed

Lines changed: 60 additions & 54 deletions

File tree

stdlib/collections/__init__.pyi

Lines changed: 60 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,16 @@ if sys.version_info >= (3, 15):
2222

2323
__all__ = ["ChainMap", "Counter", "OrderedDict", "UserDict", "UserList", "UserString", "defaultdict", "deque", "namedtuple"]
2424

25-
_S = TypeVar("_S")
2625
_T = TypeVar("_T")
2726
_T1 = TypeVar("_T1")
2827
_T2 = TypeVar("_T2")
29-
_KT = TypeVar("_KT")
28+
_KT = TypeVar("_KT", bound=Hashable)
3029
_VT = TypeVar("_VT")
31-
_KT_co = TypeVar("_KT_co", covariant=True)
30+
_KT2 = TypeVar("_KT2")
31+
_KT_co = TypeVar("_KT_co", bound=Hashable, covariant=True)
3232
_VT_co = TypeVar("_VT_co", covariant=True)
3333

34-
# namedtuple is special-cased in the type checker; the initializer is ignored.
34+
# namedtuple is special-cased in type checkers; the initializer is ignored.
3535
def namedtuple(
3636
typename: str,
3737
field_names: str | Iterable[str],
@@ -86,18 +86,18 @@ class UserDict(MutableMapping[_KT, _VT]):
8686
# See #3800 & https://github.com/python/typing/issues/548#issuecomment-683336963.
8787
@classmethod
8888
@overload
89-
def fromkeys(cls, iterable: Iterable[_T], value: None = None) -> UserDict[_T, Any | None]: ...
89+
def fromkeys(cls, iterable: Iterable[_KT], value: None = None) -> UserDict[_KT, Any | None]: ...
9090
@classmethod
9191
@overload
92-
def fromkeys(cls, iterable: Iterable[_T], value: _S) -> UserDict[_T, _S]: ...
92+
def fromkeys(cls, iterable: Iterable[_KT], value: _T) -> UserDict[_KT, _T]: ...
9393
@overload
9494
def __or__(self, other: UserDict[_KT, _VT] | dict[_KT, _VT]) -> Self: ...
9595
@overload
96-
def __or__(self, other: UserDict[_T1, _T2] | dict[_T1, _T2]) -> UserDict[_KT | _T1, _VT | _T2]: ...
96+
def __or__(self, other: UserDict[_KT2, _T] | dict[_KT2, _T]) -> UserDict[_KT | _KT2, _VT | _T]: ...
9797
@overload
9898
def __ror__(self, other: UserDict[_KT, _VT] | dict[_KT, _VT]) -> Self: ...
9999
@overload
100-
def __ror__(self, other: UserDict[_T1, _T2] | dict[_T1, _T2]) -> UserDict[_KT | _T1, _VT | _T2]: ...
100+
def __ror__(self, other: UserDict[_KT2, _T] | dict[_KT2, _T]) -> UserDict[_KT | _KT2, _VT | _T]: ...
101101
# UserDict.__ior__ should be kept roughly in line with MutableMapping.update()
102102
@overload # type: ignore[misc]
103103
def __ior__(self, other: SupportsKeysAndGetItem[_KT, _VT]) -> Self: ...
@@ -270,63 +270,69 @@ class deque(MutableSequence[_T]):
270270
def __eq__(self, value: object, /) -> bool: ...
271271
def __class_getitem__(cls, item: Any, /) -> GenericAlias: ...
272272

273-
class Counter(dict[_T, int], Generic[_T]):
273+
class Counter(dict[_KT, int], Generic[_KT]):
274274
@overload
275275
def __init__(self, iterable: None = None, /) -> None: ...
276276
@overload
277277
def __init__(self: Counter[str], iterable: None = None, /, **kwargs: int) -> None: ...
278278
@overload
279-
def __init__(self, mapping: SupportsKeysAndGetItem[_T, int], /) -> None: ...
279+
def __init__(self, mapping: SupportsKeysAndGetItem[_KT, int], /) -> None: ...
280280
@overload
281-
def __init__(self, iterable: Iterable[_T], /) -> None: ...
281+
def __init__(self, iterable: Iterable[_KT], /) -> None: ...
282282
def copy(self) -> Self: ...
283-
def elements(self) -> Iterator[_T]: ...
284-
def most_common(self, n: int | None = None) -> list[tuple[_T, int]]: ...
283+
def elements(self) -> Iterator[_KT]: ...
284+
def most_common(self, n: int | None = None) -> list[tuple[_KT, int]]: ...
285285
@classmethod
286286
def fromkeys(cls, iterable: Any, v: int | None = None) -> NoReturn: ... # type: ignore[override]
287287
@overload
288288
def subtract(self, iterable: None = None, /) -> None: ...
289289
@overload
290-
def subtract(self, mapping: Mapping[_T, int], /) -> None: ...
290+
def subtract(self, mapping: Mapping[_KT, int], /) -> None: ...
291291
@overload
292-
def subtract(self, iterable: Iterable[_T], /) -> None: ...
292+
def subtract(self, iterable: Iterable[_KT], /) -> None: ...
293293
# Unlike dict.update(), use Mapping instead of SupportsKeysAndGetItem for the first overload
294294
# (source code does an `isinstance(other, Mapping)` check)
295295
#
296296
# The second overload is also deliberately different to dict.update()
297-
# (if it were `Iterable[_T] | Iterable[tuple[_T, int]]`,
297+
# (if it were `Iterable[_KT] | Iterable[tuple[_KT, int]]`,
298298
# the tuples would be added as keys, breaking type safety)
299299
@overload # type: ignore[override]
300-
def update(self, m: Mapping[_T, int], /, **kwargs: int) -> None: ...
300+
def update(self: Counter[str], m: Mapping[str, int], /, **kwargs: int) -> None: ...
301301
@overload
302-
def update(self, iterable: Iterable[_T], /, **kwargs: int) -> None: ...
302+
def update(self: Counter[str], iterable: Iterable[str], /, **kwargs: int) -> None: ...
303303
@overload
304-
def update(self, iterable: None = None, /, **kwargs: int) -> None: ...
304+
def update(self: Counter[str], iterable: None = None, /, **kwargs: int) -> None: ...
305+
@overload
306+
def update(self, m: Mapping[_KT, int], /) -> None: ...
307+
@overload
308+
def update(self, iterable: Iterable[_KT], /) -> None: ...
309+
@overload
310+
def update(self, iterable: None = None, /) -> None: ...
305311
def total(self) -> int: ...
306-
def __missing__(self, key: _T) -> int: ...
307-
def __delitem__(self, elem: object) -> None: ...
308-
def __eq__(self, other: object) -> bool: ...
309-
def __ne__(self, other: object) -> bool: ...
310-
def __le__(self, other: Counter[Any]) -> bool: ...
311-
def __lt__(self, other: Counter[Any]) -> bool: ...
312-
def __ge__(self, other: Counter[Any]) -> bool: ...
313-
def __gt__(self, other: Counter[Any]) -> bool: ...
314-
def __add__(self, other: Counter[_S]) -> Counter[_T | _S]: ...
315-
def __sub__(self, other: Counter[_T]) -> Counter[_T]: ...
316-
def __and__(self, other: Counter[_T]) -> Counter[_T]: ...
317-
def __or__(self, other: Counter[_S]) -> Counter[_T | _S]: ... # type: ignore[override]
312+
def __missing__(self, key: _KT) -> int: ...
313+
def __delitem__(self, elem: Hashable) -> None: ...
314+
def __eq__(self, other: Hashable) -> bool: ...
315+
def __ne__(self, other: Hashable) -> bool: ...
316+
def __le__(self, other: Counter[Hashable]) -> bool: ...
317+
def __lt__(self, other: Counter[Hashable]) -> bool: ...
318+
def __ge__(self, other: Counter[Hashable]) -> bool: ...
319+
def __gt__(self, other: Counter[Hashable]) -> bool: ...
320+
def __add__(self, other: Counter[_KT2]) -> Counter[_KT | _KT2]: ...
321+
def __sub__(self, other: Counter[_KT]) -> Counter[_KT]: ...
322+
def __and__(self, other: Counter[_KT]) -> Counter[_KT]: ...
323+
def __or__(self, other: Counter[_KT2]) -> Counter[_KT | _KT2]: ... # type: ignore[override]
318324
if sys.version_info >= (3, 15):
319-
def __xor__(self, other: Counter[_S]) -> Counter[_T | _S]: ... # type: ignore[override]
325+
def __xor__(self, other: Counter[_KT2]) -> Counter[_KT | _KT2]: ... # type: ignore[override]
320326

321-
def __pos__(self) -> Counter[_T]: ...
322-
def __neg__(self) -> Counter[_T]: ...
327+
def __pos__(self) -> Counter[_KT]: ...
328+
def __neg__(self) -> Counter[_KT]: ...
323329
# several type: ignores because __iadd__ is supposedly incompatible with __add__, etc.
324-
def __iadd__(self, other: SupportsItems[_T, int]) -> Self: ... # type: ignore[misc]
325-
def __isub__(self, other: SupportsItems[_T, int]) -> Self: ...
326-
def __iand__(self, other: SupportsItems[_T, int]) -> Self: ...
327-
def __ior__(self, other: SupportsItems[_T, int]) -> Self: ... # type: ignore[override,misc]
330+
def __iadd__(self, other: SupportsItems[_KT, int]) -> Self: ... # type: ignore[misc]
331+
def __isub__(self, other: SupportsItems[_KT, int]) -> Self: ...
332+
def __iand__(self, other: SupportsItems[_KT, int]) -> Self: ...
333+
def __ior__(self, other: SupportsItems[_KT, int]) -> Self: ... # type: ignore[override,misc]
328334
if sys.version_info >= (3, 15):
329-
def __ixor__(self, other: Counter[_T]) -> Self: ... # type: ignore[misc]
335+
def __ixor__(self, other: Counter[_KT]) -> Self: ... # type: ignore[misc]
330336

331337
# The pure-Python implementations of the "views" classes
332338
# These are exposed at runtime in `collections/__init__.py`
@@ -372,10 +378,10 @@ class OrderedDict(dict[_KT, _VT]):
372378
# See #3800 & https://github.com/python/typing/issues/548#issuecomment-683336963.
373379
@classmethod
374380
@overload
375-
def fromkeys(cls, iterable: Iterable[_T], value: None = None) -> OrderedDict[_T, Any | None]: ...
381+
def fromkeys(cls, iterable: Iterable[_KT], value: None = None) -> OrderedDict[_KT, Any | None]: ...
376382
@classmethod
377383
@overload
378-
def fromkeys(cls, iterable: Iterable[_T], value: _S) -> OrderedDict[_T, _S]: ...
384+
def fromkeys(cls, iterable: Iterable[_KT], value: _T) -> OrderedDict[_KT, _T]: ...
379385
# Keep OrderedDict.setdefault in line with MutableMapping.setdefault, modulo positional-only differences.
380386
@overload
381387
def setdefault(self: OrderedDict[_KT, _T | None], key: _KT, default: None = None) -> _T | None: ...
@@ -393,22 +399,22 @@ class OrderedDict(dict[_KT, _VT]):
393399
@overload
394400
def __or__(self, value: dict[_KT, _VT] | frozendict[_KT, _VT], /) -> Self: ...
395401
@overload
396-
def __or__(self, value: dict[_T1, _T2] | frozendict[_T1, _T2], /) -> OrderedDict[_KT | _T1, _VT | _T2]: ...
402+
def __or__(self, value: dict[_KT2, _T] | frozendict[_KT2, _T], /) -> OrderedDict[_KT | _KT2, _VT | _T]: ...
397403
@overload # type: ignore[override]
398404
def __ror__(self, value: dict[_KT, _VT] | frozendict[_KT, _VT], /) -> Self: ... # type: ignore[override,misc]
399405
@overload
400406
def __ror__( # type: ignore[misc]
401-
self, value: dict[_T1, _T2] | frozendict[_T1, _T2], /
402-
) -> OrderedDict[_KT | _T1, _VT | _T2]: ...
407+
self, value: dict[_KT2, _T] | frozendict[_KT2, _T], /
408+
) -> OrderedDict[_KT | _KT2, _VT | _T]: ...
403409
else:
404410
@overload
405411
def __or__(self, value: dict[_KT, _VT], /) -> Self: ...
406412
@overload
407-
def __or__(self, value: dict[_T1, _T2], /) -> OrderedDict[_KT | _T1, _VT | _T2]: ...
413+
def __or__(self, value: dict[_KT2, _T], /) -> OrderedDict[_KT | _KT2, _VT | _T]: ...
408414
@overload
409415
def __ror__(self, value: dict[_KT, _VT], /) -> Self: ...
410416
@overload
411-
def __ror__(self, value: dict[_T1, _T2], /) -> OrderedDict[_KT | _T1, _VT | _T2]: ... # type: ignore[misc]
417+
def __ror__(self, value: dict[_KT2, _T], /) -> OrderedDict[_KT | _KT2, _VT | _T]: ... # type: ignore[misc]
412418

413419
@disjoint_base
414420
class defaultdict(dict[_KT, _VT]):
@@ -454,11 +460,11 @@ class defaultdict(dict[_KT, _VT]):
454460
@overload # type: ignore[override]
455461
def __or__(self, value: dict[_KT, _VT], /) -> Self: ...
456462
@overload
457-
def __or__(self, value: dict[_T1, _T2], /) -> defaultdict[_KT | _T1, _VT | _T2]: ...
463+
def __or__(self, value: dict[_KT2, _T], /) -> defaultdict[_KT | _KT2, _VT | _T]: ...
458464
@overload # type: ignore[override]
459465
def __ror__(self, value: dict[_KT, _VT], /) -> Self: ...
460466
@overload
461-
def __ror__(self, value: dict[_T1, _T2], /) -> defaultdict[_KT | _T1, _VT | _T2]: ... # type: ignore[misc]
467+
def __ror__(self, value: dict[_KT2, _T], /) -> defaultdict[_KT | _KT2, _VT | _T]: ... # type: ignore[misc]
462468

463469
class ChainMap(MutableMapping[_KT, _VT]):
464470
maps: list[MutableMapping[_KT, _VT]]
@@ -498,27 +504,27 @@ class ChainMap(MutableMapping[_KT, _VT]):
498504
if sys.version_info >= (3, 13):
499505
@classmethod
500506
@overload
501-
def fromkeys(cls, iterable: Iterable[_T], /) -> ChainMap[_T, Any | None]: ...
507+
def fromkeys(cls, iterable: Iterable[_KT], /) -> ChainMap[_KT, Any | None]: ...
502508
else:
503509
@classmethod
504510
@overload
505-
def fromkeys(cls, iterable: Iterable[_T]) -> ChainMap[_T, Any | None]: ...
511+
def fromkeys(cls, iterable: Iterable[_KT]) -> ChainMap[_KT, Any | None]: ...
506512

507513
@classmethod
508514
@overload
509515
# Special-case None: the user probably wants to add non-None values later.
510-
def fromkeys(cls, iterable: Iterable[_T], value: None, /) -> ChainMap[_T, Any | None]: ...
516+
def fromkeys(cls, iterable: Iterable[_KT], value: None, /) -> ChainMap[_KT, Any | None]: ...
511517
@classmethod
512518
@overload
513-
def fromkeys(cls, iterable: Iterable[_T], value: _S, /) -> ChainMap[_T, _S]: ...
519+
def fromkeys(cls, iterable: Iterable[_KT], value: _T, /) -> ChainMap[_KT, _T]: ...
514520
@overload
515521
def __or__(self, other: Mapping[_KT, _VT]) -> Self: ...
516522
@overload
517-
def __or__(self, other: Mapping[_T1, _T2]) -> ChainMap[_KT | _T1, _VT | _T2]: ...
523+
def __or__(self, other: Mapping[_KT2, _T]) -> ChainMap[_KT | _KT2, _VT | _T]: ...
518524
@overload
519525
def __ror__(self, other: Mapping[_KT, _VT]) -> Self: ...
520526
@overload
521-
def __ror__(self, other: Mapping[_T1, _T2]) -> ChainMap[_KT | _T1, _VT | _T2]: ...
527+
def __ror__(self, other: Mapping[_KT2, _T]) -> ChainMap[_KT | _KT2, _VT | _T]: ...
522528
# ChainMap.__ior__ should be kept roughly in line with MutableMapping.update()
523529
@overload # type: ignore[misc]
524530
def __ior__(self, other: SupportsKeysAndGetItem[_KT, _VT]) -> Self: ...

0 commit comments

Comments
 (0)