Skip to content

Commit

Permalink
Show hopefully friendlier messages
Browse files Browse the repository at this point in the history
Since relative imports don't work with the `import <>` syntax, suggest
importing the names directly. In these cases, the `i.module` is `None`
and the `i.level` has the number of levels down in the relative import,
e.g. `1` means the current directory.

If there's only one name, suggest:

    use 'import module.name' instead of 'from module import name'

This implements the enhancement proposed in sfstpala#1.
  • Loading branch information
ericbn committed Mar 27, 2022
1 parent 7f85171 commit f5a6d82
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
22 changes: 18 additions & 4 deletions flake8_import_style/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

__version__ = pkg_resources.get_distribution(__package__).version

I801 = "I801 use 'import {module}' instead of 'from {module} import {names}'"
I801 = "I801 use 'import {stmt}' instead of 'from {module} import {names}'"


class I8(object):
Expand All @@ -22,7 +22,21 @@ def run(self):
if isinstance(i, ast.ImportFrom):
if i.module == "__future__":
continue
message = I801.format(
module=(i.module or "..."),
names=", ".join(i.name for i in i.names))
names = ", ".join(i.name for i in i.names)
if i.module is None:
# Relative imports don't work with the `import <>` syntax.
message = I801.format(
stmt=names,
module="." * i.level,
names=names)
elif len(i.names) == 1:
message = I801.format(
stmt="{0}.{1}".format(i.module, names),
module=i.module,
names=names)
else:
message = I801.format(
stmt=i.module,
module=i.module,
names=names)
yield (i.lineno, i.col_offset, message, "I801")
15 changes: 12 additions & 3 deletions flake8_import_style/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def test_run(self):
self.assertEqual(list(i8.run()), [(
1,
0,
"I801 use 'import ...' instead of 'from ... import obj'",
"I801 use 'import obj' instead of 'from . import obj'",
"I801",
)])

Expand All @@ -30,7 +30,7 @@ def test_run(self):
self.assertEqual(list(i8.run()), [(
1,
0,
"I801 use 'import ...' instead of 'from ... import obj'",
"I801 use 'import obj' instead of 'from .. import obj'",
"I801",
)])

Expand All @@ -39,7 +39,16 @@ def test_run(self):
self.assertEqual(list(i8.run()), [(
1,
0,
"I801 use 'import mod' instead of 'from mod import obj'",
"I801 use 'import mod.obj' instead of 'from mod import obj'",
"I801",
)])

tree = ast.parse("from mod import obj1, obj2")
i8 = flake8_import_style.I8(tree)
self.assertEqual(list(i8.run()), [(
1,
0,
"I801 use 'import mod' instead of 'from mod import obj1, obj2'",
"I801",
)])

Expand Down

0 comments on commit f5a6d82

Please sign in to comment.