Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Implement modulo
  • Loading branch information
brettcannon committed Aug 11, 2020
1 parent 86cadd3 commit c0f1f32
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -11,6 +11,7 @@ Unravelling Python source code.
1. `a @ b``operator.matmul(a, b)`
1. `a / b``operator.truediv(a, b)`
1. `a // b``operator.floordiv(a, b)`
1. `a % b``operator.mod(a, b)`

## Syntax to (potentially) unravel

Expand Down Expand Up @@ -66,7 +67,6 @@ Taken from the [`keyword` module](https://github.com/python/cpython/blob/v3.8.3/
### Tokens
Taken from the [`token` module](https://github.com/python/cpython/blob/v3.8.3/Lib/token.py).

1. `%`
1. `**`
1. `<<`
1. `>>`
Expand Down
1 change: 1 addition & 0 deletions desugar/operator.py
Expand Up @@ -54,3 +54,4 @@ def binary_op(lhs: Any, rhs: Any, /) -> Any:
matmul = _create_binary_op("matmul", "@")
truediv = _create_binary_op("truediv", "/")
floordiv = _create_binary_op("floordiv", "//")
mod = _create_binary_op("mod", "%")
23 changes: 23 additions & 0 deletions tests/test_operator.py
Expand Up @@ -24,6 +24,9 @@ def __truediv__(self, _):
def __floordiv__(self, _):
return "__floordiv__"

def __mod__(self, _):
return "__mod__"


class LHSNotImplemented:
def __init__(self):
Expand Down Expand Up @@ -54,6 +57,10 @@ def __floordiv__(self, _):
self.called += 1
return NotImplemented

def __mod__(self, _):
self.called += 1
return NotImplemented


class RHS:
def __radd__(self, _):
Expand All @@ -74,6 +81,9 @@ def __rtruediv__(self, _):
def __rfloordiv__(self, _):
return "__rfloordiv__"

def __rmod__(self, _):
return "__rmod__"


class RHSNotImplemented:
def __init__(self):
Expand Down Expand Up @@ -104,6 +114,10 @@ def __rfloordiv__(self, _):
self.rcalled += 1
return NotImplemented

def __rmod__(self, _):
self.rcalled += 1
return NotImplemented


class LHSRHS(LHS, RHS):
pass
Expand Down Expand Up @@ -240,6 +254,15 @@ class TestFloorDivision(BinaryOperationTests):
rhs_method = "__rfloordiv__"


@pytest.mark.parametrize("op", [operator.mod, desugar.operator.mod])
class TestModulo(BinaryOperationTests):

"""Tests for desugar.operator.mod()."""

lhs_method = "__mod__"
rhs_method = "__rmod__"


# @pytest.mark.parametrize("op", [operator.XXX, desugar.operator.XXX])
# class TestXXX(BinaryOperationTests):

Expand Down

0 comments on commit c0f1f32

Please sign in to comment.