# Buggy code
def sort_imports(
    file_name: str,
    config: Config,
    check: bool = False,
    ask_to_apply: bool = False,
    write_to_stdout: bool = False,
    **kwargs: Any,
) -> Optional[SortAttempt]:
    incorrectly_sorted: bool = False
    skipped: bool = False
    try:
        if check:
            try:
                incorrectly_sorted = not api.check_file(
                    file_name, config=config, **kwargs
                )
            except FileSkipped:
                skipped = True
            return SortAttempt(incorrectly_sorted, skipped, True)

        try:
            incorrectly_sorted = not api.sort_file(
                file_name,
                config=config,
                ask_to_apply=ask_to_apply,
                write_to_stdout=write_to_stdout,
                **kwargs,
            )
        except FileSkipped:
            skipped = True
        return SortAttempt(incorrectly_sorted, skipped, True)
    except (OSError, ValueError) as error:
        warn(f"Unable to parse file {file_name} due to {error}")
        return None
    except UnsupportedEncoding:
        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"
        )
        raise


# Traceback (KeyError)
ERROR: Unrecoverable exception thrown when parsing src/eduid_webapp/eidas/app.py! This should NEVER happen.
If encountered, please open an issue: https://github.com/PyCQA/isort/issues/new
Traceback (most recent call last):
File "/home/lundberg/python-environments/eduid-webapp/bin/isort", line 8, in <module>
sys.exit(main())
File "/home/lundberg/python-environments/eduid-webapp/lib/python3.8/site-packages/isort/main.py", line 953, in main
for sort_attempt in attempt_iterator:
File "/home/lundberg/python-environments/eduid-webapp/lib/python3.8/site-packages/isort/main.py", line 939, in <genexpr>
sort_imports(  # type: ignore
File "/home/lundberg/python-environments/eduid-webapp/lib/python3.8/site-packages/isort/main.py", line 95, in sort_imports
incorrectly_sorted = not api.sort_file(
File "/home/lundberg/python-environments/eduid-webapp/lib/python3.8/site-packages/isort/api.py", line 324, in sort_file
changed = sort_stream(
File "/home/lundberg/python-environments/eduid-webapp/lib/python3.8/site-packages/isort/api.py", line 158, in sort_stream
changed = core.process(
File "/home/lundberg/python-environments/eduid-webapp/lib/python3.8/site-packages/isort/core.py", line 332, in process
parsed_content = parse.file_contents(import_section, config=config)
File "/home/lundberg/python-environments/eduid-webapp/lib/python3.8/site-packages/isort/parse.py", line 432, in file_contents
root = imports[placed_module][type_of_import]  # type: ignore
KeyError: 'LOCALFOLDER'

# Fixed code
def sort_imports(
    file_name: str,
    config: Config,
    check: bool = False,
    ask_to_apply: bool = False,
    write_to_stdout: bool = False,
    **kwargs: Any,
) -> Optional[SortAttempt]:
    incorrectly_sorted: bool = False
    skipped: bool = False
    try:
        if check:
            try:
                incorrectly_sorted = not api.check_file(
                    file_name, config=config, **kwargs
                )
            except FileSkipped:
                skipped = True
            return SortAttempt(incorrectly_sorted, skipped, True)

        try:
            incorrectly_sorted = not api.sort_file(
                file_name,
                config=config,
                ask_to_apply=ask_to_apply,
                write_to_stdout=write_to_stdout,
                **kwargs,
            )
        except FileSkipped:
            skipped = True
        return SortAttempt(incorrectly_sorted, skipped, True)
    except (OSError, ValueError) as error:
        warn(f"Unable to parse file {file_name} due to {error}")
        return None
    except UnsupportedEncoding:
        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