Skip to content

adopt flake8-tidy-imports to enforce O.1.5 #223

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
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
11 changes: 7 additions & 4 deletions docs/Coding-Conventions.md
Original file line number Diff line number Diff line change
Expand Up @@ -802,20 +802,23 @@ from collections import defaultdict
from contextlib import contextmanager
```

### [O.1.5] ✔️ **DO** Use absolute imports
### [O.1.5] ✔️ **DO** Use absolute imports 💻

> 🐍 This rule stems from [PEP 8](https://www.python.org/dev/peps/pep-0008)

> 💻 This rule is enforced by error code I252

ℹ️ An exception can be made for `__init__.py` files republishing child module declarations

```python
# Bad
# Bad - will produce I252
from . import sibling
from .sibling import rivalry
```

```python
# Good
import my_app.relationships.parents
from my_app.relationships.sibling import rivalry
```

Expand Down Expand Up @@ -869,8 +872,8 @@ import os # Assuming os is never used
```

```python
# Good - assuming we are in a __init__.py file
from .mysubmodule import spam, eggs # OK even if neither are used in this module
# Good - assuming we are in a monty/__init__.py file
from monty.mysubmodule import spam, eggs # OK even if neither are used in this module
```


Expand Down
8 changes: 4 additions & 4 deletions ni_python_styleguide/_utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from . import code_analysis # noqa: F401
from . import lint # noqa: F401
from . import string_helpers # noqa: F401
from . import temp_file # noqa: F401
from ni_python_styleguide._utils import code_analysis # noqa: F401
from ni_python_styleguide._utils import lint # noqa: F401
from ni_python_styleguide._utils import string_helpers # noqa: F401
from ni_python_styleguide._utils import temp_file # noqa: F401

DEFAULT_ENCODING = "UTF-8"
2 changes: 2 additions & 0 deletions ni_python_styleguide/config.ini
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,5 @@ docstring-convention=all

# flake8-import-order
import-order-style=smarkets

ban-relative-imports = True
30 changes: 29 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ isort = ">=5.10"
flake8-black = ">=0.2.1"
flake8-docstrings = ">=1.5.0"
flake8-import-order = ">=0.18.1"
flake8-tidy-imports = [
{version = ">=4.4.1", python = ">=3.7,<3.9"},
{version=">=4.11.0", python="^3.9"},
]
pep8-naming = ">=0.11.1"

# Rejected flake8 plugins should be listed here (in alphabetical order)
Expand Down
4 changes: 3 additions & 1 deletion tests/test_convention_doc/test_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,6 @@ def test_rule_documents_enforcement_codes(rule):
if rule.is_automatically_enforced:
assert rule.error_codes is not None
else:
assert rule.error_codes is None
assert (
rule.error_codes is None
), f"Rule ({rule.identifier}) is not marked as automatically enforced, but lists an error code {rule.error_codes}"
Loading