Skip to content
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

Issue/1737/dont add comment #1749

Merged
merged 4 commits into from Jun 17, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions CHANGELOG.md
Expand Up @@ -5,14 +5,16 @@ NOTE: isort follows the [semver](https://semver.org/) versioning standard.
Find out more about isort's release policy [here](https://pycqa.github.io/isort/docs/major_releases/release_policy/).

### 5.9.0 TBD
- Implemented #1697: Provisional support for PEP 582: skip `__pypackages__` directories by default.
- Implemented #1705: More intuitive handling of isort:skip_file comments on streams.
- Implemented #1737: Support for using action comments to avoid adding imports to individual files.
- Fixed (https://github.com/PyCQA/isort/pull/1695): added imports being added to doc string in some cases.
- Fixed (https://github.com/PyCQA/isort/pull/1714): in rare cases line continuation combined with tabs can output invalid code.
- Fixed (https://github.com/PyCQA/isort/pull/1726): isort ignores reverse_sort when force_sort_within_sections is true.
- Fixed #1741: comments in hanging indent modes can lead to invalid code.
- Fixed #1744: repeat noqa comments dropped when * import and non * imports exist from the same package.
- Fixed #1721: repeat noqa comments on separate from lines with force-single-line set, sometimes get dropped.
- Implemented #1697: Provisional support for PEP 582: skip `__pypackages__` directories by default.
- Implemented #1705: More intuitive handling of isort:skip_file comments on streams.


### 5.8.0 March 20th 2021
- Fixed #1631: as import comments can in some cases be duplicated.
Expand Down
9 changes: 9 additions & 0 deletions docs/configuration/action_comments.md
Expand Up @@ -106,3 +106,12 @@ import a

!!! tip
isort split is exactly the same as placing an `# isort: on` immediately below an `# isort: off`


## isort: dont-add-imports

Tells isort to not automatically add imports to this file, even if --add-imports is set.

## isort: dont-add-import: [IMPORT_LINE]

Tells isort to not automatically add a particular import, even if --add-imports says to add it.
16 changes: 14 additions & 2 deletions isort/core.py
Expand Up @@ -156,8 +156,20 @@ def process(
isort_off = True
skip_file = True

if not in_quote and stripped_line == "# isort: off":
isort_off = True
if not in_quote:
if stripped_line == "# isort: off":
isort_off = True
elif stripped_line.startswith("# isort: dont-add-imports"):
add_imports = []
elif stripped_line.startswith("# isort: dont-add-import:"):
import_not_to_add = stripped_line.split("# isort: dont-add-import:", 1)[
1
].strip()
add_imports = [
import_to_add
for import_to_add in add_imports
if not import_to_add == import_not_to_add
]

if (
(index == 0 or (index in (1, 2) and not contains_imports))
Expand Down
120 changes: 118 additions & 2 deletions poetry.lock

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

3 changes: 2 additions & 1 deletion pyproject.toml
Expand Up @@ -39,7 +39,7 @@ include = [
]

[tool.poetry.dependencies]
python = "^3.6"
python = ">=3.6.1,<4.0"
pipreqs = {version = "*", optional = true}
requirementslib = {version = "*", optional = true}
pip-api = {version = "*", optional = true}
Expand Down Expand Up @@ -90,6 +90,7 @@ py = "^1.10.0"
toml = "^0.10.2"
pytest-benchmark = "^3.4.1"
types-pkg-resources = "^0.1.2"
pre-commit = "^2.13.0"

[tool.poetry.scripts]
isort = "isort.main:main"
Expand Down
50 changes: 50 additions & 0 deletions tests/unit/test_ticketed_features.py
Expand Up @@ -1115,3 +1115,53 @@ def test_isort_can_combine_reverse_sort_with_force_sort_within_sections_issue_17
import bla
"""
)


def test_isort_can_turn_off_import_adds_with_action_comment_issue_1737():
assert (
isort.code(
"""
import os
""",
add_imports=[
"from __future__ import absolute_imports",
"from __future__ import annotations",
],
)
== """
from __future__ import absolute_imports, annotations

import os
"""
)

assert isort.check_code(
"""
# isort: dont-add-imports
import os
""",
show_diff=True,
add_imports=[
"from __future__ import absolute_imports",
"from __future__ import annotations",
],
)

assert (
isort.code(
"""
# isort: dont-add-import: from __future__ import annotations
import os
""",
add_imports=[
"from __future__ import absolute_imports",
"from __future__ import annotations",
],
)
== """
# isort: dont-add-import: from __future__ import annotations
from __future__ import absolute_imports

import os
"""
)