From 6939a8049ad71be13237785cff0894ba7e9a4128 Mon Sep 17 00:00:00 2001 From: Juan Toledo Date: Tue, 27 Dec 2022 10:47:25 +0100 Subject: [PATCH 1/2] Added test case for cython pure python import --- tests/unit/test_ticketed_features.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tests/unit/test_ticketed_features.py b/tests/unit/test_ticketed_features.py index 0460ae05..b5b6192a 100644 --- a/tests/unit/test_ticketed_features.py +++ b/tests/unit/test_ticketed_features.py @@ -1056,3 +1056,20 @@ def test_sort_configurable_sort_issue_1732() -> None: ) with pytest.raises(exceptions.SortingFunctionDoesNotExist): isort.code(test_input, sort_order="round") + + +def test_cython_pure_python_imports_2062(): + """Test to ensure an import form a cython.cimports remains import, not cimport. + See: https://github.com/pycqa/isort/issues/2062. + """ + assert isort.check_code( + """ +import cython +from cython.cimports.libc import math + + +def use_libc_math(): + return math.ceil(5.5) +""", + show_diff=True, + ) From 9b37e895aaa67223e92a0168f7427f4e375600c5 Mon Sep 17 00:00:00 2001 From: Juan Toledo Date: Tue, 27 Dec 2022 11:03:22 +0100 Subject: [PATCH 2/2] Avoid misidentifying a pure python cython import as a cimport. Fixes #2062 --- isort/core.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/isort/core.py b/isort/core.py index 9dfaf70f..4c77a1d9 100644 --- a/isort/core.py +++ b/isort/core.py @@ -326,7 +326,10 @@ def process( or " cimport " in import_statement or " cimport*" in import_statement or " cimport(" in import_statement - or ".cimport" in import_statement + or ( + ".cimport" in import_statement + and "cython.cimports" not in import_statement + ) # Allow pure python imports. See #2062 ): cimport_statement = True