diff options
author | Timothy Crosley <timothy.crosley@gmail.com> | 2021-02-07 22:55:11 -0800 |
---|---|---|
committer | Timothy Crosley <timothy.crosley@gmail.com> | 2021-02-07 22:55:11 -0800 |
commit | 929ee15fe8f2e40480434bcf641c1681c7f2ead6 (patch) | |
tree | 4a19a5f4bdddf98d1fdc3bb3fb08da3abc8cb687 | |
parent | 60dec54fc534e9085e3193c2ca9f75d97bcd19ef (diff) | |
download | isort-issue/1641.tar.gz |
Made identified imports .statement() runnable codeissue/1641
-rw-r--r-- | CHANGELOG.md | 1 | ||||
-rw-r--r-- | isort/identify.py | 10 | ||||
-rw-r--r-- | tests/unit/test_isort.py | 8 |
3 files changed, 11 insertions, 8 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index b169ff6f..483a1392 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ Find out more about isort's release policy [here](https://pycqa.github.io/isort/ ### 5.8.0 TBD - Fixed #1631: as import comments can in some cases be duplicated. - Implemented #1648: Export MyPY type hints. + - Implemented #1641: Identified import statements now return runnable code. ### 5.7.0 December 30th 2020 - Fixed #1612: In rare circumstances an extra comma is added after import and before comment. diff --git a/isort/identify.py b/isort/identify.py index ff028244..6a4f6d7d 100644 --- a/isort/identify.py +++ b/isort/identify.py @@ -23,12 +23,14 @@ class Import(NamedTuple): file_path: Optional[Path] = None def statement(self) -> str: - full_path = self.module + import_cmd = "cimport" if self.cimport else "import" if self.attribute: - full_path += f".{self.attribute}" + import_string = f"from {self.module} {import_cmd} {self.attribute}" + else: + import_string = f"{import_cmd} {self.module}" if self.alias: - full_path += f" as {self.alias}" - return f"{'cimport' if self.cimport else 'import'} {full_path}" + import_string += f" as {self.alias}" + return import_string def __str__(self): return ( diff --git a/tests/unit/test_isort.py b/tests/unit/test_isort.py index e5ab86d3..39a396b8 100644 --- a/tests/unit/test_isort.py +++ b/tests/unit/test_isort.py @@ -5032,11 +5032,11 @@ def test_find_imports_in_code() -> None: assert identified_imports == [ ":1 import first_straight", ":3 import second_straight", - ":4 import first_from.first_from_function_1", - ":4 import first_from.first_from_function_2", + ":4 from first_from import first_from_function_1", + ":4 from first_from import first_from_function_2", ":5 import bad_name as good_name", - ":6 import parent.some_bad_defs.bad_name_1 as ok_name_1", - ":6 import parent.some_bad_defs.bad_name_2 as ok_name_2", + ":6 from parent.some_bad_defs import bad_name_1 as ok_name_1", + ":6 from parent.some_bad_defs import bad_name_2 as ok_name_2", ":12 indented import needed_in_bla_2", ":15 indented import needed_in_bla", ":18 indented import needed_in_bla_bla", |