diff --git a/changelog/3061.bugfix.rst b/changelog/3061.bugfix.rst new file mode 100644 index 000000000..1bdd1064e --- /dev/null +++ b/changelog/3061.bugfix.rst @@ -0,0 +1 @@ +Fix ``ImportWarning`` triggered by explicit relative imports in assertion-rewritten package modules. diff --git a/src/_pytest/assertion/rewrite.py b/src/_pytest/assertion/rewrite.py index eceed611f..92f4ab10d 100644 --- a/src/_pytest/assertion/rewrite.py +++ b/src/_pytest/assertion/rewrite.py @@ -45,6 +45,14 @@ else: return ast.Call(a, b, c, None, None) +if sys.version_info >= (3, 4): + from importlib.util import spec_from_file_location +else: + + def spec_from_file_location(*_, **__): + return None + + class AssertionRewritingHook(object): """PEP302 Import hook which rewrites asserts.""" @@ -213,6 +221,8 @@ class AssertionRewritingHook(object): # Normally, this attribute is 3.2+. mod.__cached__ = pyc mod.__loader__ = self + # Normally, this attribute is 3.4+ + mod.__spec__ = spec_from_file_location(name, co.co_filename, loader=self) py.builtin.exec_(co, mod.__dict__) except: # noqa if name in sys.modules: diff --git a/tox.ini b/tox.ini index cb3846d54..810d5597f 100644 --- a/tox.ini +++ b/tox.ini @@ -204,9 +204,6 @@ filterwarnings = ignore:.*type argument to addoption.*:DeprecationWarning # produced by python >=3.5 on execnet (pytest-xdist) ignore:.*inspect.getargspec.*deprecated, use inspect.signature.*:DeprecationWarning - # ignore warning about package resolution using __spec__ or __package__ - # should be a temporary solution, see #3061 for discussion - ignore:.*can't resolve package from __spec__ or __package__.*:ImportWarning [flake8] max-line-length = 120