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

Fix ImportWarning triggered by explicit relative imports #3613

Merged
merged 2 commits into from
Jun 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/3061.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix ``ImportWarning`` triggered by explicit relative imports in assertion-rewritten package modules.
10 changes: 10 additions & 0 deletions src/_pytest/assertion/rewrite.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ def ast_Call(a, b, c):
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."""

Expand Down Expand Up @@ -213,6 +221,8 @@ def load_module(self, name):
# 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:
Expand Down
3 changes: 0 additions & 3 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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