TL;DR;

The eval() method of the Frame object is not “inheriting” the __future__ imports from the frame.

Long explanation

Suppose I have a fixture in which I write my assert statements. If this fixture has strings on the assert statement then when the assert fails I get the wrong kind of errors.

The problem is that the pytest assertion rewrite mechanism is calling eval() on the code that contains the frame

To better illustrate what happens, see this gist

The tests have the following output:

The output of test_plugin_fixture (omitted some output for brevity):

$ pytest -k test_plugin_fixture

        msg = e.value.msg
>       assert msg.startswith('assert <classes.ValueAndString object'), msg
E       AssertionError: ValueError: ValueAndString(2, 'lkajflaskjfalskf')  << Error!

While debugging I found out that the ValueAndString constructor was being called 3 times, instead of 2. The 3rd call happened in code.py:100, which is part of the py lib library. The “wrong” behaviour can be seen on the run_pyframe_eval.py.

I’ve tested this code on Pytest 2.6.4 with Python 2.7.8 in Windows 7 - 64bits.