-
Notifications
You must be signed in to change notification settings - Fork 291
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Imports are always moved to top of the file #459
Comments
@hhatto Imports are also moved above |
@Tadaboody. emm, I meet the same problem. Maybe this patch can solve that imports are moved above def get_module_imports_on_top_of_file(source, import_line_index):
"""return import or from keyword position
example:
> 0: import sys
1: import os
2:
3: def function():
"""
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] == "'")
def is_future_import(line):
nodes = ast.parse(line)
for node in nodes.body:
if isinstance(node, ast.ImportFrom) and node.module == '__future__':
return True
return False
allowed_try_keywords = ('try', 'except', 'else', 'finally')
for cnt, line in enumerate(source):
if not line.rstrip():
continue
elif line.startswith('#'):
continue
if line.startswith('import ') or line.startswith('from '):
if cnt == import_line_index or is_future_import(line):
continue
return cnt
elif pycodestyle.DUNDER_REGEX.match(line):
continue
elif any(line.startswith(kw) for kw in allowed_try_keywords):
continue
elif is_string_literal(line):
return cnt
else:
return cnt
return 0 It adapts to following situation import math
a = 1
import collections
from __future__ import print_function
import functools But it can't solve that import collections
from __future__ import print_function
a = 1 Because, the above code will pass the check of By the way, I think PEP8 is just code style, not a lint. It considers that the problem is not caused by Reference Flake8Rules |
@Hanaasagi Will it fix the case of turning from __future__ import print_function
print(3)
import functools
print(4) into import functools
from __future__ import print_function
print(3)
print(4) Thus creating a syntax error by using |
I think so.
And this patch using |
imports are also moved above |
It is resolved in this issue:
|
I'd like to resolve the remianing issue. |
Running autopep8 on
results in
Instead of
The text was updated successfully, but these errors were encountered: