Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple src_paths can lead to nondeterministic behavior #1819

Closed
forana opened this issue Sep 30, 2021 · 1 comment
Closed

Multiple src_paths can lead to nondeterministic behavior #1819

forana opened this issue Sep 30, 2021 · 1 comment
Labels

Comments

@forana
Copy link

@forana forana commented Sep 30, 2021

I've been facing an issue in a repo with a structure like:

/app
  __init__.py
  main.py
  whatever.py
  # etc
/tests
  __init__.py
  something/__init__.py
  something/else.py
  another_thing/__init__.py
  another_thing/problem_file.py
  # etc
/pyproject.toml

with the contents of pyproject.toml being:

[tool.isort]
profile = "black"
src_paths = ["app", "tests"]

and /tests/another_thing/problem_file.py containing:

from tests.something.else import some_function

What I was seeing was inconsistent results - when running isort tests --check -v, about half the time I would see tests.something.else module correctly being identified as FIRSTPARTY in its own absolute imports, and the other half of the time, it would be identified as THIRDPARTY.

My clue was the fact that, if you run with --show-config a number of times, you'll see that all of the list orders are nondeterministic between runs. The issue looks to be here:

isort/isort/place.py

Lines 76 to 87 in be0fbd0

for src_path in src_paths:
module_path = (src_path / root_module_name).resolve()
if not prefix and not module_path.is_dir() and src_path.name == root_module_name:
module_path = src_path.resolve()
if nested_module and (
namespace in config.namespace_packages
or (
config.auto_identify_namespace_packages
and _is_namespace_package(module_path, config.supported_extensions)
)
):
return _src_path(nested_module[0], config, (module_path,), new_prefix)

_src_path is attempting to recurse to figure out if the module is on one of the src_paths or not - but, if the ordering for my run happened to place app first, auto_identify_namespace_packages defaults to True and there's an __init__,py in the app directory, so _is_namespace_package returns True, so it recurses into the app path. But because that's done via return - it never considers the tests entry.

If I change my pyproject.toml to either include auto_identify_namespace_packages = false or use known_local_folder instead of src_paths, I no longer get inconsistent results (and always get what I expect).

@forana
Copy link
Author

@forana forana commented Sep 30, 2021

Note: this is similar to #1704, but I don't think it's the same.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants