From 463803e930f0232d4e851da0d231f8789024107b Mon Sep 17 00:00:00 2001 From: Kurt McKee Date: Wed, 5 Mar 2025 07:45:59 -0600 Subject: [PATCH 1/4] Remove duplicate tests These were identified but marked as `noqa`. Now the duplicates are simply removed. --- tests/unit/test_parse.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/unit/test_parse.py b/tests/unit/test_parse.py index 6b0d9be9..9757496a 100644 --- a/tests/unit/test_parse.py +++ b/tests/unit/test_parse.py @@ -95,10 +95,8 @@ def test_fuzz_skip_line(line, in_quote, index, section_comments, needs_import): ("from..import a", "from .. import a"), ("import *", "import *"), ("import*", "import *"), - ("from . import a", "from . import a"), # noqa: PT014 ("from .import a", "from . import a"), ("from ..import a", "from .. import a"), - ("from . cimport a", "from . cimport a"), # noqa: PT014 ("from .cimport a", "from . cimport a"), ("from ..cimport a", "from .. cimport a"), ("from\t.\timport a", "from . import a"), From d05628cf2e5fe754082b0aff0f84fd74c9ab5e9b Mon Sep 17 00:00:00 2001 From: Kurt McKee Date: Wed, 5 Mar 2025 08:33:46 -0600 Subject: [PATCH 2/4] Demonstrate failing cases in `normalize_line()` --- tests/unit/test_parse.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/unit/test_parse.py b/tests/unit/test_parse.py index 9757496a..804f7f40 100644 --- a/tests/unit/test_parse.py +++ b/tests/unit/test_parse.py @@ -99,7 +99,9 @@ def test_fuzz_skip_line(line, in_quote, index, section_comments, needs_import): ("from ..import a", "from .. import a"), ("from .cimport a", "from . cimport a"), ("from ..cimport a", "from .. cimport a"), - ("from\t.\timport a", "from . import a"), + ("from\t\t.\t\timport\t\ta", "from . import a"), + ("from \t __future__ \t import \t annotations", "from __future__ import annotations"), + ("from..x import y", "from ..x import y"), ], ) def test_normalize_line(raw_line, expected): From 9aeaf271c1fe4abef991cc7d9f1e876880ccecf7 Mon Sep 17 00:00:00 2001 From: Kurt McKee Date: Wed, 5 Mar 2025 08:39:16 -0600 Subject: [PATCH 3/4] Fix failing test cases, and with a 2.7x speedup --- isort/parse.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/isort/parse.py b/isort/parse.py index e1b20576..e288e02d 100644 --- a/isort/parse.py +++ b/isort/parse.py @@ -38,17 +38,18 @@ def _infer_line_separator(contents: str) -> str: return "\n" +_from_absolute_normalizer_pattern = re.compile(r"from[\t ]+(.+?)[\t ]+(c?import)[\t ]+") +_from_relative_normalizer_pattern = re.compile(r"from[\t ]*(\.+[^\t ]*?)[\t ]*(c?import)[\t ]+") + + def normalize_line(raw_line: str) -> Tuple[str, str]: """Normalizes import related statements in the provided line. Returns (normalized_line: str, raw_line: str) """ - line = re.sub(r"from(\.+)cimport ", r"from \g<1> cimport ", raw_line) - line = re.sub(r"from(\.+)import ", r"from \g<1> import ", line) + line = _from_absolute_normalizer_pattern.sub(r"from \g<1> \g<2> ", raw_line) + line = _from_relative_normalizer_pattern.sub(r"from \g<1> \g<2> ", line) line = line.replace("import*", "import *") - line = re.sub(r" (\.+)import ", r" \g<1> import ", line) - line = re.sub(r" (\.+)cimport ", r" \g<1> cimport ", line) - line = line.replace("\t", " ") return line, raw_line From 44c174ab9af052a3e261c0f151d43a2fd5efca2c Mon Sep 17 00:00:00 2001 From: Kurt McKee Date: Tue, 18 Mar 2025 15:58:31 -0500 Subject: [PATCH 4/4] Feedback: Uppercase variable names --- isort/parse.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/isort/parse.py b/isort/parse.py index e288e02d..3efa2b83 100644 --- a/isort/parse.py +++ b/isort/parse.py @@ -38,8 +38,8 @@ def _infer_line_separator(contents: str) -> str: return "\n" -_from_absolute_normalizer_pattern = re.compile(r"from[\t ]+(.+?)[\t ]+(c?import)[\t ]+") -_from_relative_normalizer_pattern = re.compile(r"from[\t ]*(\.+[^\t ]*?)[\t ]*(c?import)[\t ]+") +_FROM_ABSOLUTE_NORMALIZER_PATTERN = re.compile(r"from[\t ]+(.+?)[\t ]+(c?import)[\t ]+") +_FROM_RELATIVE_NORMALIZER_PATTERN = re.compile(r"from[\t ]*(\.+[^\t ]*?)[\t ]*(c?import)[\t ]+") def normalize_line(raw_line: str) -> Tuple[str, str]: @@ -47,8 +47,8 @@ def normalize_line(raw_line: str) -> Tuple[str, str]: Returns (normalized_line: str, raw_line: str) """ - line = _from_absolute_normalizer_pattern.sub(r"from \g<1> \g<2> ", raw_line) - line = _from_relative_normalizer_pattern.sub(r"from \g<1> \g<2> ", line) + line = _FROM_ABSOLUTE_NORMALIZER_PATTERN.sub(r"from \g<1> \g<2> ", raw_line) + line = _FROM_RELATIVE_NORMALIZER_PATTERN.sub(r"from \g<1> \g<2> ", line) line = line.replace("import*", "import *") return line, raw_line