##// END OF EJS Templates
import-checker: assume absolute and use modern import checker...
Gregory Szorc -
r49723:a52f5bfc default
parent child Browse files
Show More
@@ -59,21 +59,6 b' requirealias = {'
59 }
59 }
60
60
61
61
62 def usingabsolute(root):
63 """Whether absolute imports are being used."""
64 if sys.version_info[0] >= 3:
65 return True
66
67 for node in ast.walk(root):
68 if isinstance(node, ast.ImportFrom):
69 if node.module == '__future__':
70 for n in node.names:
71 if n.name == 'absolute_import':
72 return True
73
74 return False
75
76
77 def walklocal(root):
62 def walklocal(root):
78 """Recursively yield all descendant nodes but not in a different scope"""
63 """Recursively yield all descendant nodes but not in a different scope"""
79 todo = collections.deque(ast.iter_child_nodes(root))
64 todo = collections.deque(ast.iter_child_nodes(root))
@@ -403,21 +388,10 b' def imported_modules(source, modulename,'
403
388
404
389
405 def verify_import_convention(module, source, localmods):
390 def verify_import_convention(module, source, localmods):
406 """Verify imports match our established coding convention.
391 """Verify imports match our established coding convention."""
407
392 root = ast.parse(source)
408 We have 2 conventions: legacy and modern. The modern convention is in
409 effect when using absolute imports.
410
393
411 The legacy convention only looks for mixed imports. The modern convention
412 is much more thorough.
413 """
414 root = ast.parse(source)
415 absolute = usingabsolute(root)
416
417 if absolute:
418 return verify_modern_convention(module, root, localmods)
394 return verify_modern_convention(module, root, localmods)
419 else:
420 return verify_stdlib_on_own_line(root)
421
395
422
396
423 def verify_modern_convention(module, root, localmods, root_col_offset=0):
397 def verify_modern_convention(module, root, localmods, root_col_offset=0):
@@ -618,33 +592,6 b' def verify_modern_convention(module, roo'
618 )
592 )
619
593
620
594
621 def verify_stdlib_on_own_line(root):
622 """Given some python source, verify that stdlib imports are done
623 in separate statements from relative local module imports.
624
625 >>> list(verify_stdlib_on_own_line(ast.parse('import sys, foo')))
626 [('mixed imports\\n stdlib: sys\\n relative: foo', 1)]
627 >>> list(verify_stdlib_on_own_line(ast.parse('import sys, os')))
628 []
629 >>> list(verify_stdlib_on_own_line(ast.parse('import foo, bar')))
630 []
631 """
632 for node in ast.walk(root):
633 if isinstance(node, ast.Import):
634 from_stdlib = {False: [], True: []}
635 for n in node.names:
636 from_stdlib[n.name in stdlib_modules].append(n.name)
637 if from_stdlib[True] and from_stdlib[False]:
638 yield (
639 'mixed imports\n stdlib: %s\n relative: %s'
640 % (
641 ', '.join(sorted(from_stdlib[True])),
642 ', '.join(sorted(from_stdlib[False])),
643 ),
644 node.lineno,
645 )
646
647
648 class CircularImport(Exception):
595 class CircularImport(Exception):
649 pass
596 pass
650
597
General Comments 0
You need to be logged in to leave comments. Login now