Skip to content

Commit 7d10701

Browse files
committed
Fix #1290: Py3.5's @ operator/assertion rewriting.
1 parent b5dc7d9 commit 7d10701

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

CHANGELOG

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
2.8.8.dev1
22
----------
33

4+
- fix #1290: support Python 3.5's @ operator in assertion rewriting.
5+
Thanks Shinkenjoe for report with test case and Tom Viner for the PR.
6+
47
2.8.7
58
-----
69

_pytest/assertion/rewrite.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,11 @@ def _call_reprcompare(ops, results, expls, each_obj):
453453
ast.In: "in",
454454
ast.NotIn: "not in"
455455
}
456+
# Python 3.5+ compatibility
457+
try:
458+
binop_map[ast.MatMult] = "@"
459+
except AttributeError:
460+
pass
456461

457462
# Python 3.4+ compatibility
458463
if hasattr(ast, "NameConstant"):

testing/test_assertrewrite.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,19 @@ def f():
279279
assert False or 4 % 2
280280
assert getmsg(f) == "assert (False or (4 % 2))"
281281

282+
@pytest.mark.skipif("sys.version_info < (3,5)")
283+
def test_at_operator_issue1290(self, testdir):
284+
testdir.makepyfile("""
285+
class Matrix:
286+
def __init__(self, num):
287+
self.num = num
288+
def __matmul__(self, other):
289+
return self.num * other.num
290+
291+
def test_multmat_operator():
292+
assert Matrix(2) @ Matrix(3) == 6""")
293+
testdir.runpytest().assert_outcomes(passed=1)
294+
282295
def test_call(self):
283296
def g(a=42, *args, **kwargs):
284297
return False

0 commit comments

Comments
 (0)