Skip to content

Commit

Permalink
Resolve #1367: Improve error handling for known import sections
Browse files Browse the repository at this point in the history
  • Loading branch information
timothycrosley committed Aug 4, 2020
1 parent 572f4ff commit 6eba709
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ NOTE: isort follows the [semver](https://semver.org/) versioning standard.
- Added experimental support for sorting literals (issue #1358)
- Improved handling of deprecated single line variables for usage with Visual Studio Code (issue #1363)
- Improved handling of mixed newline forms within same source file.
- Improved error handling for known sections.

Internal Development:
- Initial hypothesmith powered test to help catch unexpected syntax parsing and output errors (thanks @Zac-HD!)
Expand Down
35 changes: 25 additions & 10 deletions isort/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ def __init__(

known_other = {}
import_headings = {}
for key, value in combined_config.items():
for key, value in tuple(combined_config.items()):
# Collect all known sections beyond those that have direct entries
if key.startswith(KNOWN_PREFIX) and key not in (
"known_standard_library",
Expand All @@ -314,13 +314,30 @@ def __init__(
"known_local_folder",
):
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)}."
)
maps_to_section = import_heading.upper()
combined_config.pop(key)
if maps_to_section in KNOWN_SECTION_MAPPING:
section_name = f"known_{KNOWN_SECTION_MAPPING[maps_to_section].lower()}"
if section_name in combined_config:
warn(
f"Can't set both {key} and {section_name} in the same config file.\n"
f"Default to {section_name} if unsure."
"\n\n"
"See: https://timothycrosley.github.io/isort/"
"#custom-sections-and-ordering."
)
else:
combined_config[section_name] = frozenset(value)
else:
known_other[import_heading] = frozenset(value)
if maps_to_section not in combined_config.get("sections", ()):
warn(
f"`{key}` setting is defined, but {maps_to_section} is not"
" included in `sections` config option:"
f" {combined_config.get('sections', SECTION_DEFAULTS)}.\n\n"
"See: https://timothycrosley.github.io/isort/"
"#custom-sections-and-ordering."
)
if key.startswith(IMPORT_HEADING_PREFIX):
import_headings[key[len(IMPORT_HEADING_PREFIX) :].lower()] = str(value)

Expand Down Expand Up @@ -384,8 +401,6 @@ def __init__(
combined_config.pop(deprecated_option)

if known_other:
for known_key in known_other:
combined_config.pop(f"{KNOWN_PREFIX}{known_key}", None)
combined_config["known_other"] = known_other
if import_headings:
for import_heading_key in import_headings:
Expand Down
7 changes: 7 additions & 0 deletions tests/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ class TestConfig:
def test_init(self):
assert Config()

def test_known_settings(self):
assert Config(known_third_party=["one"]).known_third_party == frozenset({"one"})
assert Config(known_thirdparty=["two"]).known_third_party == frozenset({"two"})
assert Config(
known_third_party=["one"], known_thirdparty=["two"]
).known_third_party == frozenset({"one"})

def test_invalid_settings_path(self):
with pytest.raises(exceptions.InvalidSettingsPath):
Config(settings_path="this_couldnt_possibly_actually_exists/could_it")
Expand Down

0 comments on commit 6eba709

Please sign in to comment.