Skip to content
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

@ operator lets the assertion rewrite hang up #1290

Closed
Shinkenjoe opened this issue Dec 27, 2015 · 2 comments
Closed

@ operator lets the assertion rewrite hang up #1290

Shinkenjoe opened this issue Dec 27, 2015 · 2 comments
Labels
status: critical grave problem or usability issue that affects lots of users status: help wanted developers would like help from experts on this topic type: backward compatibility might present some backward compatibility issues which should be carefully noted in the changelog type: bug problem that needs to be addressed type: enhancement new feature or API change, should be merged into features branch
Milestone

Comments

@Shinkenjoe
Copy link

Hi there,

It seems like the @ operator isn't recognized by something int the assertion_rewrite procedure.
With --assert=plain there is no problem.

I did some fake test to single out the problem in fake_matrix.py:

class FakeMatrix:

    def __init__(self, num):
        self.num = num

    def __matmul__(self, other):
        return self.num * other.num

and the test file:

from fake_matrix import FakeMatrix

def test_multmat_operator():
    assert FakeMatrix(2) @ FakeMatrix(3) == 6

traceback:

platform linux -- Python 3.5.0+, pytest-2.8.5, py-1.4.31, pluggy-0.3.1 -- /home/snail/.virtualenvs/fip/bin/python3.5
cachedir: .cache
rootdir: /home/snail/comp/python/fluent/pytest_bug_report, inifile: 
collected 0 items / 1 errors 

================================================================= ERRORS =================================================================
____________________________________________________ ERROR collecting test_multmat.py ____________________________________________________
/home/snail/.virtualenvs/fip/lib/python3.5/site-packages/py/_path/local.py:650: in pyimport
    __import__(modname)
<frozen importlib._bootstrap>:969: in _find_and_load
    ???
<frozen importlib._bootstrap>:954: in _find_and_load_unlocked
    ???
<frozen importlib._bootstrap>:892: in _find_spec
    ???
<frozen importlib._bootstrap>:873: in _find_spec_legacy
    ???
/home/snail/.virtualenvs/fip/lib/python3.5/site-packages/_pytest/assertion/rewrite.py:143: in find_module
    source_stat, co = _rewrite_test(state, fn_pypath)
/home/snail/.virtualenvs/fip/lib/python3.5/site-packages/_pytest/assertion/rewrite.py:290: in _rewrite_test
    rewrite_asserts(tree)
/home/snail/.virtualenvs/fip/lib/python3.5/site-packages/_pytest/assertion/rewrite.py:348: in rewrite_asserts
    AssertionRewriter().run(mod)
/home/snail/.virtualenvs/fip/lib/python3.5/site-packages/_pytest/assertion/rewrite.py:569: in run
    new.extend(self.visit(child))
/usr/lib/python3.5/ast.py:245: in visit
    return visitor(node)
/home/snail/.virtualenvs/fip/lib/python3.5/site-packages/_pytest/assertion/rewrite.py:677: in visit_Assert
    top_condition, explanation = self.visit(assert_.test)
/usr/lib/python3.5/ast.py:245: in visit
    return visitor(node)
/home/snail/.virtualenvs/fip/lib/python3.5/site-packages/_pytest/assertion/rewrite.py:852: in visit_Compare
    left_res, left_expl = self.visit(comp.left)
/usr/lib/python3.5/ast.py:245: in visit
    return visitor(node)
/home/snail/.virtualenvs/fip/lib/python3.5/site-packages/_pytest/assertion/rewrite.py:761: in visit_BinOp
    symbol = binop_map[binop.op.__class__]
E   KeyError: <class '_ast.MatMult'>

My system is debian linux stretch, and as you can see I'm using Python 3.5.0+, pytest-2.8.5 on a virtualenv

@RonnyPfannschmidt RonnyPfannschmidt added type: bug problem that needs to be addressed type: enhancement new feature or API change, should be merged into features branch status: help wanted developers would like help from experts on this topic type: backward compatibility might present some backward compatibility issues which should be carefully noted in the changelog status: critical grave problem or usability issue that affects lots of users labels Dec 27, 2015
@RonnyPfannschmidt RonnyPfannschmidt added this to the 2.9 milestone Feb 3, 2016
tomviner added a commit to tomviner/pytest that referenced this issue Feb 5, 2016
@tomviner
Copy link
Contributor

tomviner commented Feb 5, 2016

Adding binop_map[ast.MatMult] = "@" did the trick.

See #1361

tomviner added a commit to tomviner/pytest that referenced this issue Feb 5, 2016
tomviner added a commit to tomviner/pytest that referenced this issue Feb 5, 2016
@tomviner
Copy link
Contributor

tomviner commented Feb 5, 2016

Not strictly involved with this bug, but if you over can't sleep, you can look at pytest's generated rewritten ast, converted back to python with these instructions (now updated for Python 3).

For a failing version of your test @Shinkenjoe it generates this:

py.test test_matrix.py -s
============================================================== test session starts ===============================================================
platform linux -- Python 3.5.1, pytest-2.8.8.dev1, py-1.4.31, pluggy-0.3.1
rootdir: /home/tom/Dropbox/DellXPS-backup/dev/pytest-dev/ast_rewrite_gist, inifile: 
collecting 0 items

import builtins as @py_builtins
import _pytest.assertion.rewrite as @pytest_ar
from fake_matrix import FakeMatrix

def test_multmat_operator_non_equal():
    @py_assert1 = 2
    @py_assert3 = FakeMatrix(@py_assert1)
    @py_assert6 = 3
    @py_assert8 = FakeMatrix(@py_assert6)
    @py_assert10 = @py_assert3 @ @py_assert8
    @py_assert12 = 5
    @py_assert11 = @py_assert10 == @py_assert12
    if not @py_assert11:
        @py_format14 = @pytest_ar._call_reprcompare(('==',), (@py_assert11,
            ), (
            """(%(py4)s
{%(py4)s = %(py0)s(%(py2)s)
} @ %(py9)s
{%(py9)s = %(py5)s(%(py7)s)
}) == %(py13)s"""
            ,), (@py_assert10, @py_assert12)) % {'py9': @pytest_ar.
            _saferepr(@py_assert8), 'py13': @pytest_ar._saferepr(
            @py_assert12), 'py2': @pytest_ar._saferepr(@py_assert1), 'py4':
            @pytest_ar._saferepr(@py_assert3), 'py0': @pytest_ar._saferepr(
            FakeMatrix) if 'FakeMatrix' in @py_builtins.locals() or
            @pytest_ar._should_repr_global_name(FakeMatrix) else
            'FakeMatrix', 'py5': @pytest_ar._saferepr(FakeMatrix) if 
            'FakeMatrix' in @py_builtins.locals() or @pytest_ar.
            _should_repr_global_name(FakeMatrix) else 'FakeMatrix', 'py7':
            @pytest_ar._saferepr(@py_assert6)}
        @py_format16 = ('' + 'assert %(py15)s') % {'py15': @py_format14}
        raise AssertionError(@pytest_ar._format_explanation(@py_format16))
    (@py_assert1) = (@py_assert3) = (@py_assert6) = (@py_assert8) = (
        @py_assert10) = (@py_assert11) = (@py_assert12) = None

collected 1 items 

test_matrix.py F

==================================================================== FAILURES ====================================================================
________________________________________________________ test_multmat_operator_non_equal _________________________________________________________

    def test_multmat_operator_non_equal():
>       assert FakeMatrix(2) @ FakeMatrix(3) == 5
E       assert (FakeMatrix(2) @ FakeMatrix(3)) == 5
E        +  where FakeMatrix(2) = FakeMatrix(2)
E        +  and   FakeMatrix(3) = FakeMatrix(3)

test_matrix.py:4: AssertionError
============================================================ 1 failed in 0.01 seconds ============================================================

jsonn pushed a commit to jsonn/pkgsrc that referenced this issue Apr 14, 2016
2.9.1
=====

**Bug Fixes**

* Improve error message when a plugin fails to load.
  Thanks `@nicoddemus`_ for the PR.

* Fix (`#1178 <https://github.com/pytest-dev/pytest/issues/1178>`_):
  ``pytest.fail`` with non-ascii characters raises an internal pytest error.
  Thanks `@nicoddemus`_ for the PR.

* Fix (`#469`_): junit parses report.nodeid incorrectly, when params IDs
  contain ``::``. Thanks `@tomviner`_ for the PR (`#1431`_).

* Fix (`#578 <https://github.com/pytest-dev/pytest/issues/578>`_): SyntaxErrors
  containing non-ascii lines at the point of failure generated an internal
  py.test error.
  Thanks `@asottile`_ for the report and `@nicoddemus`_ for the PR.

* Fix (`#1437`_): When passing in a bytestring regex pattern to parameterize
  attempt to decode it as utf-8 ignoring errors.

* Fix (`#649`_): parametrized test nodes cannot be specified to run on the command line.


.. _#1437: pytest-dev/pytest#1437
.. _#469: pytest-dev/pytest#469
.. _#1431: pytest-dev/pytest#1431
.. _#649: pytest-dev/pytest#649

.. _@asottile: https://github.com/asottile


2.9.0
=====

**New Features**

* New ``pytest.mark.skip`` mark, which unconditionally skips marked tests.
  Thanks `@MichaelAquilina`_ for the complete PR (`#1040`_).

* ``--doctest-glob`` may now be passed multiple times in the command-line.
  Thanks `@jab`_ and `@nicoddemus`_ for the PR.

* New ``-rp`` and ``-rP`` reporting options give the summary and full output
  of passing tests, respectively. Thanks to `@codewarrior0`_ for the PR.

* ``pytest.mark.xfail`` now has a ``strict`` option, which makes ``XPASS``
  tests to fail the test suite (defaulting to ``False``). There's also a
  ``xfail_strict`` ini option that can be used to configure it project-wise.
  Thanks `@rabbbit`_ for the request and `@nicoddemus`_ for the PR (`#1355`_).

* ``Parser.addini`` now supports options of type ``bool``.
  Thanks `@nicoddemus`_ for the PR.

* New ``ALLOW_BYTES`` doctest option. This strips ``b`` prefixes from byte strings
  in doctest output (similar to ``ALLOW_UNICODE``).
  Thanks `@jaraco`_ for the request and `@nicoddemus`_ for the PR (`#1287`_).

* Give a hint on ``KeyboardInterrupt`` to use the ``--fulltrace`` option to show the errors.
  Fixes `#1366`_.
  Thanks to `@hpk42`_ for the report and `@RonnyPfannschmidt`_ for the PR.

* Catch ``IndexError`` exceptions when getting exception source location.
  Fixes a pytest internal error for dynamically generated code (fixtures and tests)
  where source lines are fake by intention.

**Changes**

* **Important**: `py.code <http://pylib.readthedocs.org/en/latest/code.html>`_ has been
  merged into the ``pytest`` repository as ``pytest._code``. This decision
  was made because ``py.code`` had very few uses outside ``pytest`` and the
  fact that it was in a different repository made it difficult to fix bugs on
  its code in a timely manner. The team hopes with this to be able to better
  refactor out and improve that code.
  This change shouldn't affect users, but it is useful to let users aware
  if they encounter any strange behavior.

  Keep in mind that the code for ``pytest._code`` is **private** and
  **experimental**, so you definitely should not import it explicitly!

  Please note that the original ``py.code`` is still available in
  `pylib <http://pylib.readthedocs.org>`_.

* ``pytest_enter_pdb`` now optionally receives the pytest config object.
  Thanks `@nicoddemus`_ for the PR.

* Removed code and documentation for Python 2.5 or lower versions,
  including removal of the obsolete ``_pytest.assertion.oldinterpret`` module.
  Thanks `@nicoddemus`_ for the PR (`#1226`_).

* Comparisons now always show up in full when ``CI`` or ``BUILD_NUMBER`` is
  found in the environment, even when ``-vv`` isn't used.
  Thanks `@The-Compiler`_ for the PR.

* ``--lf`` and ``--ff`` now support long names: ``--last-failed`` and
  ``--failed-first`` respectively.
  Thanks `@MichaelAquilina`_ for the PR.

* Added expected exceptions to ``pytest.raises`` fail message.

* Collection only displays progress ("collecting X items") when in a terminal.
  This avoids cluttering the output when using ``--color=yes`` to obtain
  colors in CI integrations systems (`#1397`_).

**Bug Fixes**

* The ``-s`` and ``-c`` options should now work under ``xdist``;
  ``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.

* Fix formatting utf-8 explanation messages (`#1379`_).
  Thanks `@biern`_ for the PR.

* Fix `traceback style docs`_ to describe all of the available options
  (auto/long/short/line/native/no), with `auto` being the default since v2.6.
  Thanks `@hackebrot`_ for the PR.

* Fix (`#1422`_): junit record_xml_property doesn't allow multiple records
  with same name.

.. _`traceback style docs`: https://pytest.org/latest/usage.html#modifying-python-traceback-printing

.. _#1422: pytest-dev/pytest#1422
.. _#1379: pytest-dev/pytest#1379
.. _#1366: pytest-dev/pytest#1366
.. _#1040: pytest-dev/pytest#1040
.. _#680: pytest-dev/pytest#680
.. _#1287: pytest-dev/pytest#1287
.. _#1226: pytest-dev/pytest#1226
.. _#1290: pytest-dev/pytest#1290
.. _#1355: pytest-dev/pytest#1355
.. _#1397: pytest-dev/pytest#1397
.. _@biern: https://github.com/biern
.. _@MichaelAquilina: https://github.com/MichaelAquilina
.. _@bukzor: https://github.com/bukzor
.. _@hpk42: https://github.com/hpk42
.. _@nicoddemus: https://github.com/nicoddemus
.. _@jab: https://github.com/jab
.. _@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
.. _@RonnyPfannschmidt: https://github.com/RonnyPfannschmidt
.. _@rabbbit: https://github.com/rabbbit
.. _@hackebrot: https://github.com/hackebrot
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: critical grave problem or usability issue that affects lots of users status: help wanted developers would like help from experts on this topic type: backward compatibility might present some backward compatibility issues which should be carefully noted in the changelog type: bug problem that needs to be addressed type: enhancement new feature or API change, should be merged into features branch
Projects
None yet
Development

No branches or pull requests

3 participants