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')` diff --git a/isort/main.py b/isort/main.py index fa93256c3..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,16 +109,36 @@ def sort_imports( if config.verbose: 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" + 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 +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]: