Skip to content

Commit 35d6bbf

Browse files
committed
Fix pytest-dev#1290: Py3.5's @ operator/assertion rewriting.
1 parent b5dc7d9 commit 35d6bbf

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-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 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: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,17 @@ 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):
284+
def f():
285+
class Matrix:
286+
def __init__(self, num):
287+
self.num = num
288+
def __matmul__(self, other):
289+
return self.num * other.num
290+
assert Matrix(2) @ Matrix(3) == 6
291+
getmsg(f, must_pass=True)
292+
282293
def test_call(self):
283294
def g(a=42, *args, **kwargs):
284295
return False

0 commit comments

Comments
 (0)