Skip to content

Commit

Permalink
Fixed #1284: Regression when sorting files from CLI using black profile.
Browse files Browse the repository at this point in the history
  • Loading branch information
timothycrosley committed Jul 7, 2020
1 parent 0d1526d commit b228c37
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 15 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Changelog
NOTE: isort follows the [semver](https://semver.org/) versioning standard.
### 5.0.5 July 6, 2020
- Fixed #1285: packaging issue with bundling tests via poetry.

- Fixed #1284: Regression when sorting `.pyi` files from CLI using black profile.

### 5.0.4 July 6, 2020
- Fixed #1264: a regression with comment handling and `force_sort_within_sections` config option
Expand Down
34 changes: 21 additions & 13 deletions isort/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

def sort_code_string(
code: str,
extension: str = "py",
extension: Optional[str] = None,
config: Config = DEFAULT_CONFIG,
file_path: Optional[Path] = None,
disregard_skip: bool = False,
Expand All @@ -41,7 +41,7 @@ def sort_code_string(
"""Sorts any imports within the provided code string, returning a new string with them sorted.
- **code**: The string of code with imports that need to be sorted.
- **extension**: The file extension that contains the code.
- **extension**: The file extension that contains imports. Defaults to filename extension or py.
- **config**: The config object to use when sorting imports.
- **file_path**: The disk location where the code string was pulled from.
- **disregard_skip**: set to `True` if you want to ignore a skip set in config for this file.
Expand All @@ -65,7 +65,7 @@ def sort_code_string(
def check_code_string(
code: str,
show_diff: bool = False,
extension: str = "py",
extension: Optional[str] = None,
config: Config = DEFAULT_CONFIG,
file_path: Optional[Path] = None,
disregard_skip: bool = False,
Expand All @@ -76,7 +76,7 @@ def check_code_string(
- **code**: The string of code with imports that need to be sorted.
- **show_diff**: If `True` the changes that need to be done will be printed to stdout.
- **extension**: The file extension that contains the code.
- **extension**: The file extension that contains imports. Defaults to filename extension or py.
- **config**: The config object to use when sorting imports.
- **file_path**: The disk location where the code string was pulled from.
- **disregard_skip**: set to `True` if you want to ignore a skip set in config for this file.
Expand All @@ -96,7 +96,7 @@ def check_code_string(
def sort_stream(
input_stream: TextIO,
output_stream: TextIO,
extension: str = "py",
extension: Optional[str] = None,
config: Config = DEFAULT_CONFIG,
file_path: Optional[Path] = None,
disregard_skip: bool = False,
Expand All @@ -107,7 +107,7 @@ def sort_stream(
- **input_stream**: The stream of code with imports that need to be sorted.
- **output_stream**: The stream where sorted imports should be written to.
- **extension**: The file extension that contains the code.
- **extension**: The file extension that contains imports. Defaults to filename extension or py.
- **config**: The config object to use when sorting imports.
- **file_path**: The disk location where the code string was pulled from.
- **disregard_skip**: set to `True` if you want to ignore a skip set in config for this file.
Expand All @@ -133,7 +133,12 @@ def sort_stream(
_internal_output = StringIO()

try:
changed = _sort_imports(input_stream, _internal_output, extension=extension, config=config)
changed = _sort_imports(
input_stream,
_internal_output,
extension=extension or (file_path and file_path.suffix.lstrip(".")) or "py",
config=config,
)
except FileSkipComment:
raise FileSkipComment(content_source)

Expand All @@ -153,7 +158,7 @@ def sort_stream(
def check_stream(
input_stream: TextIO,
show_diff: bool = False,
extension: str = "py",
extension: Optional[str] = None,
config: Config = DEFAULT_CONFIG,
file_path: Optional[Path] = None,
disregard_skip: bool = False,
Expand All @@ -164,7 +169,7 @@ def check_stream(
- **input_stream**: The stream of code with imports that need to be sorted.
- **show_diff**: If `True` the changes that need to be done will be printed to stdout.
- **extension**: The file extension that contains the code.
- **extension**: The file extension that contains imports. Defaults to filename extension or py.
- **config**: The config object to use when sorting imports.
- **file_path**: The disk location where the code string was pulled from.
- **disregard_skip**: set to `True` if you want to ignore a skip set in config for this file.
Expand Down Expand Up @@ -213,24 +218,25 @@ def check_file(
config: Config = DEFAULT_CONFIG,
file_path: Optional[Path] = None,
disregard_skip: bool = True,
extension: Optional[str] = None,
**config_kwargs,
) -> bool:
"""Checks any imports within the provided file, returning `False` if any unsorted or
incorrectly imports are found or `True` if no problems are identified.
- **filename**: The name or Path of the file to check.
- **show_diff**: If `True` the changes that need to be done will be printed to stdout.
- **extension**: The file extension that contains the code.
- **config**: The config object to use when sorting imports.
- **file_path**: The disk location where the code string was pulled from.
- **disregard_skip**: set to `True` if you want to ignore a skip set in config for this file.
- **extension**: The file extension that contains imports. Defaults to filename extension or py.
- ****config_kwargs**: Any config modifications.
"""
with io.File.read(filename) as source_file:
return check_stream(
source_file.stream,
show_diff=show_diff,
extension=source_file.extension or "py",
extension=extension,
config=config,
file_path=file_path or source_file.path,
disregard_skip=disregard_skip,
Expand All @@ -240,7 +246,7 @@ def check_file(

def sort_file(
filename: Union[str, Path],
extension: str = "py",
extension: Optional[str] = None,
config: Config = DEFAULT_CONFIG,
file_path: Optional[Path] = None,
disregard_skip: bool = True,
Expand All @@ -252,7 +258,7 @@ def sort_file(
"""Sorts and formats any groups of imports imports within the provided file or Path.
- **filename**: The name or Path of the file to format.
- **extension**: The file extension that contains the code.
- **extension**: The file extension that contains imports. Defaults to filename extension or py.
- **config**: The config object to use when sorting imports.
- **file_path**: The disk location where the code string was pulled from.
- **disregard_skip**: set to `True` if you want to ignore a skip set in config for this file.
Expand All @@ -271,6 +277,7 @@ def sort_file(
config=config,
file_path=file_path or source_file.path,
disregard_skip=disregard_skip,
extension=extension,
**config_kwargs,
)
else:
Expand All @@ -286,6 +293,7 @@ def sort_file(
config=config,
file_path=file_path or source_file.path,
disregard_skip=disregard_skip,
extension=extension,
**config_kwargs,
)
if changed:
Expand Down
4 changes: 3 additions & 1 deletion tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ def test_read(self, tmpdir):
def test_from_content(self, tmpdir):
test_file = tmpdir.join("file.py")
test_file.write_text("import os", "utf8")
assert io.File.from_contents("import os", filename=str(test_file))
file_obj = io.File.from_contents("import os", filename=str(test_file))
assert file_obj
assert file_obj.extension == "py"

def test_open(self, tmpdir):
with pytest.raises(Exception):
Expand Down
7 changes: 7 additions & 0 deletions tests/test_isort.py
Original file line number Diff line number Diff line change
Expand Up @@ -4112,6 +4112,13 @@ def test_pyi_formatting_issue_942(tmpdir) -> None:
== expected_pyi_output
)

# Ensure it works for direct file API as well (see: issue #1284)
source_pyi = tmpdir.join("source.pyi")
source_pyi.write(test_input)
api.sort_file(Path(source_pyi))

assert source_pyi.read().splitlines() == expected_pyi_output


def test_move_class_issue_751() -> None:
test_input = (
Expand Down

0 comments on commit b228c37

Please sign in to comment.