Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions stdlib/@tests/test_cases/check_operator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import operator
from typing import overload


class Result: ...


class Operand: ...


class LessThan(Operand):
def __lt__(self, other: Operand, /) -> Result:
return Result()


class GreaterEqual:
def __ge__(self, other: Operand, /) -> Result:
return Result()


def check_comparisons(lt: LessThan, ge: GreaterEqual) -> None:
operator.lt(lt, lt)
operator.gt(lt, lt)
operator.ge(ge, lt)
operator.le(lt, ge)


def check_one_sided_comparisons(lt: LessThan, ge: GreaterEqual, other: Operand) -> None:
operator.lt(lt, other)
operator.gt(other, lt)
operator.ge(ge, other)
operator.le(other, ge)
operator.lt(1, other) # type: ignore[misc]

operators = {"<": operator.lt, "<=": operator.le, ">": operator.gt, ">=": operator.ge, "==": operator.eq, "!=": operator.ne}
operators["<"](lt, other)


class Overloaded:
@overload
def __lt__(self, other: int, /) -> int: ...
@overload
def __lt__(self, other: str, /) -> str: ...
def __lt__(self, other: int | str, /) -> int | str:
return other


def check_overloaded(value: Overloaded, other: int | str) -> None:
operator.lt(value, other)
66 changes: 54 additions & 12 deletions stdlib/_operator.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@ import sys
from _typeshed import (
ReadableBuffer,
SupportsAdd,
SupportsDunderGE,
SupportsDunderGT,
SupportsDunderLE,
SupportsDunderLT,
SupportsGetItem,
SupportsMul,
SupportsRAdd,
Expand All @@ -26,7 +22,33 @@ _K = TypeVar("_K")
_V = TypeVar("_V")
_P = ParamSpec("_P")

_SupportsComparison: TypeAlias = SupportsDunderLE[Any] | SupportsDunderGE[Any] | SupportsDunderGT[Any] | SupportsDunderLT[Any]
# operator returns the rich comparison result without testing its truth value, so these protocols
# return `object` (unlike the similar protocols in `_typeshed`).
Comment on lines +25 to +26

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# operator returns the rich comparison result without testing its truth value, so these protocols
# return `object` (unlike the similar protocols in `_typeshed`).
# Unlike other stdlib APIs, APIs in the `operator` module return the result of calling a rich-comparison
# method without testing the truth value of that result. Thus, whereas `_typeshed.SupportsDunderLT`
# (a general-purpose API used in multiple locations) returns `SupportsBool`, it's sufficient for
# the version here to return `object`.

@type_check_only
class _SupportsDunderLT(Protocol[_T_contra]):
def __lt__(self, other: _T_contra, /) -> object: ...

@type_check_only
class _SupportsDunderGT(Protocol[_T_contra]):
def __gt__(self, other: _T_contra, /) -> object: ...

@type_check_only
class _SupportsDunderLE(Protocol[_T_contra]):
def __le__(self, other: _T_contra, /) -> object: ...

@type_check_only
class _SupportsDunderGE(Protocol[_T_contra]):
def __ge__(self, other: _T_contra, /) -> object: ...

_SupportsComparisonWith: TypeAlias = _SupportsDunderLT[_T] | _SupportsDunderGT[_T] | _SupportsDunderLE[_T] | _SupportsDunderGE[_T]
_SupportsComparison: TypeAlias = _SupportsComparisonWith[Any]

# _T is the argument type accepted by the other operand's ordering method.
# _SupportsComparison preserves the rule that any two operands with
# ordering methods are accepted. For example, if x.__lt__ has separate int and
# str overloads and y is int | str, neither overload accepts the whole union.
# The fallback accepts lt(x, y) because int and str each define an ordering method.
_ComparisonOperand: TypeAlias = _T | _SupportsComparison

@type_check_only
class _SupportsInversion(Protocol[_T_co]):
Expand Down Expand Up @@ -177,13 +199,33 @@ class _SupportsIConcat(Protocol[_T_contra, _T_co]):
def __iadd__(self, value: _T_contra, /) -> _T_co: ...
def __getitem__(self, key: Any) -> Any: ... # method needs only exist

# All four comparison functions must have the same signature, or we get false-positive errors
def lt(a: _SupportsComparison, b: _SupportsComparison, /) -> Any: ...
def le(a: _SupportsComparison, b: _SupportsComparison, /) -> Any: ...
def eq(a: object, b: object, /) -> Any: ...
def ne(a: object, b: object, /) -> Any: ...
def ge(a: _SupportsComparison, b: _SupportsComparison, /) -> Any: ...
def gt(a: _SupportsComparison, b: _SupportsComparison, /) -> Any: ...
# Either operand can implement the comparison. Keep the four ordering functions' signatures
# identical so mypy infers a common callable type when they are stored in a dictionary (see check_operator.py).
@overload
def lt(a: _SupportsComparisonWith[_T], b: _ComparisonOperand[_T], /) -> Any: ...
@overload
def lt(a: _ComparisonOperand[_T], b: _SupportsComparisonWith[_T], /) -> Any: ...

@overload
def le(a: _SupportsComparisonWith[_T], b: _ComparisonOperand[_T], /) -> Any: ...
@overload
def le(a: _ComparisonOperand[_T], b: _SupportsComparisonWith[_T], /) -> Any: ...

# Equality accepts any operands. Using Any lets mypy infer a callable value type for
# ops = {"<": lt, "==": eq}, so ops[symbol](a, b) type-checks.
def eq(a: Any, b: Any, /) -> Any: ...
def ne(a: Any, b: Any, /) -> Any: ...

@overload
def ge(a: _SupportsComparisonWith[_T], b: _ComparisonOperand[_T], /) -> Any: ...
@overload
def ge(a: _ComparisonOperand[_T], b: _SupportsComparisonWith[_T], /) -> Any: ...

@overload
def gt(a: _SupportsComparisonWith[_T], b: _ComparisonOperand[_T], /) -> Any: ...
@overload
def gt(a: _ComparisonOperand[_T], b: _SupportsComparisonWith[_T], /) -> Any: ...

def not_(a: object, /) -> bool: ...
def truth(a: object, /) -> bool: ...
def is_(a: object, b: object, /) -> bool: ...
Expand Down
Loading