Skip to content

Commit 5393c7d

Browse files
authored
Merge pull request #613 from sigmavirus24/pr/523
Allow all dunder variables above imports
2 parents 860369c + 08a6d8a commit 5393c7d

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

pycodestyle.py

+3
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@
134134
'while',
135135
)))
136136
)
137+
DUNDER_REGEX = re.compile(r'^__([^\s]+)__ = ')
137138

138139
# Work around Python < 2.6 behaviour, which does not generate NL after
139140
# a comment which is on a line by itself.
@@ -955,6 +956,8 @@ def is_string_literal(line):
955956
if line.startswith('import ') or line.startswith('from '):
956957
if checker_state.get('seen_non_imports', False):
957958
yield 0, "E402 module level import not at top of file"
959+
elif re.match(DUNDER_REGEX, line):
960+
return
958961
elif any(line.startswith(kw) for kw in allowed_try_keywords):
959962
# Allow try, except, else, finally keywords intermixed with imports in
960963
# order to support conditional importing

testsuite/E40.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,17 @@
1111

1212
import myclass
1313
import foo.bar.yourclass
14-
#: E402
14+
#: Okay
1515
__all__ = ['abc']
1616

17+
import foo
18+
#: Okay
19+
__version__ = "42"
20+
21+
import foo
22+
#: Okay
23+
__author__ = "Simon Gomizelj"
24+
1725
import foo
1826
#: Okay
1927
try:

0 commit comments

Comments
 (0)