contrib/import-checker.py
changeset 28330 f3fb24e36d61
parent 27621 39845b064041
child 28400 a84fc98a8af5
equal deleted inserted replaced
28329:e69343e80aec 28330:f3fb24e36d61
   364     """
   364     """
   365     topmodule = module.split('.')[0]
   365     topmodule = module.split('.')[0]
   366     fromlocal = fromlocalfunc(module, localmods)
   366     fromlocal = fromlocalfunc(module, localmods)
   367 
   367 
   368     # Whether a local/non-stdlib import has been performed.
   368     # Whether a local/non-stdlib import has been performed.
   369     seenlocal = False
   369     seenlocal = None
   370     # Whether a relative, non-symbol import has been seen.
   370     # Whether a relative, non-symbol import has been seen.
   371     seennonsymbolrelative = False
   371     seennonsymbolrelative = False
   372     # The last name to be imported (for sorting).
   372     # The last name to be imported (for sorting).
   373     lastname = None
   373     lastname = None
   374     # Relative import levels encountered so far.
   374     # Relative import levels encountered so far.
   401                 lastname = name
   401                 lastname = name
   402 
   402 
   403             # stdlib imports should be before local imports.
   403             # stdlib imports should be before local imports.
   404             stdlib = name in stdlib_modules
   404             stdlib = name in stdlib_modules
   405             if stdlib and seenlocal and node.col_offset == root_col_offset:
   405             if stdlib and seenlocal and node.col_offset == root_col_offset:
   406                 yield msg('stdlib import follows local import: %s', name)
   406                 yield msg('stdlib import "%s" follows local import: %s',
       
   407                           name, seenlocal)
   407 
   408 
   408             if not stdlib:
   409             if not stdlib:
   409                 seenlocal = True
   410                 seenlocal = name
   410 
   411 
   411             # Import of sibling modules should use relative imports.
   412             # Import of sibling modules should use relative imports.
   412             topname = name.split('.')[0]
   413             topname = name.split('.')[0]
   413             if topname == topmodule:
   414             if topname == topmodule:
   414                 yield msg('import should be relative: %s', name)
   415                 yield msg('import should be relative: %s', name)
   435             # symbol import.
   436             # symbol import.
   436             if fullname != '__future__':
   437             if fullname != '__future__':
   437                 if not fullname or fullname in stdlib_modules:
   438                 if not fullname or fullname in stdlib_modules:
   438                     yield msg('relative import of stdlib module')
   439                     yield msg('relative import of stdlib module')
   439                 else:
   440                 else:
   440                     seenlocal = True
   441                     seenlocal = fullname
   441 
   442 
   442             # Direct symbol import is only allowed from certain modules and
   443             # Direct symbol import is only allowed from certain modules and
   443             # must occur before non-symbol imports.
   444             # must occur before non-symbol imports.
   444             if node.module and node.col_offset == root_col_offset:
   445             if node.module and node.col_offset == root_col_offset:
   445                 found = fromlocal(node.module, node.level)
   446                 found = fromlocal(node.module, node.level)