diff --git a/HISTORY.rst b/HISTORY.rst index 9b19d94..62064a8 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -3,6 +3,11 @@ History ------- +0.5.1 (2017-05-09) +~~~~~~~~~~~~~~~~~~ + +* Fixed bug which incorrectly removed duplicate leafless imports which had different ``as`` names + 0.5.0 (2017-05-03) ~~~~~~~~~~~~~~~~~~ diff --git a/importanize/__init__.py b/importanize/__init__.py index 9cf9f41..273edef 100755 --- a/importanize/__init__.py +++ b/importanize/__init__.py @@ -4,7 +4,7 @@ __author__ = 'Miroslav Shubernetskiy' __email__ = 'miroslav@miki725.com' -__version__ = '0.5' +__version__ = '0.5.1' __description__ = ( 'Utility for organizing Python imports using PEP8 or custom rules' ) diff --git a/importanize/groups.py b/importanize/groups.py index 0adaf65..e9d5fd2 100644 --- a/importanize/groups.py +++ b/importanize/groups.py @@ -36,7 +36,7 @@ def merged_statements(self): else: leafless_counter[statement.stem].append(statement) - merged_statements = [i[0] for i in leafless_counter.values()] + merged_statements = list(itertools.chain(*leafless_counter.values())) def merge(statements): _special = [] diff --git a/importanize/statements.py b/importanize/statements.py index 68b0fc8..f8f1ba9 100755 --- a/importanize/statements.py +++ b/importanize/statements.py @@ -56,7 +56,8 @@ def __hash__(self): return hash(self.as_string()) def __eq__(self, other): - return self.name == other.name + return all([self.name == other.name, + self.as_name == other.as_name]) def __gt__(self, other): def _type(obj): @@ -121,6 +122,13 @@ def __init__(self, line_numbers, stem, leafs=None, self.comments = comments or [] self.file_artifacts = kwargs.get('file_artifacts', {}) + @property + def full_stem(self): + stem = self.stem + if self.as_name: + stem += ' as {}'.format(self.as_name) + return stem + @property def unique_leafs(self): return sorted(list(set(self.leafs))) @@ -137,10 +145,7 @@ def root_module(self): def as_string(self): if not self.leafs: - data = 'import {}'.format(self.stem) - if self.as_name: - data += ' as {}'.format(self.as_name) - return data + return 'import {}'.format(self.full_stem) else: return ( 'from {} import {}' @@ -179,6 +184,7 @@ def __add__(self, other): def __eq__(self, other): return all((self.stem == other.stem, + self.as_name == other.as_name, self.unique_leafs == other.unique_leafs)) def __gt__(self, other): @@ -224,4 +230,4 @@ def __gt__(self, other): return self_len > other_len # alphabetical sort - return self.stem > other.stem + return self.full_stem > other.full_stem diff --git a/tests/test_data/input.py b/tests/test_data/input.py index 4cf3b95..dec704e 100644 --- a/tests/test_data/input.py +++ b/tests/test_data/input.py @@ -6,6 +6,8 @@ import os.path as ospath import datetime from package.subpackage.module.submodule import CONSTANT, Klass, foo, bar, rainbows +import datetime.parser +import datetime as mydatetime from .module import foo, bar from ..othermodule import rainbows from a import b @@ -18,6 +20,7 @@ import coverage # in site-packages from z import foo +import datetime.parser import something # with comment from other.package.subpackage.module.submodule import CONSTANT, Klass, foo, bar, rainbows # noqa from other import( diff --git a/tests/test_data/output_grouped.py b/tests/test_data/output_grouped.py index 8176c14..38e3715 100644 --- a/tests/test_data/output_grouped.py +++ b/tests/test_data/output_grouped.py @@ -2,6 +2,8 @@ from __future__ import print_function, unicode_literals import datetime +import datetime as mydatetime +import datetime.parser from os import path as ospath import coverage # in site-packages diff --git a/tests/test_data/output_inline_grouped.py b/tests/test_data/output_inline_grouped.py index 3908cea..55b776e 100644 --- a/tests/test_data/output_inline_grouped.py +++ b/tests/test_data/output_inline_grouped.py @@ -2,6 +2,8 @@ from __future__ import print_function, unicode_literals import datetime +import datetime as mydatetime +import datetime.parser from os import path as ospath import coverage # in site-packages diff --git a/tests/test_parser.py b/tests/test_parser.py index 001742e..0f5592e 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -279,6 +279,10 @@ def test_import_statements(self): 'import a as b', 'import a as b' ) + self._test_import_string_matches( + 'import a.b', + 'import a.b' + ) self._test_import_string_matches( 'import a.b as b', 'from a import b' @@ -287,6 +291,10 @@ def test_import_statements(self): 'import a.b as c', 'from a import b as c' ) + self._test_import_string_matches( + 'import a.b.c as d', + 'from a.b import c as d' + ) self._test_import_string_matches( 'import a.b.c', 'import a.b.c',