Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle errors when default section(s) forgotten #1572

Merged
merged 3 commits into from
Oct 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion docs/configuration/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -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')`
Expand Down
34 changes: 27 additions & 7 deletions isort/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]:
Expand Down