##// END OF EJS Templates
import-checker: establish new function for verifying import conventions...
Gregory Szorc -
r25702:ab2c5163 default
parent child Browse files
Show More
@@ -272,7 +272,13 b' def imported_modules(source, modulename,'
272 continue
272 continue
273 yield found[1]
273 yield found[1]
274
274
275 def verify_stdlib_on_own_line(source):
275 def verify_import_convention(module, source):
276 """Verify imports match our established coding convention."""
277 root = ast.parse(source)
278
279 return verify_stdlib_on_own_line(root)
280
281 def verify_stdlib_on_own_line(root):
276 """Given some python source, verify that stdlib imports are done
282 """Given some python source, verify that stdlib imports are done
277 in separate statements from relative local module imports.
283 in separate statements from relative local module imports.
278
284
@@ -280,14 +286,14 b' def verify_stdlib_on_own_line(source):'
280 annoying lib2to3 bug in relative import rewrites:
286 annoying lib2to3 bug in relative import rewrites:
281 http://bugs.python.org/issue19510.
287 http://bugs.python.org/issue19510.
282
288
283 >>> list(verify_stdlib_on_own_line('import sys, foo'))
289 >>> list(verify_stdlib_on_own_line(ast.parse('import sys, foo')))
284 ['mixed imports\\n stdlib: sys\\n relative: foo']
290 ['mixed imports\\n stdlib: sys\\n relative: foo']
285 >>> list(verify_stdlib_on_own_line('import sys, os'))
291 >>> list(verify_stdlib_on_own_line(ast.parse('import sys, os')))
286 []
292 []
287 >>> list(verify_stdlib_on_own_line('import foo, bar'))
293 >>> list(verify_stdlib_on_own_line(ast.parse('import foo, bar')))
288 []
294 []
289 """
295 """
290 for node in ast.walk(ast.parse(source)):
296 for node in ast.walk(root):
291 if isinstance(node, ast.Import):
297 if isinstance(node, ast.Import):
292 from_stdlib = {False: [], True: []}
298 from_stdlib = {False: [], True: []}
293 for n in node.names:
299 for n in node.names:
@@ -367,7 +373,7 b' def main(argv):'
367 src = f.read()
373 src = f.read()
368 used_imports[modname] = sorted(
374 used_imports[modname] = sorted(
369 imported_modules(src, modname, localmods, ignore_nested=True))
375 imported_modules(src, modname, localmods, ignore_nested=True))
370 for error in verify_stdlib_on_own_line(src):
376 for error in verify_import_convention(modname, src):
371 any_errors = True
377 any_errors = True
372 print source_path, error
378 print source_path, error
373 f.close()
379 f.close()
General Comments 0
You need to be logged in to leave comments. Login now