Skip to content

Commit 01b9d77

Browse files
authored
adopt flake8-tidy-imports to enforce O.1.5 (#223)
* make it show which rule is failing more clearly * add tool and update config and convention docs * add tool to lock file * add example import that doesn't use from * update own code to match * format * unformat * format * remove the exception note * add another ok example * try disabling mac testing for now * try byte comparison * ok, let's use an rstrip
1 parent 358e535 commit 01b9d77

File tree

10 files changed

+54
-17
lines changed

10 files changed

+54
-17
lines changed

docs/Coding-Conventions.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -802,20 +802,21 @@ from collections import defaultdict
802802
from contextlib import contextmanager
803803
```
804804

805-
### [O.1.5] ✔️ **DO** Use absolute imports
805+
### [O.1.5] ✔️ **DO** Use absolute imports 💻
806806

807807
> 🐍 This rule stems from [PEP 8](https://www.python.org/dev/peps/pep-0008)
808808
809-
ℹ️ An exception can be made for `__init__.py` files republishing child module declarations
809+
> 💻 This rule is enforced by error code I252
810810
811811
```python
812-
# Bad
812+
# Bad - will produce I252
813813
from . import sibling
814814
from .sibling import rivalry
815815
```
816816

817817
```python
818818
# Good
819+
import my_app.relationships.parents
819820
from my_app.relationships.sibling import rivalry
820821
```
821822

@@ -869,8 +870,9 @@ import os # Assuming os is never used
869870
```
870871

871872
```python
872-
# Good - assuming we are in a __init__.py file
873-
from .mysubmodule import spam, eggs # OK even if neither are used in this module
873+
# Good - assuming we are in a monty/__init__.py file
874+
from monty import sandwich_shop
875+
from monty.mysubmodule import spam, eggs # OK even if neither are used in this module
874876
```
875877

876878

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
from . import code_analysis # noqa: F401
2-
from . import lint # noqa: F401
3-
from . import string_helpers # noqa: F401
4-
from . import temp_file # noqa: F401
1+
from ni_python_styleguide._utils import code_analysis # noqa: F401
2+
from ni_python_styleguide._utils import lint # noqa: F401
3+
from ni_python_styleguide._utils import string_helpers # noqa: F401
4+
from ni_python_styleguide._utils import temp_file # noqa: F401
55

66
DEFAULT_ENCODING = "UTF-8"

ni_python_styleguide/config.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,3 +129,5 @@ docstring-convention=all
129129

130130
# flake8-import-order
131131
import-order-style=smarkets
132+
133+
ban-relative-imports = True

poetry.lock

Lines changed: 29 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ isort = ">=5.10"
3838
flake8-black = ">=0.2.1"
3939
flake8-docstrings = ">=1.5.0"
4040
flake8-import-order = ">=0.18.1"
41+
flake8-tidy-imports = [
42+
{version = ">=4.4.1", python = ">=3.7,<3.9"},
43+
{version=">=4.11.0", python="^3.9"},
44+
]
4145
pep8-naming = ">=0.11.1"
4246

4347
# Rejected flake8 plugins should be listed here (in alphabetical order)
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +0,0 @@
1-

tests/test_cli/format_test_cases__snapshots/file_with_hanging_whitespace/output.diff

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,4 @@
1313

1414
-x = 5
1515
? ^
16-
+x = 5
17-
16+
+x = 5

tests/test_cli/format_test_cases__snapshots/file_with_malsorted_imports/output.diff

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,4 @@
1616
+
1717

1818
@click.command()
19-
def _main():
20-
19+
def _main():

tests/test_cli/test_format.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def test_given_input_file__produces_expected_output_simple(
2424
output = styleguide_command(command="format")
2525

2626
assert output.exit_code in (True, 0), f"Error in running:\n{output}"
27-
result = test_file.read_text(encoding="UTF-8")
27+
result = test_file.read_text()
2828
snapshot.snapshot_dir = test_dir
2929
snapshot.assert_match(result, "output.py")
3030

@@ -44,6 +44,8 @@ def test_given_input_file__produces_expected_output_diff(
4444

4545
assert output.exception is None
4646
result = output.stdout.replace(str(test_file), in_file.relative_to(TEST_CASE_DIR).as_posix())
47+
# to make line endings on different platforms consistent
48+
result = result.rstrip()
4749
snapshot.snapshot_dir = test_dir
4850
snapshot.assert_match(result, "output.diff")
4951

tests/test_convention_doc/test_rules.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,6 @@ def test_rule_documents_enforcement_codes(rule):
5353
if rule.is_automatically_enforced:
5454
assert rule.error_codes is not None
5555
else:
56-
assert rule.error_codes is None
56+
assert (
57+
rule.error_codes is None
58+
), f"Rule ({rule.identifier}) is not marked as automatically enforced, but lists an error code {rule.error_codes}"

0 commit comments

Comments
 (0)