Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion isort/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -1067,7 +1067,8 @@ def main(argv: Sequence[str] | None = None, stdin: TextIOWrapper | None = None)

config_trie: Trie | None = None
if resolve_all_configs:
config_trie = find_all_configs(config_dict.pop("config_root", "."))
config_root = config_dict.pop("config_root", ".")
config_trie = find_all_configs(config_root, config_dict)

if "src_paths" in config_dict:
# Keep CLI-provided values as-is so wildcard patterns can be expanded later
Expand Down
15 changes: 11 additions & 4 deletions isort/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -782,13 +782,20 @@ def _find_config(path: str) -> tuple[str, dict[str, Any]]:
return (path, {})


def find_all_configs(path: str) -> Trie:
def find_all_configs(path: str, config_overrides: dict[str, Any] | None = None) -> Trie:
"""
Looks for config files in the path provided and in all of its sub-directories.
Parses and stores any config file encountered in a trie and returns the root of
the trie
the trie. Any config_overrides take precedence over the settings of every config file.
The settings_path and settings_file overrides are ignored, so that each file keeps the
config file found nearest to it.
"""
trie_root = Trie("default", {})
config_overrides = {
key: value
for key, value in (config_overrides or {}).items()
if key not in ("settings_path", "settings_file")
}
trie_root = Trie("default", config_overrides)

for dirpath, _, _ in os.walk(path):
for config_file_name in CONFIG_SOURCES:
Expand All @@ -807,7 +814,7 @@ def find_all_configs(path: str) -> Trie:
config_data = {}

if config_data:
trie_root.insert(potential_config_file, config_data)
trie_root.insert(potential_config_file, {**config_data, **config_overrides})
break

return trie_root
Expand Down
30 changes: 30 additions & 0 deletions tests/unit/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,36 @@ def test_config_root_interaction_with_resolve_all_configs(tmp_path: Path) -> Non
main.main([str(python_file), "--config-root", str(tmp_path), "--resolve-all-configs"])


def test_resolve_all_configs_applies_command_line_options(tmp_path: Path) -> None:
project_a = tmp_path / "project_a"
project_b = tmp_path / "project_b"
project_a.mkdir()
project_b.mkdir()
(project_a / ".isort.cfg").write_text("[settings]\nforce_sort_within_sections = true\n")
(project_b / ".isort.cfg").write_text("[settings]\nline_length = 100\n")
file_a = project_a / "a.py"
file_b = project_b / "b.py"
file_c = tmp_path / "c.py"
for python_file in (file_a, file_b, file_c):
python_file.write_text("from os import path, sep\nimport sys\n")

main.main(
[
str(file_a),
str(file_b),
str(file_c),
"--force-single-line-imports",
"--resolve-all-configs",
"--config-root",
str(tmp_path),
]
)

assert file_a.read_text() == "from os import path\nfrom os import sep\nimport sys\n"
assert file_b.read_text() == "import sys\nfrom os import path\nfrom os import sep\n"
assert file_c.read_text() == "import sys\nfrom os import path\nfrom os import sep\n"


def test_main(capsys: pytest.CaptureFixture[str], tmp_path: Path) -> None:
base_args = [
"--sp",
Expand Down
23 changes: 23 additions & 0 deletions tests/unit/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,29 @@ def test_as_bool() -> None:
settings._as_bool("truthy")


def test_find_all_configs_ignores_settings_path_overrides(tmp_path: Path) -> None:
project = tmp_path / "project"
project.mkdir()
(project / ".isort.cfg").write_text("[settings]\nprofile=black\n", "utf-8")

config_trie = settings.find_all_configs(
str(tmp_path),
{
"line_length": 100,
"settings_path": str(tmp_path),
"settings_file": str(tmp_path / "other.cfg"),
},
)

project_config = config_trie.search(str(project / "test.py"))[1]
assert project_config["profile"] == "black"
assert project_config["line_length"] == 100
assert "settings_path" not in project_config
assert "settings_file" not in project_config

assert config_trie.search(str(tmp_path / "test.py")) == ("default", {"line_length": 100})


def test_find_all_configs(tmp_path: Path) -> None:
setup_cfg = """
[isort]
Expand Down
Loading