Skip to content

Commit a39c3eb

Browse files
committed
Add protocol overload to definition of builtins.divmod.
PiperOrigin-RevId: 606466608
1 parent 834cd27 commit a39c3eb

2 files changed

Lines changed: 19 additions & 0 deletions

File tree

pytype/stubs/builtins/builtins.pytd

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,19 @@ def coerce(x: _T, y: _T) -> tuple[_T, _T]: ... # E.g. coerce([], [1]) -> ([], [
6565
def compile(source, filename: str, mode: str, flags: int = ..., dont_inherit: int = ..., optimize: int = ...) -> code: ...
6666
def delattr(object, name: Union[str, bytes, bytearray]) -> None: ...
6767
def dir(*args, **kwargs) -> list[str]: ...
68+
69+
class _SupportsDivMod(Protocol[_T, _T2, _T3]):
70+
def __divmod__(self, other: _T) -> tuple[_T2, _T3]: ...
71+
72+
@overload
6873
def divmod(x: int, y: int) -> tuple[int, int]: ...
74+
@overload
6975
def divmod(x: Union[int, float], y: Union[int, float]) -> tuple[float, float]: ...
76+
@overload
7077
def divmod(x: Union[int, float, complex], y: Union[int, float, complex]) -> tuple[complex, complex]: ...
78+
@overload
79+
def divmod(x: _SupportsDivMod[_T, _T2, _T3], y: _T) -> tuple[_T2, _T3]: ...
80+
7181
def eval(src, *args, **kwargs) -> Any: ... # Can't say *anything* about the result -- different from "-> object"
7282
def exec(src, *args, **kwargs) -> NoneType: ...
7383
def execfile(filename: str, *args, **kwargs) -> NoneType: ...

pytype/tests/test_builtins4.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -768,6 +768,15 @@ def __index__(self) -> int:
768768
print(x[C()])
769769
""")
770770

771+
def test_divmod(self):
772+
self.Check("""
773+
import datetime
774+
from typing import Tuple
775+
assert_type(divmod(1, 2), Tuple[int, int])
776+
assert_type(divmod(datetime.timedelta(1), datetime.timedelta(2)),
777+
Tuple[int, datetime.timedelta])
778+
""")
779+
771780

772781
class SetMethodsTest(test_base.BaseTest):
773782
"""Tests for methods of the `set` class."""

0 commit comments

Comments
 (0)