Skip to content

Commit

Permalink
Improve reporting of known errors in isort, reachieve 100% test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
timothycrosley committed Dec 7, 2020
1 parent f32cf77 commit 0566b4e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 17 deletions.
17 changes: 13 additions & 4 deletions isort/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,18 @@ def __init__(self, unsupported_settings: Dict[str, Dict[str, str]]):
class UnsupportedEncoding(ISortError):
"""Raised when isort encounters an encoding error while trying to read a file"""

def __init__(
self,
filename: Union[str, Path],
):
def __init__(self, filename: Union[str, Path]):
super().__init__(f"Unknown or unsupported encoding in {filename}")
self.filename = filename


class MissingSection(ISortError):
"""Raised when isort encounters an import that matches a section that is not defined"""

def __init__(self, import_module: str, section: str):
super().__init__(
f"Found {import_module} import while parsing, but {section} 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."
)
17 changes: 4 additions & 13 deletions isort/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
from warnings import warn

from . import __version__, api, sections
from .exceptions import FileSkipped, UnsupportedEncoding
from .exceptions import FileSkipped, ISortError, UnsupportedEncoding
from .format import create_terminal_printer
from .logo import ASCII_ART
from .profiles import profiles
from .settings import DEFAULT_CONFIG, VALID_PY_TARGETS, Config, WrapModes
from .settings import VALID_PY_TARGETS, Config, WrapModes

try:
from .setuptools_commands import ISortCommand # noqa: F401
Expand Down Expand Up @@ -110,17 +110,8 @@ 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)
except ISortError as error:
_print_hard_fail(config, message=str(error))
sys.exit(os.EX_CONFIG)
except Exception:
_print_hard_fail(config, offending_file=file_name)
Expand Down
5 changes: 5 additions & 0 deletions isort/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from . import place
from .comments import parse as parse_comments
from .deprecated.finders import FindersManager
from .exceptions import MissingSection
from .settings import DEFAULT_CONFIG, Config

if TYPE_CHECKING:
Expand Down Expand Up @@ -524,6 +525,10 @@ def file_contents(contents: str, config: Config = DEFAULT_CONFIG) -> ParsedConte
" Do you need to define a default section?"
)
imports.setdefault("", {"straight": OrderedDict(), "from": OrderedDict()})

if placed_module and placed_module not in imports:
raise MissingSection(import_module=module, section=placed_module)

straight_import |= imports[placed_module][type_of_import].get( # type: ignore
module, False
)
Expand Down

0 comments on commit 0566b4e

Please sign in to comment.