@@ -22,7 +22,8 @@ _K = TypeVar("_K")
2222_V = TypeVar ("_V" )
2323_P = ParamSpec ("_P" )
2424
25- # operator returns the rich comparison result without testing its truth value.
25+ # operator returns the rich comparison result without testing its truth value, so these protocols
26+ # return `object` (unlike the similar protocols in `_typeshed`).
2627@type_check_only
2728class _SupportsDunderLT (Protocol [_T_contra ]):
2829 def __lt__ (self , other : _T_contra , / ) -> object : ...
@@ -39,18 +40,15 @@ class _SupportsDunderLE(Protocol[_T_contra]):
3940class _SupportsDunderGE (Protocol [_T_contra ]):
4041 def __ge__ (self , other : _T_contra , / ) -> object : ...
4142
42- # Some projects use _SupportsComparison without a type argument.
43- _OperandT = TypeVar ("_OperandT" , default = Any )
44- _SupportsComparison : TypeAlias = (
45- _SupportsDunderLT [_OperandT ] | _SupportsDunderGT [_OperandT ] | _SupportsDunderLE [_OperandT ] | _SupportsDunderGE [_OperandT ]
46- )
43+ _SupportsComparisonWith : TypeAlias = _SupportsDunderLT [_T ] | _SupportsDunderGT [_T ] | _SupportsDunderLE [_T ] | _SupportsDunderGE [_T ]
44+ _SupportsComparison : TypeAlias = _SupportsComparisonWith [Any ]
4745
4846# _T is the argument type accepted by the other operand's ordering method.
49- # _SupportsComparison[Any] preserves the rule that any two operands with
47+ # _SupportsComparison preserves the rule that any two operands with
5048# ordering methods are accepted. For example, if x.__lt__ has separate int and
5149# str overloads and y is int | str, neither overload accepts the whole union.
5250# The fallback accepts lt(x, y) because int and str each define an ordering method.
53- _ComparisonOperand : TypeAlias = _T | _SupportsComparison [ Any ]
51+ _ComparisonOperand : TypeAlias = _T | _SupportsComparison
5452
5553@type_check_only
5654class _SupportsInversion (Protocol [_T_co ]):
@@ -201,32 +199,32 @@ class _SupportsIConcat(Protocol[_T_contra, _T_co]):
201199 def __iadd__ (self , value : _T_contra , / ) -> _T_co : ...
202200 def __getitem__ (self , key : Any ) -> Any : ... # method needs only exist
203201
204- # Either operand can implement the comparison. Dispatch tables rely on all four
205- # ordering functions having the same signatures (#6462 ).
202+ # Either operand can implement the comparison. Keep the four ordering functions' signatures
203+ # identical so mypy infers a common callable type when they are stored in a dictionary (see check_operator.py ).
206204@overload
207- def lt (a : _SupportsComparison [_T ], b : _ComparisonOperand [_T ], / ) -> Any : ...
205+ def lt (a : _SupportsComparisonWith [_T ], b : _ComparisonOperand [_T ], / ) -> Any : ...
208206@overload
209- def lt (a : _ComparisonOperand [_T ], b : _SupportsComparison [_T ], / ) -> Any : ...
207+ def lt (a : _ComparisonOperand [_T ], b : _SupportsComparisonWith [_T ], / ) -> Any : ...
210208
211209@overload
212- def le (a : _SupportsComparison [_T ], b : _ComparisonOperand [_T ], / ) -> Any : ...
210+ def le (a : _SupportsComparisonWith [_T ], b : _ComparisonOperand [_T ], / ) -> Any : ...
213211@overload
214- def le (a : _ComparisonOperand [_T ], b : _SupportsComparison [_T ], / ) -> Any : ...
212+ def le (a : _ComparisonOperand [_T ], b : _SupportsComparisonWith [_T ], / ) -> Any : ...
215213
216- # Equality accepts any operands. Using Any also lets mypy infer a callable
217- # for a dispatch table containing both equality and ordering functions .
214+ # Equality accepts any operands. Using Any lets mypy infer a callable value type for
215+ # ops = {"<": lt, "==": eq}, so ops[symbol](a, b) type-checks .
218216def eq (a : Any , b : Any , / ) -> Any : ...
219217def ne (a : Any , b : Any , / ) -> Any : ...
220218
221219@overload
222- def ge (a : _SupportsComparison [_T ], b : _ComparisonOperand [_T ], / ) -> Any : ...
220+ def ge (a : _SupportsComparisonWith [_T ], b : _ComparisonOperand [_T ], / ) -> Any : ...
223221@overload
224- def ge (a : _ComparisonOperand [_T ], b : _SupportsComparison [_T ], / ) -> Any : ...
222+ def ge (a : _ComparisonOperand [_T ], b : _SupportsComparisonWith [_T ], / ) -> Any : ...
225223
226224@overload
227- def gt (a : _SupportsComparison [_T ], b : _ComparisonOperand [_T ], / ) -> Any : ...
225+ def gt (a : _SupportsComparisonWith [_T ], b : _ComparisonOperand [_T ], / ) -> Any : ...
228226@overload
229- def gt (a : _ComparisonOperand [_T ], b : _SupportsComparison [_T ], / ) -> Any : ...
227+ def gt (a : _ComparisonOperand [_T ], b : _SupportsComparisonWith [_T ], / ) -> Any : ...
230228
231229def not_ (a : object , / ) -> bool : ...
232230def truth (a : object , / ) -> bool : ...
0 commit comments