Skip to content

src_path semantics needs clarification #2365

Description

@layus

I have added two lines to tests/unit/test_place.py to show the confusion.
In that test, the src_path is set to /home/layus/projects/isort_github_repo/isort (on my machine obviously) and contains the deprecated subfolder. (src_path is the isort folder in this repo).

I would expect this src_path to report either the isort module or the deprecated module to be THIRDPARTY. But they are all reported as FIRSTPARTY.

That is why I think that src_path should have stricter semantics. For my purposes I would prefer to never accept src_path / "__init__.py" as a valid src_path.name module, but I can see the convenience of specifying modules directly instead of their parent directory.
It is easy to switch from one semantics to the other using 'src_path/*orsrc_path.parent`.

I believe this is the confusion behind #1696, #2247 and #2247.

I can patch it, but I need some decision on the desired behavior.

diff --git a/tests/unit/test_place.py b/tests/unit/test_place.py
index c850c689..876b0c8d 100644
--- a/tests/unit/test_place.py
+++ b/tests/unit/test_place.py
@@ -9,6 +9,8 @@ from isort.settings import Config
 def test_module(src_path):
     place_tester = partial(place.module, config=Config(src_paths=[src_path]))
     assert place_tester("isort") == sections.FIRSTPARTY
+    assert place_tester("isort.deprecated") == sections.FIRSTPARTY
+    assert place_tester("deprecated") == sections.THIRDPARTY
     assert place_tester("os") == sections.STDLIB
     assert place_tester(".deprecated") == sections.LOCALFOLDER
     assert place_tester("__future__") == sections.FUTURE

test result:

===================================================================================================== FAILURES =====================================================================================================
___________________________________________________________________________________________________ test_module ____________________________________________________________________________________________________

src_path = PosixPath('/home/layus/projects/isort_github_repo/isort')

    def test_module(src_path):
        place_tester = partial(place.module, config=Config(src_paths=[src_path]))
        assert place_tester("isort") == sections.FIRSTPARTY
        assert place_tester("isort.deprecated") == sections.FIRSTPARTY
>       assert place_tester("deprecated") == sections.THIRDPARTY
E       AssertionError: assert 'FIRSTPARTY' == 'THIRDPARTY'
E         
E         - THIRDPARTY
E         + FIRSTPARTY

tests/unit/test_place.py:13: AssertionError
----------------------------------------------------------------------------------------------- Captured stdout call -----------------------------------------------------------------------------------------------
/home/layus/projects/isort_github_repo/isort/isort isort
exists_case_sensitive(/home/layus/projects/isort_github_repo/isort.py) = False
exists_case_sensitive(/home/layus/projects/isort_github_repo/isort.cpython-312-x86_64-linux-gnu.so) = False
exists_case_sensitive(/home/layus/projects/isort_github_repo/isort.abi3.so) = False
exists_case_sensitive(/home/layus/projects/isort_github_repo/isort.so) = False
exists_case_sensitive(/home/layus/projects/isort_github_repo/isort/__init__.py) = True
_is_module(/home/layus/projects/isort_github_repo/isort) = True
/home/layus/projects/isort_github_repo/isort/isort isort
exists_case_sensitive(/home/layus/projects/isort_github_repo/isort) = True
_is_package(/home/layus/projects/isort_github_repo/isort) = True
_is_module(/home/layus/projects/isort_github_repo/isort) = True
/home/layus/projects/isort_github_repo/isort/deprecated deprecated
exists_case_sensitive(/home/layus/projects/isort_github_repo/isort/deprecated.py) = False
exists_case_sensitive(/home/layus/projects/isort_github_repo/isort/deprecated.cpython-312-x86_64-linux-gnu.so) = False
exists_case_sensitive(/home/layus/projects/isort_github_repo/isort/deprecated.abi3.so) = False
exists_case_sensitive(/home/layus/projects/isort_github_repo/isort/deprecated.so) = False
exists_case_sensitive(/home/layus/projects/isort_github_repo/isort/deprecated/__init__.py) = True
_is_module(/home/layus/projects/isort_github_repo/isort/deprecated) = True
============================================================================================= short test summary info ==============================================================================================
FAILED tests/unit/test_place.py::test_module - AssertionError: assert 'FIRSTPARTY' == 'THIRDPARTY'
=========================================================================================== 1 failed, 3 passed in 0.56s ============================================================================================

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions