Skip to content

Commit eb63ca4

Browse files
nicksherronjschaf
authored andcommitted
fix import order and run black for formatting
When using this plugin it causes pylint to complain because astroid is imported before re. I constantly get the following diagnositcs message when using with vim [pylint C0411] standard import "import re" should be placed before "from astroid import MANAGER" (wrong-import-order)
1 parent 3851d14 commit eb63ca4

File tree

1 file changed

+42
-40
lines changed

1 file changed

+42
-40
lines changed

pylint_flask/__init__.py

+42-40
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
'''pylint_flask module'''
1+
"""pylint_flask module"""
22

3-
from astroid import MANAGER
4-
from astroid import nodes
53
import re
64

5+
from astroid import MANAGER, nodes
6+
77

88
def register(_):
9-
'''register is expected by pylint for plugins, but we are creating a
9+
"""register is expected by pylint for plugins, but we are creating a
1010
transform, not registering a checker.
11-
'''
11+
"""
1212
pass
1313

1414

@@ -17,42 +17,40 @@ def copy_node_info(src, dest):
1717
1818
Every node in the AST has to have line number information. Get
1919
the information from the old stmt."""
20-
for attr in ['lineno', 'fromlineno', 'tolineno',
21-
'col_offset', 'parent']:
20+
for attr in ["lineno", "fromlineno", "tolineno", "col_offset", "parent"]:
2221
if hasattr(src, attr):
2322
setattr(dest, attr, getattr(src, attr))
2423

2524

2625
def mark_transformed(node):
27-
'''Mark a node as transformed so we don't process it multiple times.'''
26+
"""Mark a node as transformed so we don't process it multiple times."""
2827
node.pylint_flask_was_transformed = True
2928

3029

3130
def is_transformed(node):
32-
'''Return True if `node` was already transformed.'''
33-
return getattr(node, 'pylint_flask_was_transformed', False)
31+
"""Return True if `node` was already transformed."""
32+
return getattr(node, "pylint_flask_was_transformed", False)
3433

3534

3635
def make_non_magical_flask_import(flask_ext_name):
37-
'''Convert a flask.ext.admin into flask_admin.'''
38-
match = re.match(r'flask\.ext\.(.*)', flask_ext_name)
36+
"""Convert a flask.ext.admin into flask_admin."""
37+
match = re.match(r"flask\.ext\.(.*)", flask_ext_name)
3938
if match is None:
40-
raise LookupError("Module name `{}` doesn't match"
41-
"`flask.ext` style import.")
39+
raise LookupError("Module name `{}` doesn't match" "`flask.ext` style import.")
4240
from_name = match.group(1)
43-
actual_module_name = 'flask_{}'.format(from_name)
41+
actual_module_name = "flask_{}".format(from_name)
4442
return actual_module_name
4543

4644

4745
def transform_flask_from_import(node):
48-
'''Translates a flask.ext from-style import into a non-magical import.
46+
"""Translates a flask.ext from-style import into a non-magical import.
4947
5048
Translates:
5149
from flask.ext import wtf, bcrypt as fcrypt
5250
Into:
5351
import flask_wtf as wtf, flask_bcrypt as fcrypt
5452
55-
'''
53+
"""
5654
new_names = []
5755
# node.names is a list of 2-tuples. Each tuple consists of (name, as_name).
5856
# So, the import would be represented as:
@@ -61,7 +59,7 @@ def transform_flask_from_import(node):
6159
#
6260
# node.names = [('wtf', 'ftw'), ('admin', None)]
6361
for (name, as_name) in node.names:
64-
actual_module_name = 'flask_{}'.format(name)
62+
actual_module_name = "flask_{}".format(name)
6563
new_names.append((actual_module_name, as_name or name))
6664

6765
new_node = nodes.Import()
@@ -72,17 +70,18 @@ def transform_flask_from_import(node):
7270

7371

7472
def is_flask_from_import(node):
75-
'''Predicate for checking if we have the flask module.'''
73+
"""Predicate for checking if we have the flask module."""
7674
# Check for transformation first so we don't double process
77-
return not is_transformed(node) and node.modname == 'flask.ext'
75+
return not is_transformed(node) and node.modname == "flask.ext"
76+
7877

79-
MANAGER.register_transform(nodes.ImportFrom,
80-
transform_flask_from_import,
81-
is_flask_from_import)
78+
MANAGER.register_transform(
79+
nodes.ImportFrom, transform_flask_from_import, is_flask_from_import
80+
)
8281

8382

8483
def transform_flask_from_long(node):
85-
'''Translates a flask.ext.wtf from-style import into a non-magical import.
84+
"""Translates a flask.ext.wtf from-style import into a non-magical import.
8685
8786
Translates:
8887
from flask.ext.wtf import Form
@@ -91,7 +90,7 @@ def transform_flask_from_long(node):
9190
from flask_wtf import Form
9291
from flask_admin.model import InlineFormAdmin
9392
94-
'''
93+
"""
9594
actual_module_name = make_non_magical_flask_import(node.modname)
9695
new_node = nodes.ImportFrom(actual_module_name, node.names, node.level)
9796
copy_node_info(node, new_node)
@@ -100,29 +99,30 @@ def transform_flask_from_long(node):
10099

101100

102101
def is_flask_from_import_long(node):
103-
'''Check if an import is like `from flask.ext.wtf import Form`.'''
102+
"""Check if an import is like `from flask.ext.wtf import Form`."""
104103
# Check for transformation first so we don't double process
105-
return not is_transformed(node) and node.modname.startswith('flask.ext.')
104+
return not is_transformed(node) and node.modname.startswith("flask.ext.")
106105

107-
MANAGER.register_transform(nodes.ImportFrom,
108-
transform_flask_from_long,
109-
is_flask_from_import_long)
106+
107+
MANAGER.register_transform(
108+
nodes.ImportFrom, transform_flask_from_long, is_flask_from_import_long
109+
)
110110

111111

112112
def transform_flask_bare_import(node):
113-
'''Translates a flask.ext.wtf bare import into a non-magical import.
113+
"""Translates a flask.ext.wtf bare import into a non-magical import.
114114
115115
Translates:
116116
import flask.ext.admin as admin
117117
Into:
118118
import flask_admin as admin
119-
'''
119+
"""
120120

121121
new_names = []
122122
for (name, as_name) in node.names:
123-
match = re.match(r'flask\.ext\.(.*)', name)
123+
match = re.match(r"flask\.ext\.(.*)", name)
124124
from_name = match.group(1)
125-
actual_module_name = 'flask_{}'.format(from_name)
125+
actual_module_name = "flask_{}".format(from_name)
126126
new_names.append((actual_module_name, as_name))
127127

128128
new_node = nodes.Import()
@@ -133,10 +133,12 @@ def transform_flask_bare_import(node):
133133

134134

135135
def is_flask_bare_import(node):
136-
'''Check if an import is like `import flask.ext.admin as admin`.'''
137-
return (not is_transformed(node) and
138-
any(['flask.ext' in pair[0] for pair in node.names]))
136+
"""Check if an import is like `import flask.ext.admin as admin`."""
137+
return not is_transformed(node) and any(
138+
["flask.ext" in pair[0] for pair in node.names]
139+
)
140+
139141

140-
MANAGER.register_transform(nodes.Import,
141-
transform_flask_bare_import,
142-
is_flask_bare_import)
142+
MANAGER.register_transform(
143+
nodes.Import, transform_flask_bare_import, is_flask_bare_import
144+
)

0 commit comments

Comments
 (0)