Skip to content

Commit

Permalink
Merge pull request #15 from routablehq/DEV-9930-add-a-flake-8-rule-fo…
Browse files Browse the repository at this point in the history
…r-not-allowing-imports-from-models-subpackages

[DEV-9930] Add a Flake8 rule for not allowing imports from models subpackages
  • Loading branch information
tomharel authored Jan 18, 2023
2 parents 1de18c7 + 6f20346 commit c5cbab3
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,6 @@ dmypy.json

# Pyre type checker
.pyre/

# idea files
.idea
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ repos:
language_version: python3.8


- repo: https://gitlab.com/pycqa/flake8
- repo: https://github.com/pycqa/flake8
rev: 4.0.1
hooks:
- id: flake8
4 changes: 4 additions & 0 deletions flake8_routable.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
ROU105 = "ROU105 Constants are not in order"
ROU106 = "ROU106 Relative imports are not allowed"
ROU107 = "ROU107 Inline function import is not at top of statement"
ROU108 = "ROU108 Import from model module instead of sub-packages"


@dataclass
Expand Down Expand Up @@ -153,6 +154,9 @@ def visit_ImportFrom(self, node: ast.ImportFrom) -> None:
if node.level > 0:
self.errors.append((node.lineno, node.col_offset, ROU106))

if node.module is not None and ".models." in node.module:
self.errors.append((node.lineno, node.col_offset, ROU108))

def visit_Set(self, node: ast.Set) -> None:
if not self._is_ordered(node.elts):
self.errors.append((node.lineno, node.col_offset, ROU103))
Expand Down
18 changes: 18 additions & 0 deletions tests/test_flake8_routable.py
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,24 @@ def test_upper_imports(self, upper_import):
assert error == set()


class TestROU108:
def test_non_model_subpackage_import(self):
error = results("from app.constants.subpackage import ModelA, ModelB")
assert error == set()

def test_non_model_named_like_a_model_subpackage_import(self):
error = results("from app.like_a_model.subpackage import ModelA, ModelB")
assert error == set()

def test_model_subpackage_import(self):
error = results("from app.models.subpackage import ModelA, ModelB")
assert error == {"1:0: ROU108 Import from model module instead of sub-packages"}

def test_model_module_import(self):
error = results("from app.models import Model")
assert error == set()


class TestVisitor:
def test_parse_to_string_warning(self):
visitor = Visitor()
Expand Down

0 comments on commit c5cbab3

Please sign in to comment.