Skip to content

Commit

Permalink
Omit relative imports from checking
Browse files Browse the repository at this point in the history
  • Loading branch information
arkq committed Nov 27, 2017
1 parent 39c66b1 commit 619d16a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/flake8_requirements/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ def visit_Import(self, node):
self.imports.append((node, node.names[0].name))

def visit_ImportFrom(self, node):
if node.level != 0:
# Omit relative imports (local modules).
return
self.imports.append((node, node.module))


Expand Down
14 changes: 14 additions & 0 deletions test/test_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,17 @@ def test_3rd_party_hyphen(self):
def test_3rd_party_multi_module(self):
errors = check("import pkg_resources")
self.assertEqual(len(errors), 0)

def test_non_top_level_import(self):
errors = check("def function():\n import cat")
self.assertEqual(len(errors), 1)
self.assertEqual(
errors[0][2],
"I900 'cat' not listed as a requirement",
)

def test_relative_import(self):
errors = check("from . import local")
self.assertEqual(len(errors), 0)
errors = check("from ..local import local")
self.assertEqual(len(errors), 0)

0 comments on commit 619d16a

Please sign in to comment.