8000 Cherry-pick some stub fixes from typeshed (#11739) · python/mypy@5d71f58 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5d71f58

Browse files
authored
Cherry-pick some stub fixes from typeshed (#11739)
* Typeshed cherry-pick: Relax signature of logging.config.loadConfig (#6577) * Typeshed cherry-pick: Use AbstractSet instead of set in random and inspect (#6574) * Partial typeshed cherry-pick: Add `SupportsRichComparison` type to `_typeshed` (#6583) The original PR doesn't apply cleanly so I only included the changes to builtins to avoid having to manually deal with all the conflicts.
1 parent 510e043 commit 5d71f58

File tree

5 files changed

+53
-30
lines changed

5 files changed

+53
-30
lines changed

mypy/typeshed/stdlib/_typeshed/__init__.pyi

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,26 @@ class SupportsGreaterThan(Protocol):
4545

4646
SupportsGreaterThanT = TypeVar("SupportsGreaterThanT", bound=SupportsGreaterThan) # noqa: Y001
4747

48+
# Comparison protocols
49+
50+
class SupportsDunderLT(Protocol):
51+
def __lt__(self, __other: Any) -> Any: ...
52+
53+
class SupportsDunderGT(Protocol):
54+
def __gt__(self, __other: Any) -> Any: ...
55+
56+
class SupportsDunderLE(Protocol):
57+
def __le__(self, __other: Any) -> Any: ...
58+
59+
class SupportsDunderGE(Protocol):
60+
def __ge__(self, __other: Any) -> Any: ...
61+
62+
class SupportsAllComparisons(SupportsDunderLT, SupportsDunderGT, SupportsDunderLE, SupportsDunderGE, Protocol): ...
63+
64+
SupportsRichComparison = Union[SupportsDunderLT, SupportsDunderGT]
65+
SupportsRichComparisonT = TypeVar("SupportsRichComparisonT", bound=SupportsRichComparison) # noqa: Y001
66+
SupportsAnyComparison = Union[SupportsDunderLE, SupportsDunderGE, SupportsDunderGT, SupportsDunderLT]
67+
4868
class SupportsDivMod(Protocol[_T_contra, _T_co]):
4969
def __divmod__(self, __other: _T_contra) -> _T_co: ...
5070

mypy/typeshed/stdlib/builtins.pyi

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,12 @@ from _typeshed import (
1313
StrOrBytesPath,
1414
SupportsAnext,
1515
SupportsDivMod,
16-
SupportsGreaterThan,
17-
SupportsGreaterThanT,
1816
SupportsKeysAndGetItem,
1917
SupportsLenAndGetItem,
20-
SupportsLessThan,
21-
SupportsLessThanT,
2218
SupportsNext,
2319
SupportsRDivMod,
20+
SupportsRichComparison,
21+
SupportsRichComparisonT,
2422
SupportsTrunc,
2523
SupportsWrite,
2624
)
@@ -779,9 +777,9 @@ class list(MutableSequence[_T], Generic[_T]):
779777
def remove(self, __value: _T) -> None: ...
780778
def reverse(self) -> None: ...
781779
@overload
782-
def sort(self: list[SupportsLessThanT], *, key: None = ..., reverse: bool = ...) -> None: ...
780+
def sort(self: list[SupportsRichComparisonT], *, key: None = ..., reverse: bool = ...) -> None: ...
783781
@overload
784-
def sort(self, *, key: Callable[[_T], SupportsLessThan], reverse: bool = ...) -> None: ...
782+
def sort(self, *, key: Callable[[_T], SupportsRichComparison], reverse: bool = ...) -> None: ...
785783
def __len__(self) -> int: ...
786784
def __iter__(self) -> Iterator[_T]: ...
787785
def __str__(self) -> str: ...
@@ -1146,32 +1144,32 @@ class map(Iterator[_S], Generic[_S]):
11461144

11471145
@overload
11481146
def max(
1149-
__arg1: SupportsGreaterThanT, __arg2: SupportsGreaterThanT, *_args: SupportsGreaterThanT, key: None = ...
1150-
) -> SupportsGreaterThanT: ...
1147+
__arg1: SupportsRichComparisonT, __arg2: SupportsRichComparisonT, *_args: SupportsRichComparisonT, key: None = ...
1148+
) -> SupportsRichComparisonT: ...
11511149
@overload
1152-
def max(__arg1: _T, __arg2: _T, *_args: _T, key: Callable[[_T], SupportsGreaterThan]) -> _T: ...
1150+
def max(__arg1: _T, __arg2: _T, *_args: _T, key: Callable[[_T], SupportsRichComparison]) -> _T: ...
11531151
@overload
1154-
def max(__iterable: Iterable[SupportsGreaterThanT], *, key: None = ...) -> SupportsGreaterThanT: ...
1152+
def max(__iterable: Iterable[SupportsRichComparisonT], *, key: None = ...) -> SupportsRichComparisonT: ...
11551153
@overload
1156-
def max(__iterable: Iterable[_T], *, key: Callable[[_T], SupportsGreaterThan]) -> _T: ...
1154+
def max(__iterable: Iterable[_T], *, key: Callable[[_T], SupportsRichComparison]) -> _T: ...
11571155
@overload
1158-
def max(__iterable: Iterable[SupportsGreaterThanT], *, key: None = ..., default: _T) -> SupportsGreaterThanT | _T: ...
1156+
def max(__iterable: Iterable[SupportsRichComparisonT], *, key: None = ..., default: _T) -> SupportsRichComparisonT | _T: ...
11591157
@overload
1160-
def max(__iterable: Iterable[_T1], *, key: Callable[[_T1], SupportsGreaterThan], default: _T2) -> _T1 | _T2: ...
1158+
def max(__iterable: Iterable[_T1], *, key: Callable[[_T1], SupportsRichComparison], default: _T2) -> _T1 | _T2: ...
11611159
@overload
11621160
def min(
1163-
__arg1: SupportsLessThanT, __arg2: SupportsLessThanT, *_args: SupportsLessThanT, key: None = ...
1164-
) -> SupportsLessThanT: ...
1161+
__arg1: SupportsRichComparisonT, __arg2: SupportsRichComparisonT, *_args: SupportsRichComparisonT, key: None = ...
1162+
) -> SupportsRichComparisonT: ...
11651163
@overload
1166-
def min(__arg1: _T, __arg2: _T, *_args: _T, key: Callable[[_T], SupportsLessThan]) -> _T: ...
1164+
def min(__arg1: _T, __arg2: _T, *_args: _T, key: Callable[[_T], SupportsRichComparison]) -> _T: ...
11671165
@overload
1168-
def min(__iterable: Iterable[SupportsLessThanT], *, key: None = ...) -> SupportsLessThanT: ...
1166+
def min(__iterable: Iterable[SupportsRichComparisonT], *, key: None = ...) -> SupportsRichComparisonT: ...
11691167
@overload
1170-
def min(__iterable: Iterable[_T], *, key: Callable[[_T], SupportsLessThan]) -> _T: ...
1168+
def min(__iterable: Iterable[_T], *, key: Callable[[_T], SupportsRichComparison]) -> _T: ...
11711169
@overload
1172-
def min(__iterable: Iterable[SupportsLessThanT], *, key: None = ..., default: _T) -> SupportsLessThanT | _T: ...
1170+
def min(__iterable: Iterable[SupportsRichComparisonT], *, key: None = ..., default: _T) -> SupportsRichComparisonT | _T: ...
11731171
@overload
1174-
def min(__iterable: Iterable[_T1], *, key: Callable[[_T1], SupportsLessThan], default: _T2) -> _T1 | _T2: ...
1172+
def min(__iterable: Iterable[_T1], *, key: Callable[[_T1], SupportsRichComparison], default: _T2) -> _T1 | _T2: ...
11751173
@overload
11761174
def next(__i: SupportsNext[_T]) -> _T: ...
11771175
@overload
@@ -1382,9 +1380,9 @@ def round(number: SupportsRound[_T], ndigits: SupportsIndex) -> _T: ...
13821380
# for why arg 3 of `setattr` should be annotated with `Any` and not `object`
13831381
def setattr(__obj: object, __name: str, __value: Any) -> None: ...
13841382
@overload
1385-
def sorted(__iterable: Iterable[SupportsLessThanT], *, key: None = ..., reverse: bool = ...) -> list[SupportsLessThanT]: ...
1383+
def sorted(__iterable: Iterable[SupportsRichComparisonT], *, key: None = ..., reverse: bool = ...) -> list[SupportsRichComparisonT]: ...
13861384
@overload
1387-
def sorted(__iterable: Iterable[_T], *, key: Callable[[_T], SupportsLessThan], reverse: bool = ...) -> list[_T]: ...
1385+
def sorted(__iterable: Iterable[_T], *, key: Callable[[_T], SupportsRichComparison], reverse: bool = ...) -> list[_T]: ...
13881386

13891387
if sys.version_info >= (3, 8):
13901388
@overload

mypy/typeshed/stdlib/inspect.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import sys
33
import types
44
from _typeshed import Self
55
from collections import OrderedDict
6-
from collections.abc import Awaitable, Callable, Generator, Mapping, Sequence
6+
from collections.abc import Awaitable, Callable, Generator, Mapping, Sequence, Set as AbstractSet
77
from types import (
88
AsyncGeneratorType,
99
BuiltinFunctionType,
@@ -313,7 +313,7 @@ class ClosureVars(NamedTuple):
313313
nonlocals: Mapping[str, Any]
314314
globals: Mapping[str, Any]
315315
builtins: Mapping[str, Any]
316-
unbound: set[str]
316+
unbound: AbstractSet[str]
317317

318318
def getclosurevars(func: Callable[..., Any]) -> ClosureVars: ...
319319
def unwrap(func: Callable[..., Any], *, stop: Callable[[Any], Any] | None = ...) -> Any: ...

mypy/typeshed/stdlib/logging/config.pyi

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,12 @@ class _OptionalDictConfigArgs(TypedDict, total=False):
4343
class _DictConfigArgs(_OptionalDictConfigArgs, TypedDict):
4444
version: Literal[1]
4545

46-
def dictConfig(config: _DictConfigArgs) -> None: ...
46+
# Accept dict[str, Any] to avoid false positives if called with a dict
47+
# type, since dict types are not compatible with TypedDicts.
48+
#
49+
# Also accept a TypedDict type, to allow callers to use TypedDict
50+
# types, and for somewhat stricter type checking of dict literals.
51+
def dictConfig(config: _DictConfigArgs | dict[str, Any]) -> None: ...
4752

4853
if sys.version_info >= (3, 10):
4954
def fileConfig(

mypy/typeshed/stdlib/random.pyi

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import _random
22
import sys
3-
from collections.abc import Callable, Iterable, MutableSequence, Sequence
3+
from collections.abc import Callable, Iterable, MutableSequence, Sequence, Set as AbstractSet
44
from fractions import Fraction
55
from typing import Any, NoReturn, Tuple, TypeVar
66

@@ -27,9 +27,9 @@ class Random(_random.Random):
2727
) -> list[_T]: ...
2828
def shuffle(self, x: MutableSequence[Any], random: Callable[[], float] | None = ...) -> None: ...
2929
if sys.version_info >= (3, 9):
30-
def sample(self, population: Sequence[_T] | set[_T], k: int, *, counts: Iterable[_T] | None = ...) -> list[_T]: ...
30+
def sample(self, population: Sequence[_T] | AbstractSet[_T], k: int, *, counts: Iterable[_T] | None = ...) -> list[_T]: ...
3131
else:
32-
def sample(self, population: Sequence[_T] | set[_T], k: int) -> list[_T]: ...
32+
def sample(self, population: Sequence[_T] | AbstractSet[_T], k: int) -> list[_T]: ...
3333
def random(self) -> float: ...
3434
def uniform(self, a: float, b: float) -> float: ...
3535
def triangular(self, low: float = ..., high: float = ..., mode: float | None = ...) -> float: ...
@@ -66,10 +66,10 @@ def choices(
6666
def shuffle(x: MutableSequence[Any], random: Callable[[], float] | None = ...) -> None: ...
6767

6868
if sys.version_info >= (3, 9):
69-
def sample(population: Sequence[_T] | set[_T], k: int, *, counts: Iterable[_T] | None = ...) -> list[_T]: ...
69+
def sample(population: Sequence[_T] | AbstractSet[_T], k: int, *, counts: Iterable[_T] | None = ...) -> list[_T]: ...
7070

7171
else:
72-
def sample(population: Sequence[_T] | set[_T], k: int) -> list[_T]: ...
72+
def sample(population: Sequence[_T] | AbstractSet[_T], k: int) -> list[_T]: ...
7373

7474
def random() -> float: ...
7575
def uniform(a: float, b: float) -> float: ...

0 commit comments

Comments
 (0)
0