From e50043217f06fd0105bd6b1530ff921123431705 Mon Sep 17 00:00:00 2001 From: Timothy Crosley Date: Thu, 25 Dec 2014 01:58:09 -0500 Subject: [PATCH 1/6] Update readme --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 7a5f4ef7a..90b62046b 100644 --- a/README.md +++ b/README.md @@ -404,6 +404,7 @@ hours automating it. I was given permission to open source sortImports and here -------------------------------------------- + Thanks and I hope you find isort useful! ~Timothy Crosley From db9f7a2d1d6466f0fb8c12a2ec643dc0e0c6f725 Mon Sep 17 00:00:00 2001 From: Timothy Crosley Date: Thu, 25 Dec 2014 02:00:50 -0500 Subject: [PATCH 2/6] Update readme --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index 90b62046b..7a5f4ef7a 100644 --- a/README.md +++ b/README.md @@ -404,7 +404,6 @@ hours automating it. I was given permission to open source sortImports and here -------------------------------------------- - Thanks and I hope you find isort useful! ~Timothy Crosley From c707c7e3e248c2068aa731bcdbfb53b18492a407 Mon Sep 17 00:00:00 2001 From: Timothy Crosley Date: Thu, 25 Dec 2014 15:03:02 -0500 Subject: [PATCH 3/6] Add test for desired isort above commenting feature --- test_isort.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test_isort.py b/test_isort.py index 00a403035..9bfb6679e 100644 --- a/test_isort.py +++ b/test_isort.py @@ -1105,3 +1105,11 @@ 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). isort:comment-above\n" + "from selenium.webdriver.remote.webdriver import WebDriver # noqa\n") + assert SortImports(file_contents=test_input).output == test_input \ No newline at end of file From b1b46c6aba61d986bacbbca3cf212e906bdaa3af Mon Sep 17 00:00:00 2001 From: Timothy Crosley Date: Mon, 29 Dec 2014 16:29:11 -0500 Subject: [PATCH 4/6] Fix isort to assume that above comments auto get included instead of requiring manual adjustment --- test_isort.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/test_isort.py b/test_isort.py index 9bfb6679e..978463940 100644 --- a/test_isort.py +++ b/test_isort.py @@ -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" @@ -1106,10 +1107,11 @@ def test_placement_control(): "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). isort:comment-above\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 \ No newline at end of file + assert SortImports(file_contents=test_input).output == test_input From de68bee9b875ba7e7eb2b614b4bc786f95f8b7f1 Mon Sep 17 00:00:00 2001 From: Timothy Crosley Date: Mon, 29 Dec 2014 16:30:00 -0500 Subject: [PATCH 5/6] Add support for automatically storing and redrawing comments that show above imports --- isort/isort.py | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/isort/isort.py b/isort/isort.py index ba3765416..6b6518ebc 100644 --- a/isort/isort.py +++ b/isort/isort.py @@ -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']): @@ -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()) @@ -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 @@ -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: @@ -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: @@ -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) From b2d93dffcda7284168b4c052894892065ad218af Mon Sep 17 00:00:00 2001 From: Timothy Crosley Date: Mon, 29 Dec 2014 16:32:26 -0500 Subject: [PATCH 6/6] Bump version to 3.9.4 --- isort/__init__.py | 2 +- setup.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/isort/__init__.py b/isort/__init__.py index 6a905ca85..377a86346 100644 --- a/isort/__init__.py +++ b/isort/__init__.py @@ -25,4 +25,4 @@ from . import settings from .isort import SECTION_NAMES, SECTIONS, SortImports -__version__ = "3.9.3" +__version__ = "3.9.4" diff --git a/setup.py b/setup.py index 8e838087e..8d3912133 100755 --- a/setup.py +++ b/setup.py @@ -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='timothy.crosley@gmail.com', 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': [