8000 Improve protocol return types (#7093) · python/typeshed@54fde0c · GitHub
[go: up one dir, main page]

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 54fde0c

Browse files
authored
Improve protocol return types (#7093)
* Dunder comparisons must return bool. * write() return type should be ignored. * Use custom comparison protocols in _operator.pyi
1 parent 587d15a commit 54fde0c

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

stdlib/_operator.pyi

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import sys
2-
from _typeshed import SupportsAnyComparison
32
from 8000 typing import (
43
Any,
54
AnyStr,
@@ -25,6 +24,24 @@ _K = TypeVar("_K")
2524
_V = TypeVar("_V")
2625
_P = ParamSpec("_P")
2726

27+
# The following protocols return "Any" instead of bool, since the comparison
28+
# operators can be overloaded to return an arbitrary object. For example,
29+
# the numpy.array comparison dunders return another numpy.array.
30+
31+
class _SupportsDunderLT(Protocol):
32+
def __lt__(self, __other: Any) -> Any: ...
33+
34+
class _SupportsDunderGT(Protocol):
35+
def __gt__(self, __other: Any) -> Any: ...
36+
37+
class _SupportsDunderLE(Protocol):
38+
def __le__(self, __other: Any) -> Any: ...
39+
40+
class _SupportsDunderGE(Protocol):
41+
def __ge__(self, __other: Any) -> Any: ...
42+
43+
_SupportsComparison = _SupportsDunderLE | _SupportsDunderGE | _SupportsDunderGT | _SupportsDunderLT
44+
2845
class _SupportsInversion(Protocol[_T_co]):
2946
def __invert__(self) -> _T_co: ...
3047

@@ -35,12 +52,12 @@ class _SupportsPos(Protocol[_T_co]):
3552
def __pos__(self) -> _T_co: ...
3653

3754
# All four comparison functions must have the same signature, or we get false-positive errors
38-
def lt(__a: SupportsAnyComparison, __b: SupportsAnyComparison) -> Any: ...
39-
def le(__a: SupportsAnyComparison, __b: SupportsAnyComparison) -> Any: ...
55+
def lt(__a: _SupportsComparison, __b: _SupportsComparison) -> Any: ...
56+
def le(__a: _SupportsComparison, __b: _SupportsComparison) -> Any: ...
4057
def eq(__a: object, __b: object) -> Any: ...
4158
def ne(__a: object, __b: object) -> Any: ...
42-
def ge(__a: SupportsAnyComparison, __b: SupportsAnyComparison) -> Any: ...
43-
def gt(__a: SupportsAnyComparison, __b: SupportsAnyComparison) -> Any: ...
59+
def ge(__a: _SupportsComparison, __b: _SupportsComparison) -> Any: ...
60+
def gt(__a: _SupportsComparison, __b: _SupportsComparison) -> Any: ...
4461
def not_(__a: object) -> bool: ...
4562
def truth(__a: object) -> bool: ...
4663
def is_(__a: object, __b: object) -> bool: ...

stdlib/_typeshed/__init__.pyi

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,16 @@ class SupportsAnext(Protocol[_T_co]):
3838
# Comparison protocols
3939

4040
class SupportsDunderLT(Protocol):
41-
def __lt__(self, __other: Any) -> Any: ...
41+
def __lt__(self, __other: Any) -> bool: ...
4242

4343
class SupportsDunderGT(Protocol):
44-
def __gt__(self, __other: Any) -> Any: ...
44+
def __gt__(self, __other: Any) -> bool: ...
4545

4646
class SupportsDunderLE(Protocol):
47-
def __le__(self, __other: Any) -> Any: ...
47+
def __le__(self, __other: Any) -> bool: ...
4848

4949
class SupportsDunderGE(Protocol):
50-
def __ge__(self, __other: Any) -> Any: ...
50+
def __ge__(self, __other: Any) -> bool: ...
5151

5252
class SupportsAllComparisons(SupportsDunderLT, SupportsDunderGT, SupportsDunderLE, SupportsDunderGE, Protocol): ...
5353

@@ -181,7 +181,7 @@ class SupportsNoArgReadline(Protocol[_T_co]):
181181

182182
# stable
183183
class SupportsWrite(Protocol[_T_contra]):
184-
def write(self, __s: _T_contra) -> Any: ...
184+
def write(self, __s: _T_contra) -> object: ...
185185

186186
ReadOnlyBuffer = bytes # stable
187187
# Anything that implements the read-write buffer interface.

0 commit comments

Comments
 (0)
0