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
10 changes: 6 additions & 4 deletions isort/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,10 +327,11 @@ def check_file(
config_trie = config_kwargs.pop("config_trie", None)
if config_trie:
config_info = config_trie.search(filename)
if config.verbose:
if config_info[0] and config.verbose:
print(f"{config_info[0]} used for file {filename}")

file_config = Config(**config_info[1])
if config_info[0]:
file_config = Config(**config_info[1])

with io.File.read(filename) as source_file:
return check_stream(
Expand Down Expand Up @@ -422,10 +423,11 @@ def sort_file(
config_trie = config_kwargs.pop("config_trie", None)
if config_trie:
config_info = config_trie.search(filename)
if config.verbose:
if config_info[0] and config.verbose:
print(f"{config_info[0]} used for file {filename}")

file_config = Config(**config_info[1])
if config_info[0]:
file_config = Config(**config_info[1])

with io.File.read(filename) as source_file:
actual_file_path = file_path or source_file.path
Expand Down
2 changes: 2 additions & 0 deletions isort/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -1141,6 +1141,7 @@ def main(argv: Sequence[str] | None = None, stdin: TextIOWrapper | None = None)
show_diff=show_diff,
write_to_stdout=write_to_stdout,
extension=ext_format,
disregard_skip=not resolve_all_configs,
config_trie=config_trie,
),
file_names,
Expand All @@ -1156,6 +1157,7 @@ def main(argv: Sequence[str] | None = None, stdin: TextIOWrapper | None = None)
show_diff=show_diff,
write_to_stdout=write_to_stdout,
extension=ext_format,
disregard_skip=not resolve_all_configs,
config_trie=config_trie,
)
for file_name in file_names
Expand Down
2 changes: 1 addition & 1 deletion isort/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -774,7 +774,7 @@ def find_all_configs(path: str) -> Trie:
Parses and stores any config file encountered in a trie and returns the root of
the trie
"""
trie_root = Trie("default", {})
trie_root = Trie("default", {}, root_path=path)

for dirpath, _, _ in os.walk(path):
for config_file_name in CONFIG_SOURCES:
Expand Down
17 changes: 15 additions & 2 deletions isort/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,14 @@ class Trie:
associated with each file
"""

def __init__(self, config_file: str = "", config_data: dict[str, Any] | None = None) -> None:
def __init__(
self,
config_file: str = "",
config_data: dict[str, Any] | None = None,
root_path: str = "",
) -> None:
self.root: TrieNode = TrieNode(config_file, config_data)
self.root_path: Path | None = Path(root_path).resolve() if root_path else None

def insert(self, config_file: str, config_data: dict[str, Any]) -> None:
resolved_config_path_as_tuple = Path(config_file).parent.resolve().parts
Expand All @@ -41,7 +47,14 @@ def search(self, filename: str) -> tuple[str, dict[str, Any]]:
Returns the closest config relative to filename by doing a depth
first search on the prefix tree.
"""
resolved_file_path_as_tuple = Path(filename).resolve().parts
resolved_file_path = Path(filename).resolve()
if self.root_path:
try:
resolved_file_path.relative_to(self.root_path)
except ValueError:
return ("", {})

resolved_file_path_as_tuple = resolved_file_path.parts

temp = self.root

Expand Down
16 changes: 16 additions & 0 deletions tests/unit/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,22 @@ def test_missing_default_section(tmpdir):
main.main([str(python_file)])


def test_resolve_all_configs_honors_skip_for_direct_file(tmp_path, capsys):
config_file = tmp_path / "pyproject.toml"
config_file.write_text("[tool.isort]\nextend_skip = ['a.py']\n")
python_file = tmp_path / "a.py"
original_content = "import sys\nimport os\n"
python_file.write_text(original_content)

main.main([str(python_file), "--resolve-all-configs", "--verbose"])

out, error = capsys.readouterr()
assert not error
assert "default used for file" not in out
assert "Skipped 1 files" in out
assert python_file.read_text() == original_content


def test_ran_against_root():
with pytest.raises(SystemExit):
main.main(["/"])
Expand Down
Loading