From d599a535b2739f9a441c33e95ec8bb521516d18b Mon Sep 17 00:00:00 2001 From: Bas van Beek <43369155+BvB93@users.noreply.github.com> Date: Sat, 4 Sep 2021 18:16:53 +0200 Subject: [PATCH] Allow `math.trunc` to only accept `__trunc__`-supporting objects (#6003) Add SupportsTrunc protocol --- stdlib/_typeshed/__init__.pyi | 3 +++ stdlib/math.pyi | 3 ++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/stdlib/_typeshed/__init__.pyi b/stdlib/_typeshed/__init__.pyi index cf67751f8..493c4d088 100644 --- a/stdlib/_typeshed/__init__.pyi +++ b/stdlib/_typeshed/__init__.pyi @@ -41,6 +41,9 @@ class SupportsLenAndGetItem(Protocol[_T_co]): def __len__(self) -> int: ... def __getitem__(self, __k: int) -> _T_co: ... +class SupportsTrunc(Protocol): + def __trunc__(self) -> int: ... + # Mapping-like protocols # stable diff --git a/stdlib/math.pyi b/stdlib/math.pyi index 5a1e71a1c..1046676be 100644 --- a/stdlib/math.pyi +++ b/stdlib/math.pyi @@ -1,4 +1,5 @@ import sys +from _typeshed import SupportsTrunc from typing import Iterable, SupportsFloat, SupportsInt, Tuple, overload e: float @@ -94,7 +95,7 @@ def sinh(__x: SupportsFloat) -> float: ... def sqrt(__x: SupportsFloat) -> float: ... def tan(__x: SupportsFloat) -> float: ... def tanh(__x: SupportsFloat) -> float: ... -def trunc(__x: SupportsFloat) -> int: ... +def trunc(__x: SupportsTrunc) -> int: ... if sys.version_info >= (3, 9): def ulp(__x: SupportsFloat) -> float: ...