Merge pull request #3613 from asottile/module_spec_warnings

Fix `ImportWarning` triggered by explicit relative imports
This commit is contained in:
Bruno Oliveira 2018-06-23 18:27:19 -03:00 committed by GitHub
commit 50a0d4fa95
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 3 deletions

View File

@ -0,0 +1 @@
Fix ``ImportWarning`` triggered by explicit relative imports in assertion-rewritten package modules.

View File

@ -45,6 +45,14 @@ else:
return ast.Call(a, b, c, None, None) 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): class AssertionRewritingHook(object):
"""PEP302 Import hook which rewrites asserts.""" """PEP302 Import hook which rewrites asserts."""
@ -213,6 +221,8 @@ class AssertionRewritingHook(object):
# Normally, this attribute is 3.2+. # Normally, this attribute is 3.2+.
mod.__cached__ = pyc mod.__cached__ = pyc
mod.__loader__ = self 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__) py.builtin.exec_(co, mod.__dict__)
except: # noqa except: # noqa
if name in sys.modules: if name in sys.modules:

View File

@ -204,9 +204,6 @@ filterwarnings =
ignore:.*type argument to addoption.*:DeprecationWarning ignore:.*type argument to addoption.*:DeprecationWarning
# produced by python >=3.5 on execnet (pytest-xdist) # produced by python >=3.5 on execnet (pytest-xdist)
ignore:.*inspect.getargspec.*deprecated, use inspect.signature.*:DeprecationWarning 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] [flake8]
max-line-length = 120 max-line-length = 120