From e3f924a13ab0db4ac15f45c6b94d9069ee3638aa Mon Sep 17 00:00:00 2001 From: Timothy Crosley Date: Mon, 20 Jul 2020 21:29:07 -0700 Subject: [PATCH] resolves #1331: Warns when sections don't match up. --- CHANGELOG.md | 1 + isort/settings.py | 20 +++++++++++++++++- tests/test_ticketed_features.py | 37 +++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0b6232d93..7b6eba858 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/isort/settings.py b/isort/settings.py index 68f32a662..46c6002a1 100644 --- a/isort/settings.py +++ b/isort/settings.py @@ -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) @@ -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"]) diff --git a/tests/test_ticketed_features.py b/tests/test_ticketed_features.py index 917fe46d6..ae9fec59d 100644 --- a/tests/test_ticketed_features.py +++ b/tests/test_ticketed_features.py @@ -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(): @@ -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" + )