Skip to content

Commit

Permalink
resolves #1331: Warns when sections don't match up.
Browse files Browse the repository at this point in the history
  • Loading branch information
timothycrosley committed Jul 21, 2020
1 parent e2c48a1 commit e3f924a
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ NOTE: isort follows the [semver](https://semver.org/) versioning standard.

### 5.2.0 TBD
- Implemented #1335: Official API for diff capturing.
- Implemented #1331: Warn when sections don't match up.

### 5.1.4 July 19, 2020
- Fixed issue #1333: Use of wrap_length raises an exception about it not being lower or equal to line_length.
Expand Down
20 changes: 19 additions & 1 deletion isort/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,14 @@ def __init__(
"known_third_party",
"known_first_party",
):
known_other[key[len(KNOWN_PREFIX) :].lower()] = frozenset(value)
import_heading = key[len(KNOWN_PREFIX) :].lower()
known_other[import_heading] = frozenset(value)
if not import_heading.upper() in combined_config.get("sections", ()):
warn(
f"`{key}` setting is defined, but not {import_heading.upper} is not"
" included in `sections` config option:"
f" {combined_config.get('sections', SECTION_DEFAULTS)}."
)
if key.startswith(IMPORT_HEADING_PREFIX):
import_headings[key[len(IMPORT_HEADING_PREFIX) :].lower()] = str(value)

Expand All @@ -301,6 +308,17 @@ def __init__(

combined_config[key] = type(default_value)(value)

for section in combined_config.get("sections", ()):
if section in SECTION_DEFAULTS:
continue
elif not section.lower() in known_other:
config_keys = ", ".join(known_other.keys())
warn(
f"`sections` setting includes {section}, but no known_{section.lower()} "
"is defined. "
f"The following known_SECTION config options are defined: {config_keys}."
)

if "directory" not in combined_config:
combined_config["directory"] = (
os.path.dirname(config_settings["source"])
Expand Down
37 changes: 37 additions & 0 deletions tests/test_ticketed_features.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
"""
from io import StringIO

import pytest

import isort
from isort import Config


def test_semicolon_ignored_for_dynamic_lines_after_import_issue_1178():
Expand Down Expand Up @@ -161,3 +164,37 @@ def test_isort_provides_official_api_for_diff_output_issue_1335():
isort.code("import b\nimport a\n", show_diff=diff_output)
diff_output.seek(0)
assert "+import a" in diff_output.read()


def test_isort_warns_when_known_sections_dont_match_issue_1331():
"""Test to ensure that isort warns if there is a mismatch between sections and known_sections.
See: https://github.com/timothycrosley/isort/issues/1331.
"""
assert (
isort.place_module(
"bot_core",
config=Config(
known_robotlocomotion_upstream=["bot_core"],
sections=["ROBOTLOCOMOTION_UPSTREAM", "THIRDPARTY"],
),
)
== "ROBOTLOCOMOTION_UPSTREAM"
)
with pytest.warns(UserWarning):
assert (
isort.place_module(
"bot_core",
config=Config(
known_robotlocomotion_upstream=["bot_core"],
sections=["ROBOTLOOMOTION_UPSTREAM", "THIRDPARTY"],
),
)
== "THIRDPARTY"
)
with pytest.warns(UserWarning):
assert (
isort.place_module(
"bot_core", config=Config(known_robotlocomotion_upstream=["bot_core"])
)
== "THIRDPARTY"
)

0 comments on commit e3f924a

Please sign in to comment.