Skip to content

Treat divmod like a binary operator #4585

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 20, 2018
Merged
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
2 changes: 2 additions & 0 deletions mypy/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1321,6 +1321,7 @@ def accept(self, visitor: ExpressionVisitor[T]) -> T:
'*': '__mul__',
'/': '__truediv__',
'%': '__mod__',
'divmod': '__divmod__',
'//': '__floordiv__',
'**': '__pow__',
'@': '__matmul__',
Expand Down Expand Up @@ -1356,6 +1357,7 @@ def accept(self, visitor: ExpressionVisitor[T]) -> T:
'__mul__': '__rmul__',
'__truediv__': '__rtruediv__',
'__mod__': '__rmod__',
'__divmod__': '__rdivmod__',
'__floordiv__': '__rfloordiv__',
'__pow__': '__rpow__',
'__matmul__': '__rmatmul__',
Expand Down
6 changes: 6 additions & 0 deletions mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -3272,6 +3272,12 @@ def visit_call_expr(self, expr: CallExpr) -> None:
expr.analyzed.accept(self)
elif refers_to_fullname(expr.callee, 'builtins.dict'):
expr.analyzed = self.translate_dict_call(expr)
elif refers_to_fullname(expr.callee, 'builtins.divmod'):
if not self.check_fixed_args(expr, 2, 'divmod'):
return
expr.analyzed = OpExpr('divmod', expr.args[0], expr.args[1])
expr.analyzed.line = expr.line
expr.analyzed.accept(self)
else:
# Normal call expression.
for a in expr.args:
Expand Down
45 changes: 45 additions & 0 deletions test-data/unit/check-expressions.test
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,51 @@ tmp/m.py:7: error: Invalid index type "int" for "A"; expected type "str"
tmp/m.py:8: error: Invalid index type "int" for "A"; expected type "str"


[case testDivmod]
from typing import Tuple, Union, SupportsInt
_Decimal = Union[Decimal, int]
class Decimal(SupportsInt):
def __init__(self, int) -> None: ...
def __divmod__(self, other: _Decimal) -> Tuple[Decimal, Decimal]: ...
def __rdivmod__(self, other: _Decimal) -> Tuple[Decimal, Decimal]: ...

i = 8
f = 8.0
d = Decimal(8)

reveal_type(divmod(i, i)) # E: Revealed type is 'Tuple[builtins.int, builtins.int]'
reveal_type(divmod(f, i)) # E: Revealed type is 'Tuple[builtins.float, builtins.float]'
reveal_type(divmod(d, i)) # E: Revealed type is 'Tuple[__main__.Decimal, __main__.Decimal]'

reveal_type(divmod(i, f)) # E: Revealed type is 'Tuple[builtins.float, builtins.float]'
reveal_type(divmod(f, f)) # E: Revealed type is 'Tuple[builtins.float, builtins.float]'
divmod(d, f) # E: Unsupported operand types for divmod ("Decimal" and "float")

reveal_type(divmod(i, d)) # E: Revealed type is 'Tuple[__main__.Decimal, __main__.Decimal]'
divmod(f, d) # E: Unsupported operand types for divmod ("float" and "Decimal")
reveal_type(divmod(d, d)) # E: Revealed type is 'Tuple[__main__.Decimal, __main__.Decimal]'

# Now some bad calls
divmod() # E: 'divmod' expects 2 arguments \
# E: Too few arguments for "divmod"
divmod(7) # E: 'divmod' expects 2 arguments \
# E: Too few arguments for "divmod"
divmod(7, 8, 9) # E: 'divmod' expects 2 arguments \
# E: Too many arguments for "divmod"
divmod(_x=7, _y=9) # E: 'divmod' must be called with 2 positional arguments

divmod('foo', 'foo') # E: Unsupported left operand type for divmod ("str")
divmod(i, 'foo') # E: Unsupported operand types for divmod ("int" and "str")
divmod(f, 'foo') # E: Unsupported operand types for divmod ("float" and "str")
divmod(d, 'foo') # E: Unsupported operand types for divmod ("Decimal" and "str")

divmod('foo', i) # E: Unsupported operand types for divmod ("str" and "int")
divmod('foo', f) # E: Unsupported operand types for divmod ("str" and "float")
divmod('foo', d) # E: Unsupported operand types for divmod ("str" and "Decimal")

[builtins fixtures/divmod.pyi]


-- Unary operators
-- ---------------

Expand Down
21 changes: 21 additions & 0 deletions test-data/unit/fixtures/divmod.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from typing import TypeVar, Tuple, SupportsInt
class object:
def __init__(self): pass

class int(SupportsInt):
def __divmod__(self, other: int) -> Tuple[int, int]: pass
def __rdivmod__(self, other: int) -> Tuple[int, int]: pass

class float(SupportsInt):
def __divmod__(self, other: float) -> Tuple[float, float]: pass
def __rdivmod__(self, other: float) -> Tuple[float, float]: pass


class tuple: pass
class function: pass
class str: pass
class type: pass
class ellipsis: pass

_N = TypeVar('_N', int, float)
def divmod(_x: _N, _y: _N) -> Tuple[_N, _N]: ...