Ensure rewritten modules don't inherit __future__ flags from pytest

In a recent refactoring we enabled all __future__ features in pytest
modules, but that has the unwanted side effect of propagating those
features to compile()'d modules inside assertion rewriting, unless
we pass dont_inherit=False to compile().
This commit is contained in:
Bruno Oliveira 2017-04-06 14:58:07 -03:00
parent 83b241b449
commit 1b5f898dc5
2 changed files with 19 additions and 1 deletions

View File

@ -337,7 +337,7 @@ def _rewrite_test(config, fn):
return None, None return None, None
rewrite_asserts(tree, fn, config) rewrite_asserts(tree, fn, config)
try: try:
co = compile(tree, fn.strpath, "exec") co = compile(tree, fn.strpath, "exec", dont_inherit=True)
except SyntaxError: except SyntaxError:
# It's possible that this error is from some bug in the # It's possible that this error is from some bug in the
# assertion rewriting, but I don't know of a fast way to tell. # assertion rewriting, but I don't know of a fast way to tell.

View File

@ -712,6 +712,24 @@ def test_rewritten():
result.stdout.fnmatch_lines(['*= 1 passed in *=*']) result.stdout.fnmatch_lines(['*= 1 passed in *=*'])
assert 'pytest-warning summary' not in result.stdout.str() assert 'pytest-warning summary' not in result.stdout.str()
@pytest.mark.skipif(sys.version_info[0] > 2, reason='python 2 only')
def test_rewrite_future_imports(self, testdir):
"""Test that rewritten modules don't inherit the __future__ flags
from the assertrewrite module.
assertion.rewrite imports __future__.division (and others), so
ensure rewritten modules don't inherit those flags.
The test below will fail if __future__.division is enabled
"""
testdir.makepyfile('''
def test():
x = 1 / 2
assert type(x) is int
''')
result = testdir.runpytest()
assert result.ret == 0
class TestAssertionRewriteHookDetails(object): class TestAssertionRewriteHookDetails(object):
def test_loader_is_package_false_for_module(self, testdir): def test_loader_is_package_false_for_module(self, testdir):