summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIan Lee <IanLee1521@gmail.com>2014-12-20 19:10:25 -0800
committerIan Lee <IanLee1521@gmail.com>2014-12-20 19:10:25 -0800
commitbc57ff0434cd0d3ca7d0b4857086993bea8b613c (patch)
tree660ba8c57e1f6afb538e693dbe8f3248ca3e34c5
parent40b1861c00ce6d0a18d6705dcc99d990aa0cf3bf (diff)
parentd6da0b82a3c5d13c51d61da01c1a6eb3be16cf9a (diff)
downloadpep8-bc57ff0434cd0d3ca7d0b4857086993bea8b613c.tar.gz
Merge branch 'issue-304'
-rwxr-xr-xpep8.py17
-rw-r--r--testsuite/E40.py25
2 files changed, 35 insertions, 7 deletions
diff --git a/pep8.py b/pep8.py
index 914d7fd..92a10db 100755
--- a/pep8.py
+++ b/pep8.py
@@ -47,8 +47,6 @@ W warnings
"""
from __future__ import with_statement
-__version__ = '1.6.0a0'
-
import os
import sys
import re
@@ -64,6 +62,8 @@ try:
except ImportError:
from ConfigParser import RawConfigParser
+__version__ = '1.6.0a0'
+
DEFAULT_EXCLUDE = '.svn,CVS,.bzr,.hg,.git,__pycache__,.tox'
DEFAULT_IGNORE = 'E123,E226,E24,E704'
try:
@@ -850,7 +850,8 @@ def module_imports_on_top_of_file(
Okay: # this is a comment\nimport os
Okay: '''this is a module docstring'''\nimport os
Okay: r'''this is a module docstring'''\nimport os
- Okay: __version__ = "123"\nimport os
+ Okay: try:\n import x\nexcept:\n pass\nelse:\n pass\nimport y
+ Okay: try:\n import x\nexcept:\n pass\nfinally:\n pass\nimport y
E402: a=1\nimport os
E402: 'One string'\n"Two string"\nimport os
E402: a=1\nfrom sys import x
@@ -864,6 +865,8 @@ def module_imports_on_top_of_file(
line = line[1:]
return line and (line[0] == '"' or line[0] == "'")
+ allowed_try_keywords = ('try', 'except', 'else', 'finally')
+
if indent_level: # Allow imports in conditional statements or functions
return
if not logical_line: # Allow empty lines or comments
@@ -873,10 +876,10 @@ def module_imports_on_top_of_file(
line = logical_line
if line.startswith('import ') or line.startswith('from '):
if checker_state.get('seen_non_imports', False):
- yield 0, "E402 import not at top of file"
- elif line.startswith('__version__ '):
- # These lines should be included after the module's docstring, before
- # any other code, separated by a blank line above and below.
+ yield 0, "E402 module level import not at top of file"
+ elif any(line.startswith(kw) for kw in allowed_try_keywords):
+ # Allow try, except, else, finally keywords intermixed with imports in
+ # order to support conditional importing
return
elif is_string_literal(line):
# The first literal is a docstring, allow it. Otherwise, report error.
diff --git a/testsuite/E40.py b/testsuite/E40.py
index d921c25..1051e32 100644
--- a/testsuite/E40.py
+++ b/testsuite/E40.py
@@ -11,3 +11,28 @@ from foo.bar.yourclass import YourClass
import myclass
import foo.bar.yourclass
+#: E402
+__all__ = ['abc']
+
+import foo
+#: Okay
+try:
+ import foo
+except:
+ pass
+else:
+ print('imported foo')
+finally:
+ print('made attempt to import foo')
+
+import bar
+#: E402
+VERSION = '1.2.3'
+
+import foo
+#: E402
+import foo
+
+a = 1
+
+import bar