Skip to content

Commit 2a2b8ce

Browse files
committed
Fix false-positive warnings from assertion rewrite hook
Fix pytest-dev#2005
1 parent 82fb63c commit 2a2b8ce

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

CHANGELOG.rst

+10-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,15 @@
1212
* When loading plugins, import errors which contain non-ascii messages are now properly handled in Python 2 (`#1998`_).
1313
Thanks `@nicoddemus`_ for the PR.
1414

15+
* Fixed false-positives warnings from assertion rewrite hook for modules that were rewritten but
16+
were later marked explicitly by ``pytest.register_assert_rewrite``
17+
or implicitly as a plugin (`#2005`_).
18+
Thanks `@RonnyPfannschmidt`_ for the report and `@nicoddemus`_ for the PR.
19+
20+
*
21+
22+
*
23+
1524
*
1625

1726

@@ -21,7 +30,7 @@
2130
.. _#1976: https://github.com/pytest-dev/pytest/issues/1976
2231
.. _#1998: https://github.com/pytest-dev/pytest/issues/1998
2332
.. _#2004: https://github.com/pytest-dev/pytest/issues/2004
24-
33+
.. _#2005: https://github.com/pytest-dev/pytest/issues/2005
2534

2635

2736
3.0.3

_pytest/assertion/rewrite.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ def __init__(self, config):
5151
self.fnpats = config.getini("python_files")
5252
self.session = None
5353
self.modules = {}
54+
self._rewritten_names = set()
5455
self._register_with_pkg_resources()
5556
self._must_rewrite = set()
5657

@@ -92,6 +93,8 @@ def find_module(self, name, path=None):
9293
if not self._should_rewrite(name, fn_pypath, state):
9394
return None
9495

96+
self._rewritten_names.add(name)
97+
9598
# The requested module looks like a test file, so rewrite it. This is
9699
# the most magical part of the process: load the source, rewrite the
97100
# asserts, and load the rewritten source. We also cache the rewritten
@@ -178,7 +181,9 @@ def mark_rewrite(self, *names):
178181
"""
179182
already_imported = set(names).intersection(set(sys.modules))
180183
if already_imported:
181-
self._warn_already_imported(already_imported)
184+
for name in names:
185+
if name not in self._rewritten_names:
186+
self._warn_already_imported(already_imported)
182187
self._must_rewrite.update(names)
183188

184189
def _warn_already_imported(self, names):

testing/test_assertrewrite.py

+16
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,22 @@ def test_rewritten():
543543
''')
544544
assert testdir.runpytest_subprocess().ret == 0
545545

546+
def test_remember_rewritten_modules(self, pytestconfig, testdir, monkeypatch):
547+
"""
548+
AssertionRewriteHook should remember rewritten modules so it
549+
doesn't give false positives (#2005).
550+
"""
551+
monkeypatch.syspath_prepend(testdir.tmpdir)
552+
testdir.makepyfile(test_remember_rewritten_modules='')
553+
warnings = []
554+
hook = AssertionRewritingHook(pytestconfig)
555+
monkeypatch.setattr(hook.config, 'warn', lambda code, msg: warnings.append(msg))
556+
hook.find_module('test_remember_rewritten_modules')
557+
hook.load_module('test_remember_rewritten_modules')
558+
hook.mark_rewrite('test_remember_rewritten_modules')
559+
hook.mark_rewrite('test_remember_rewritten_modules')
560+
assert warnings == []
561+
546562

547563
class TestAssertionRewriteHookDetails(object):
548564
def test_loader_is_package_false_for_module(self, testdir):

0 commit comments

Comments
 (0)