diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 28dd664e1..d36fd5e3c 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -57,11 +57,15 @@ ``Config.fromdictargs`` now represents its input much more faithfully. Thanks to `@bukzor`_ for the complete PR (`#680`_). +* Fix (`#1290`_): support Python 3.5's `@` operator in assertion rewriting. + Thanks `@Shinkenjoe`_ for report with test case and `@tomviner`_ for the PR. + .. _#1040: https://github.com/pytest-dev/pytest/pull/1040 .. _#680: https://github.com/pytest-dev/pytest/issues/680 .. _#1287: https://github.com/pytest-dev/pytest/pull/1287 .. _#1226: https://github.com/pytest-dev/pytest/pull/1226 +.. _#1290: https://github.com/pytest-dev/pytest/pull/1290 .. _@MichaelAquilina: https://github.com/MichaelAquilina .. _@bukzor: https://github.com/bukzor .. _@nicoddemus: https://github.com/nicoddemus @@ -69,7 +73,8 @@ .. _@codewarrior0: https://github.com/codewarrior0 .. _@jaraco: https://github.com/jaraco .. _@The-Compiler: https://github.com/The-Compiler - +.. _@Shinkenjoe: https://github.com/Shinkenjoe +.. _@tomviner: https://github.com/tomviner 2.8.7 diff --git a/_pytest/assertion/rewrite.py b/_pytest/assertion/rewrite.py index 9484ba4b3..14b8e49db 100644 --- a/_pytest/assertion/rewrite.py +++ b/_pytest/assertion/rewrite.py @@ -453,6 +453,11 @@ binop_map = { ast.In: "in", ast.NotIn: "not in" } +# Python 3.5+ compatibility +try: + binop_map[ast.MatMult] = "@" +except AttributeError: + pass # Python 3.4+ compatibility if hasattr(ast, "NameConstant"): diff --git a/testing/test_assertrewrite.py b/testing/test_assertrewrite.py index d06cb3a00..f43c424ca 100644 --- a/testing/test_assertrewrite.py +++ b/testing/test_assertrewrite.py @@ -280,6 +280,19 @@ class TestAssertionRewrite: assert False or 4 % 2 assert getmsg(f) == "assert (False or (4 % 2))" + @pytest.mark.skipif("sys.version_info < (3,5)") + def test_at_operator_issue1290(self, testdir): + testdir.makepyfile(""" + class Matrix: + def __init__(self, num): + self.num = num + def __matmul__(self, other): + return self.num * other.num + + def test_multmat_operator(): + assert Matrix(2) @ Matrix(3) == 6""") + testdir.runpytest().assert_outcomes(passed=1) + def test_call(self): def g(a=42, *args, **kwargs): return False