Skip to content

Commit

Permalink
- Fixed #1301: Import headings in nested sections leads to check errors
Browse files Browse the repository at this point in the history
  • Loading branch information
timothycrosley committed Jul 11, 2020
1 parent d744e7f commit 7769781
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 11 deletions.
1 change: 1 addition & 0 deletions isort/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
from .api import sort_code_string as code
from .api import sort_file as file
from .api import sort_stream as stream
from .settings import Config
16 changes: 12 additions & 4 deletions isort/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,18 @@
from warnings import warn

from . import io, output, parse
from .exceptions import (ExistingSyntaxErrors, FileSkipComment,
FileSkipSetting, IntroducedSyntaxErrors)
from .format import (ask_whether_to_apply_changes_to_file, format_natural,
remove_whitespace, show_unified_diff)
from .exceptions import (
ExistingSyntaxErrors,
FileSkipComment,
FileSkipSetting,
IntroducedSyntaxErrors,
)
from .format import (
ask_whether_to_apply_changes_to_file,
format_natural,
remove_whitespace,
show_unified_diff,
)
from .io import Empty
from .place import module as place_module # skipcq: PYL-W0611 (intended export of public API)
from .place import module_with_reason as place_module_with_reason # skipcq: PYL-W0611 (^)
Expand Down
10 changes: 5 additions & 5 deletions isort/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
usage:
exit_code = git_hook(strict=True|False, modify=True|False)
"""
import os
import subprocess # nosec - Needed for hook
from pathlib import Path
from typing import List

from isort import api
from isort import Config, api


def get_output(command: List[str]) -> str:
Expand Down Expand Up @@ -51,17 +52,16 @@ def git_hook(strict: bool = False, modify: bool = False) -> int:
files_modified = get_lines(diff_cmd)

errors = 0
config = Config(settings_path=os.path.dirname(os.path.abspath(files_modified[0])))
for filename in files_modified:
if filename.endswith(".py"):
# Get the staged contents of the file
staged_cmd = ["git", "show", f":{filename}"]
staged_contents = get_output(staged_cmd)

if not api.check_code_string(
staged_contents, file_path=Path(filename), settings_path=Path(filename).parent
):
if not api.check_code_string(staged_contents, file_path=Path(filename), config=config):
errors += 1
if modify:
api.sort_file(filename, settings_path=Path(filename).parent)
api.sort_file(filename, config=config)

return errors if strict else 0
3 changes: 1 addition & 2 deletions isort/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
from distutils.util import strtobool as _as_bool
from functools import lru_cache
from pathlib import Path
from typing import (Any, Callable, Dict, FrozenSet, Iterable, List, Optional,
Pattern, Set, Tuple)
from typing import Any, Callable, Dict, FrozenSet, Iterable, List, Optional, Pattern, Set, Tuple
from warnings import warn

from . import stdlibs
Expand Down

0 comments on commit 7769781

Please sign in to comment.