Fixed warning H302 when used with six.moves

Checking whether something is a module now also consults ``sys.meta_path``, as
per the import specification. This fixes H302 with six >= 1.7.0, where a custom
finder/loader is used.

Change-Id: I4e401e0fcce077f81a5471692aa6ecd16f26f6ed
Close-bug: 1329002
This commit is contained in:
Alex Gaynor 2014-06-11 12:55:08 -07:00
parent 2d76161033
commit e56eddf55b
1 changed files with 5 additions and 1 deletions

View File

@ -48,6 +48,7 @@ def hacking_import_rules(logical_line, physical_line, filename, noqa):
Okay: import os.path
Okay: from nova.compute import rpcapi
Okay: from os.path import dirname as dirname2 # noqa
Okay: from six.moves.urllib import parse
H302: from os.path import dirname as dirname2
H302: from os.path import (dirname as dirname2)
H303: from os.path import *
@ -65,6 +66,9 @@ def hacking_import_rules(logical_line, physical_line, filename, noqa):
def is_module_for_sure(mod, search_path=sys.path):
mod = mod.replace('(', '') # Ignore parentheses
for finder in sys.meta_path:
if finder.find_module(mod) is not None:
return True
try:
mod_name = mod
while '.' in mod_name:
@ -130,7 +134,7 @@ def hacking_import_rules(logical_line, physical_line, filename, noqa):
if 'from' == split_line[0] and split_line_len > 3:
mod = '.'.join((split_line[1], split_line[3]))
if core.is_import_exception(mod):
return
return
if RE_RELATIVE_IMPORT.search(logical_line):
yield logical_line.find('.'), (
"H304: No relative imports. '%s' is a relative import"