Skip to content

Commit

Permalink
Merge branch 'release/3.9.4'
Browse files Browse the repository at this point in the history
  • Loading branch information
timothycrosley committed Dec 29, 2014
2 parents e8a556b + b2d93df commit 7b1bb99
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 5 deletions.
2 changes: 1 addition & 1 deletion isort/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@
from . import settings
from .isort import SECTION_NAMES, SECTIONS, SortImports

__version__ = "3.9.3"
__version__ = "3.9.4"
19 changes: 17 additions & 2 deletions isort/isort.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def __init__(self, file_path=None, file_contents=None, write_to_stdout=False, ch
self.number_of_lines = len(self.in_lines)

self.out_lines = []
self.comments = {'from': {}, 'straight': {}, 'nested': {}}
self.comments = {'from': {}, 'straight': {}, 'nested': {}, 'above': {'straight': {}, 'from': {}}}
self.imports = {}
self.as_map = {}
for section in itertools.chain(SECTIONS, self.config['forced_separate']):
Expand Down Expand Up @@ -305,6 +305,9 @@ def _add_formatted_imports(self):
else:
import_definition = "import {0}".format(module)

comments_above = self.comments['above']['straight'].get(module, None)
if comments_above:
section_output.append(comments_above)
section_output.append(self._add_comments(self.comments['straight'].get(module), import_definition))

from_modules = list(self.imports[section]['from'].keys())
Expand Down Expand Up @@ -362,6 +365,9 @@ def _add_formatted_imports(self):
if comment:
single_import_line = self._add_comments(comments, import_start + from_import)
single_import_line += "{0} {1}".format(comments and ";" or " #", comment)
above_comments = self.comments['above']['from'].get(module, None)
if above_comments:
section_output.extend(above_comments)
section_output.append(self._wrap(single_import_line))
from_imports.remove(from_import)
comments = None
Expand Down Expand Up @@ -400,9 +406,12 @@ def _add_formatted_imports(self):
else:
import_statement = self._wrap(import_statement)


if import_statement:
above_comments = self.comments['above']['from'].get(module, None)
if above_comments:
section_output.extend(above_comments)
section_output.append(import_statement)

if section_output:
section_name = section
if section in SECTIONS:
Expand Down Expand Up @@ -669,6 +678,9 @@ def _parse(self):
comments.pop(comments.index(associated_commment))
if comments:
self.comments['from'].setdefault(import_from, []).extend(comments)
last = self.out_lines and self.out_lines[-1].rstrip() or ""
if last.startswith("#") and not last.endswith('"""') and not last.endswith("'''"):
self.comments['above']['from'].setdefault(import_from, []).append(self.out_lines.pop(-1))
if root.get(import_from, False):
root[import_from].update(imports)
else:
Expand All @@ -678,4 +690,7 @@ def _parse(self):
if comments:
self.comments['straight'][module] = comments
comments = None
last = self.out_lines and self.out_lines[-1].rstrip() or ""
if last.startswith("#") and not last.endswith('"""') and not last.endswith("'''"):
self.comments['above']['from'][module] = self.out_lines.pop(-1)
self.imports[self.place_module(module)][import_type].add(module)
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ def run(self):
readme = ''

setup(name='isort',
version='3.9.3',
version='3.9.4',
description='A Python utility / library to sort Python imports.',
long_description=readme,
author='Timothy Crosley',
author_email='[email protected]',
url='https://github.com/timothycrosley/isort',
download_url='https://github.com/timothycrosley/isort/archive/3.9.3.tar.gz',
download_url='https://github.com/timothycrosley/isort/archive/3.9.4.tar.gz',
license="MIT",
entry_points={
'console_scripts': [
Expand Down
10 changes: 10 additions & 0 deletions test_isort.py
Original file line number Diff line number Diff line change
Expand Up @@ -1095,6 +1095,7 @@ def test_placement_control():
test_output = SortImports(file_contents=test_input,
known_first_party=['p24', 'p24.imports._VERSION'],
known_standard_library=['p24.imports'],
known_third_party=['bottle'],
default_section="THIRDPARTY").output
assert test_output == ("import os\n"
"import p24.imports._argparse as argparse\n"
Expand All @@ -1105,3 +1106,12 @@ def test_placement_control():
"\n"
"import p24.imports._VERSION as VERSION\n"
"import p24.shared.media_wiki_syntax as syntax\n")


def test_sticky_comments():
"""Test to ensure it is possible to make comments 'stick' above imports"""
test_input = ("import os\n"
"\n"
"# Used for type-hinting (ref: https://github.com/davidhalter/jedi/issues/414).\n"
"from selenium.webdriver.remote.webdriver import WebDriver # noqa\n")
assert SortImports(file_contents=test_input).output == test_input

0 comments on commit 7b1bb99

Please sign in to comment.