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

Support for Multiple configs #1820

Merged
merged 18 commits into from
Oct 15, 2021
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
RCorrect deepsource errors
anirudnits committed Oct 3, 2021
commit 2f2bc56485e986b8ddd245f97c91cadea6e5573d
4 changes: 2 additions & 2 deletions isort/api.py
Original file line number Diff line number Diff line change
@@ -328,7 +328,7 @@ def check_file(
if "config_trie" in config_kwargs:
config_trie = config_kwargs.pop("config_trie", None)
if config_trie:
config_info = config_trie._search(filename)
config_info = config_trie.search(filename)
if config.verbose:
print(f"{config_info[0]} used for file {filename}")

@@ -396,7 +396,7 @@ def sort_file(
if "config_trie" in config_kwargs:
config_trie = config_kwargs.pop("config_trie", None)
if config_trie:
config_info = config_trie._search(filename)
config_info = config_trie.search(filename)
if config.verbose:
print(f"{config_info[0]} used for file {filename}")

2 changes: 1 addition & 1 deletion isort/settings.py
Original file line number Diff line number Diff line change
@@ -807,7 +807,7 @@ def find_all_configs(src_paths: Tuple[str]) -> Trie:
config_data = {}

if config_data:
trie_root._insert(potential_config_file, config_data)
trie_root.insert(potential_config_file, config_data)
break

return trie_root
4 changes: 2 additions & 2 deletions isort/utils.py
Original file line number Diff line number Diff line change
@@ -22,7 +22,7 @@ class Trie:
def __init__(self, config_file: str = "", config_data: Optional[Dict[str, Any]] = None) -> None:
self.root: TrieNode = TrieNode(config_file, config_data)

def _insert(self, config_file: str, config_data: Dict[str, Any]) -> None:
def insert(self, config_file: str, config_data: Dict[str, Any]) -> None:
resolved_config_path_as_tuple = Path(config_file).parent.resolve().parts

temp = self.root
@@ -35,7 +35,7 @@ def _insert(self, config_file: str, config_data: Dict[str, Any]) -> None:

temp.config_info = (config_file, config_data)

def _search(self, filename: str) -> Tuple[str, Dict[str, Any]]:
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.
8 changes: 4 additions & 4 deletions tests/unit/test_settings.py
Original file line number Diff line number Diff line change
@@ -274,17 +274,17 @@ def test_find_all_configs(tmpdir):

config_trie = settings.find_all_configs((str(tmpdir),))

config_info_1 = config_trie._search(str(dir1 / "test1.py"))
config_info_1 = config_trie.search(str(dir1 / "test1.py"))
assert config_info_1[0] == str(setup_cfg_file)
assert "profile" in config_info_1[1] and config_info_1[1]["profile"] == "django"

config_info_2 = config_trie._search(str(dir2 / "test2.py"))
config_info_2 = config_trie.search(str(dir2 / "test2.py"))
assert config_info_2[0] == str(pyproject_toml_file)
assert "profile" in config_info_2[1] and config_info_2[1]["profile"] == "hug"

config_info_3 = config_trie._search(str(dir3 / "test3.py"))
config_info_3 = config_trie.search(str(dir3 / "test3.py"))
assert config_info_3[0] == str(isort_cfg_file)
assert "profile" in config_info_3[1] and config_info_3[1]["profile"] == "black"

config_info_4 = config_trie._search(str(tmpdir / "file4.py"))
config_info_4 = config_trie.search(str(tmpdir / "file4.py"))
assert config_info_4[0] == "default"
20 changes: 10 additions & 10 deletions tests/unit/test_utils.py
Original file line number Diff line number Diff line change
@@ -4,35 +4,35 @@
def test_trie():
trie_root = Trie("default", {"line_length": 70})

trie_root._insert("/temp/config1/.isort.cfg", {"line_length": 71})
trie_root._insert("/temp/config2/setup.cfg", {"line_length": 72})
trie_root._insert("/temp/config3/pyproject.toml", {"line_length": 73})
trie_root.insert("/temp/config1/.isort.cfg", {"line_length": 71})
trie_root.insert("/temp/config2/setup.cfg", {"line_length": 72})
trie_root.insert("/temp/config3/pyproject.toml", {"line_length": 73})

# Ensure that appropriate configs are resolved for files in different directories
config1 = trie_root._search("/temp/config1/subdir/file1.py")
config1 = trie_root.search("/temp/config1/subdir/file1.py")
assert config1[0] == "/temp/config1/.isort.cfg"
assert config1[1] == {"line_length": 71}

config1_2 = trie_root._search("/temp/config1/file1_2.py")
config1_2 = trie_root.search("/temp/config1/file1_2.py")
assert config1_2[0] == "/temp/config1/.isort.cfg"
assert config1_2[1] == {"line_length": 71}

config2 = trie_root._search("/temp/config2/subdir/subsubdir/file2.py")
config2 = trie_root.search("/temp/config2/subdir/subsubdir/file2.py")
assert config2[0] == "/temp/config2/setup.cfg"
assert config2[1] == {"line_length": 72}

config2_2 = trie_root._search("/temp/config2/subdir/file2_2.py")
config2_2 = trie_root.search("/temp/config2/subdir/file2_2.py")
assert config2_2[0] == "/temp/config2/setup.cfg"
assert config2_2[1] == {"line_length": 72}

config3 = trie_root._search("/temp/config3/subdir/subsubdir/subsubsubdir/file3.py")
config3 = trie_root.search("/temp/config3/subdir/subsubdir/subsubsubdir/file3.py")
assert config3[0] == "/temp/config3/pyproject.toml"
assert config3[1] == {"line_length": 73}

config3_2 = trie_root._search("/temp/config3/file3.py")
config3_2 = trie_root.search("/temp/config3/file3.py")
assert config3_2[0] == "/temp/config3/pyproject.toml"
assert config3_2[1] == {"line_length": 73}

config_outside = trie_root._search("/temp/file.py")
config_outside = trie_root.search("/temp/file.py")
assert config_outside[0] == "default"
assert config_outside[1] == {"line_length": 70}