From bacd6c7b9f390dbe02e65bdc5a2c1670197f05cf Mon Sep 17 00:00:00 2001 From: jaydesl Date: Sun, 25 Oct 2020 11:42:39 +0000 Subject: [PATCH 1/3] Refactor error printer --- isort/main.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/isort/main.py b/isort/main.py index fa93256c3..84569441f 100644 --- a/isort/main.py +++ b/isort/main.py @@ -110,15 +110,23 @@ def sort_imports( warn(f"Encoding not supported for {file_name}") return SortAttempt(incorrectly_sorted, skipped, False) except Exception: - printer = create_terminal_printer(color=config.color_output) - printer.error( - f"Unrecoverable exception thrown when parsing {file_name}! " - "This should NEVER happen.\n" - "If encountered, please open an issue: https://github.com/PyCQA/isort/issues/new" - ) + _print_hard_fail(config, offending_file=file_name) raise +def _print_hard_fail( + config: Config, offending_file: Optional[str] = None, message: Optional[str] = None +) -> None: + """Fail on unrecoverable exception with custom message.""" + message = message or ( + f"Unrecoverable exception thrown when parsing {offending_file or ''}!" + "This should NEVER happen.\n" + "If encountered, please open an issue: https://github.com/PyCQA/isort/issues/new" + ) + printer = create_terminal_printer(color=config.color_output) + printer.error(message) + + def iter_source_code( paths: Iterable[str], config: Config, skipped: List[str], broken: List[str] ) -> Iterator[str]: From 435d2cd3470c24057ea0519140a000a09be4a526 Mon Sep 17 00:00:00 2001 From: jaydesl Date: Sun, 25 Oct 2020 11:43:16 +0000 Subject: [PATCH 2/3] Gracefully fail on missing default sections --- isort/main.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/isort/main.py b/isort/main.py index 84569441f..8ac59a709 100644 --- a/isort/main.py +++ b/isort/main.py @@ -14,7 +14,7 @@ from .format import create_terminal_printer from .logo import ASCII_ART from .profiles import profiles -from .settings import VALID_PY_TARGETS, Config, WrapModes +from .settings import DEFAULT_CONFIG, VALID_PY_TARGETS, Config, WrapModes try: from .setuptools_commands import ISortCommand # noqa: F401 @@ -109,6 +109,18 @@ def sort_imports( if config.verbose: warn(f"Encoding not supported for {file_name}") return SortAttempt(incorrectly_sorted, skipped, False) + except KeyError as error: + if error.args[0] not in DEFAULT_CONFIG.sections: + _print_hard_fail(config, offending_file=file_name) + raise + msg = ( + f"Found {error} imports while parsing, but {error} was not included " + "in the `sections` setting of your config. Please add it before continuing\n" + "See https://pycqa.github.io/isort/#custom-sections-and-ordering " + "for more info." + ) + _print_hard_fail(config, message=msg) + sys.exit(os.EX_CONFIG) except Exception: _print_hard_fail(config, offending_file=file_name) raise From 1bb257663caefc347b48ab3c2b2bccb2e0c42e50 Mon Sep 17 00:00:00 2001 From: jaydesl Date: Sun, 25 Oct 2020 11:43:30 +0000 Subject: [PATCH 3/3] Improve docs around custom section ordering --- README.md | 2 +- docs/configuration/options.md | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index c2c6a8244..f55c15883 100644 --- a/README.md +++ b/README.md @@ -348,7 +348,7 @@ of: FUTURE,STDLIB,THIRDPARTY,FIRSTPARTY,LOCALFOLDER ``` -to your preference: +to your preference (if defined, omitting a default section may cause errors): ```ini sections=FUTURE,STDLIB,FIRSTPARTY,THIRDPARTY,LOCALFOLDER diff --git a/docs/configuration/options.md b/docs/configuration/options.md index bf2205243..de70eda48 100644 --- a/docs/configuration/options.md +++ b/docs/configuration/options.md @@ -108,7 +108,9 @@ Forces line endings to the specified value. If not set, values will be guessed p ## Sections -**No Description** +Specifies a custom ordering for sections. Any custom defined sections should also be +included in this ordering. Omitting any of the default sections from this tuple may +result in unexpected sorting or an exception being raised. **Type:** Tuple **Default:** `('FUTURE', 'STDLIB', 'THIRDPARTY', 'FIRSTPARTY', 'LOCALFOLDER')`