Skip to content

Commit

Permalink
Merge branch 'release/3.9.6'
Browse files Browse the repository at this point in the history
  • Loading branch information
timothycrosley committed Feb 6, 2015
2 parents beede24 + b1ccf0b commit db10e70
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 14 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.5"
__version__ = "3.9.6"
28 changes: 17 additions & 11 deletions isort/isort.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
from sys import stderr, stdout

from natsort import natsorted
from pies.collections import OrderedDict
from pies.overrides import *

from . import settings
Expand Down Expand Up @@ -111,7 +112,7 @@ def __init__(self, file_path=None, file_contents=None, write_to_stdout=False, ch
self.imports = {}
self.as_map = {}
for section in itertools.chain(SECTIONS, self.config['forced_separate']):
self.imports[section] = {'straight': set(), 'from': {}}
self.imports[section] = {'straight': set(), 'from': OrderedDict()}

self.index = 0
self.import_index = -1
Expand Down Expand Up @@ -426,7 +427,7 @@ def _add_formatted_imports(self):
straight_modules = list(self.imports[section]['straight'])
straight_modules = natsorted(straight_modules, key=lambda key: self._module_key(key, self.config))
from_modules = list(self.imports[section]['from'].keys())
from_modules = natsorted(from_modules, key=lambda key: self._module_key(key, self.config))
from_modules = natsorted(from_modules, key=lambda key: self._module_key(key, self.config, ))

section_output = []
if self.config.get('from_first', False):
Expand Down Expand Up @@ -640,7 +641,7 @@ def _parse(self):
self.import_index = self.index - 1
continue

if "isort:" + "imports-" in line:
if "isort:" + "imports-" in line and line.startswith("#"):
section = line.split("isort:" + "imports-")[-1].split()[0]
self.place_imports[section.upper()] = []
self.import_placements[line] = section.upper()
Expand Down Expand Up @@ -719,10 +720,13 @@ 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 (len(self.out_lines) > self.import_index and last.startswith("#") and not last.endswith('"""') and not
last.endswith("'''")):
self.comments['above']['from'].setdefault(import_from, []).append(self.out_lines.pop(-1))

if len(self.out_lines) > self.import_index:
last = self.out_lines and self.out_lines[-1].rstrip() or ""
while last.startswith("#") and not last.endswith('"""') and not last.endswith("'''"):
self.comments['above']['from'].setdefault(import_from, []).insert(0, self.out_lines.pop(-1))
last = self.out_lines and self.out_lines[-1].rstrip() or ""

if root.get(import_from, False):
root[import_from].update(imports)
else:
Expand All @@ -732,8 +736,10 @@ def _parse(self):
if comments:
self.comments['straight'][module] = comments
comments = None
last = self.out_lines and self.out_lines[-1].rstrip() or ""
if (len(self.out_lines) > self.import_index and last.startswith("#") and not
last.endswith('"""') and not last.endswith("'''")):
self.comments['above']['from'][module] = self.out_lines.pop(-1)

if len(self.out_lines) > self.import_index:
last = self.out_lines and self.out_lines[-1].rstrip() or ""
while last.startswith("#") and not last.endswith('"""') and not last.endswith("'''"):
self.comments['above']['from'].setdefault(module, []).insert(0, self.out_lines.pop(-1))
last = self.out_lines and self.out_lines[-1].rstrip() or ""
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.5',
version='3.9.6',
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.5.tar.gz',
download_url='https://github.com/timothycrosley/isort/archive/3.9.6.tar.gz',
license="MIT",
entry_points={
'console_scripts': [
Expand Down
8 changes: 8 additions & 0 deletions test_isort.py
Original file line number Diff line number Diff line change
Expand Up @@ -1206,6 +1206,14 @@ def test_sticky_comments():
"from selenium.webdriver.remote.webdriver import WebDriver # noqa\n")
assert SortImports(file_contents=test_input).output == test_input

test_input = ("from django import forms\n"
"# While this couples the geographic forms to the GEOS library,\n"
"# it decouples from database (by not importing SpatialBackend).\n"
"from django.contrib.gis.geos import GEOSException, GEOSGeometry\n"
"from django.utils.translation import ugettext_lazy as _\n")
assert SortImports(file_contents=test_input).output == test_input



def test_zipimport():
"""Imports ending in "import" shouldn't be clobbered"""
Expand Down

0 comments on commit db10e70

Please sign in to comment.