Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Semantic versioning in our case means:

### Features

- Allows `__init__.py` files that consist only of imports, #3486
- Adds `--max-conditions` option, #3493

### Misc
Expand Down
6 changes: 4 additions & 2 deletions tests/test_visitors/test_ast/test_modules/test_empty_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from two import two_func
"""

module_with_multiple_statements = 'x = 1\ny = 2'
module_with_logic = """
try:
import some_module
Expand All @@ -40,6 +41,8 @@
empty_module,
module_with_docstring,
module_with_comments,
module_with_imports,
module_with_one_import,
],
)
def test_init_without_logic(
Expand All @@ -64,9 +67,8 @@ def test_init_without_logic(
@pytest.mark.parametrize(
'code',
[
module_with_imports,
module_with_one_import,
module_with_logic,
module_with_multiple_statements,
],
)
def test_init_with_logic(
Expand Down
1 change: 1 addition & 0 deletions wemake_python_styleguide/violations/best_practices.py
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,7 @@ class InitModuleHasLogicViolation(SimpleViolation):

1. comments, since they are dropped before AST comes in play
2. docstrings are used sometimes when required to state something
3. imports and re-exports to define a public module API

It is also fine when you have different users that use your code.
And you do not want to break everything for them.
Expand Down
7 changes: 7 additions & 0 deletions wemake_python_styleguide/visitors/ast/modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ def _check_init_contents(self, node: ast.Module) -> None:
if not self._is_init() or not node.body:
return

only_imports = all(
isinstance(statement, (ast.Import, ast.ImportFrom))
for statement in node.body
)
if only_imports:
return

if len(node.body) > 1:
self.add_violation(InitModuleHasLogicViolation())
return
Expand Down