def module_imports_on_top_of_file(logical_line, indent_level, checker_state, noqa):
 def is_string_literal(line):
  if (line[0] in 'uUbB'):
   line = line[1:]
  if (line and (line[0] in 'rR')):
   line = line[1:]
  return (line and ((line[0] == '"') or (line[0] == "'")))
 allowed_try_keywords = ('try', 'except', 'else', 'finally')
 if indent_level:
  return
 if (not logical_line):
  return
 if noqa:
  return
 line = logical_line
 if (line.startswith('import ') or line.startswith('from ')):
  if checker_state.get('seen_non_imports', False):
   (yield (0, 'E402 module level import not at top of file'))
 elif any((line.startswith(kw) for kw in allowed_try_keywords)):
  return
 elif is_string_literal(line):
  if checker_state.get('seen_docstring', False):
   checker_state['seen_non_imports'] = True
  else:
   checker_state['seen_docstring'] = True
 else:
  checker_state['seen_non_imports'] = True

def module_imports_on_top_of_file ( logical_line , indent_level , checker_state , noqa ) : def is_string_literal ( line ) : if ( line [ 0 ] in 'uUbB' ) : line = line [ 1 : ] if ( line and ( line [ 0 ] in 'rR' ) ) : line = line [ 1 : ] return ( line and ( ( line [ 0 ] == '"' ) or ( line [ 0 ] == "'" ) ) ) allowed_try_keywords = ( 'try' , 'except' , 'else' , 'finally' ) if indent_level : return if ( not logical_line ) : return if noqa : return line = logical_line if ( line . startswith ( 'import ' ) or line . startswith ( 'from ' ) ) : if checker_state . get ( 'seen_non_imports' , False ) : ( yield ( 0 , 'E402 module level import not at top of file' ) ) elif any ( ( line . startswith ( kw ) for kw in allowed_try_keywords ) ) : return elif is_string_literal ( line ) : if checker_state . get ( 'seen_docstring' , False ) : checker_state [ 'seen_non_imports' ] = True else : checker_state [ 'seen_docstring' ] = True else : checker_state [ 'seen_non_imports' ] = True
place imports at the top of the file .