Skip to content

Commit

Permalink
Issue/942 (#946)
Browse files Browse the repository at this point in the history
* Add test cases for .pyi extension support, in particular from file based loading

* Implement native support for pyi files

* Bump version to prepare for immediate deploy
  • Loading branch information
timothycrosley committed May 13, 2019
1 parent ba8354c commit 55f1ae7
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 11 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ Internal:
Planned:
- profile support for common project types (black, django, google, etc)

### 4.3.19 - May 12, 2019 - hot fix release
- Fixed issue #942 - correctly handle pyi (Python Template Files) to match `black` output

### 4.3.18 - May 1, 2019 - hot fix release
- Fixed an issue with parsing files that contain unicode characters in Python 2
- Fixed issue #924 - Pulling in pip internals causes depreciation warning
Expand Down
2 changes: 1 addition & 1 deletion isort/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@
from . import settings # noqa: F401
from .compat import SortImports # noqa: F401

__version__ = "4.3.18"
__version__ = "4.3.19"
8 changes: 7 additions & 1 deletion isort/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,11 @@ def __init__(
ask_to_apply: bool = False,
run_path: str = '',
check_skip: bool = True,
extension: Optional[str] = None,
**setting_overrides: Any
):
file_path = None if file_path is None else Path(file_path)
file_name = None
settings_path = None if settings_path is None else Path(settings_path)

self.config = settings.prepare_config(get_settings_path(settings_path, file_path),
Expand Down Expand Up @@ -130,8 +132,12 @@ def __init__(
sys.stdout.write(file_contents)
return

if not extension:
extension = file_name.split('.')[-1] if file_name else "py"

self.sorted_imports = _SortImports(file_contents=file_contents,
config=self.config)
config=self.config,
extension=extension)
self.output = self.sorted_imports.output

if self.config['atomic']:
Expand Down
9 changes: 6 additions & 3 deletions isort/isort.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@


class _SortImports(object):
def __init__(self, file_contents: str, config: Dict[str, Any]) -> None:
def __init__(self, file_contents: str, config: Dict[str, Any], extension: str = "py") -> None:
self.config = config
self.extension = extension

self.place_imports = {} # type: Dict[str, List[str]]
self.import_placements = {} # type: Dict[str, str]
Expand Down Expand Up @@ -556,8 +557,10 @@ def by_module(line: str) -> str:

if self.config['lines_after_imports'] != -1:
self.out_lines[imports_tail:0] = ["" for line in range(self.config['lines_after_imports'])]
elif next_construct.startswith("def ") or next_construct.startswith("class ") or \
next_construct.startswith("@") or next_construct.startswith("async def"):
elif self.extension != "pyi" and (next_construct.startswith("def ") or
next_construct.startswith("class ") or
next_construct.startswith("@") or
next_construct.startswith("async def")):
self.out_lines[imports_tail:0] = ["", ""]
else:
self.out_lines[imports_tail:0] = [""]
Expand Down
4 changes: 4 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
pytest==4.4.1
ipython==4.1.2
appdirs
pipfile
pyproject
requirementslib
pipreqs
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
readme = f.read()

setup(name='isort',
version='4.3.18',
version='4.3.19',
description='A Python utility / library to sort Python imports.',
long_description=readme,
author='Timothy Crosley',
Expand Down
22 changes: 22 additions & 0 deletions test_isort.py
Original file line number Diff line number Diff line change
Expand Up @@ -3307,3 +3307,25 @@ def test_isort_keeps_comments_issue_691():
'def path(*subdirectories):\n'
' return os.path.join(PROJECT_DIR, *subdirectories)\n')
assert SortImports(file_contents=test_input).output == expected_output


def test_pyi_formatting_issue_942(tmpdir):
test_input = ('import os\n'
'\n'
'\n'
'def my_method():\n')
expected_py_output = test_input.splitlines()
expected_pyi_output = ('import os\n'
'\n'
'def my_method():\n').splitlines()
assert SortImports(file_contents=test_input).output.splitlines() == expected_py_output
assert SortImports(file_contents=test_input,
extension="pyi").output.splitlines() == expected_pyi_output

source_py = tmpdir.join('source.py')
source_py.write(test_input)
assert SortImports(file_path=str(source_py)).output.splitlines() == expected_py_output

source_pyi = tmpdir.join('source.pyi')
source_pyi.write(test_input)
assert SortImports(file_path=str(source_pyi)).output.splitlines() == expected_pyi_output
5 changes: 0 additions & 5 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@ commands = pytest {posargs}
basepython = python3
commands = python setup.py isort

[testenv:lint]
deps = flake8==3.7.6
commands = flake8
skip_install = True

[testenv:mypy]
basepython = python3
deps = -r{toxinidir}/mypy-requirements.txt
Expand Down

0 comments on commit 55f1ae7

Please sign in to comment.