Skip to content

Commit

Permalink
Merge pull request #1572 from jaydesl/develop
Browse files Browse the repository at this point in the history
Handle errors when default section(s) forgotten
  • Loading branch information
timothycrosley authored Oct 27, 2020
2 parents 42ef409 + 1bb2576 commit 7fb8567
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
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

0 comments on commit 7fb8567

Please sign in to comment.