Skip to content

Commit 5e9502b

Browse files
committed
Preserve changes to known_first_party too
1 parent 45d97e8 commit 5e9502b

File tree

4 files changed

+16
-6
lines changed

4 files changed

+16
-6
lines changed

repo_helper/files/testing.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -901,12 +901,14 @@ def make_formate_toml(repo_path: pathlib.Path, templates: Environment) -> List[s
901901
:param templates:
902902
"""
903903

904+
known_first_party = set()
904905
known_third_party = set()
905906

906907
isort_file = PathPlus(repo_path / ".isort.cfg")
907908
formate_file = PathPlus(repo_path / "formate.toml")
908909

909910
isort_config = get_isort_config(repo_path, templates)
911+
known_first_party.update(isort_config["known_first_party"])
910912
known_third_party.update(isort_config["known_third_party"])
911913

912914
if formate_file.is_file():
@@ -919,13 +921,20 @@ def make_formate_toml(repo_path: pathlib.Path, templates: Environment) -> List[s
919921
isort = ConfigUpdater()
920922
isort.read(str(isort_file))
921923

922-
if "settings" in isort.sections() and "known_third_party" in isort["settings"]:
923-
known_third_party.update(re.split(r"(\n|,\s*)", isort["settings"]["known_third_party"].value))
924+
if "settings" in isort.sections():
925+
if "known_first_party" in isort["settings"]:
926+
existing_known_first_party = isort["settings"]["known_first_party"].value
927+
if isinstance(existing_known_first_party, str):
928+
existing_known_first_party = [existing_known_first_party]
929+
known_first_party.update(re.split(r"(\n|,\s*)", existing_known_first_party))
930+
if "known_third_party" in isort["settings"]:
931+
known_third_party.update(re.split(r"(\n|,\s*)", isort["settings"]["known_third_party"].value))
924932

925933
isort_file.unlink(missing_ok=True)
926934

927935
if "hooks" in formate_config and "isort" in formate_config["hooks"]:
928936
if "kwargs" in formate_config["hooks"]["isort"]:
937+
known_first_party.update(formate_config["hooks"]["isort"]["kwargs"].get("known_first_party", ()))
929938
known_third_party.update(formate_config["hooks"]["isort"]["kwargs"].get("known_third_party", ()))
930939

931940
for existing_key, value in formate_config["hooks"]["isort"]["kwargs"].items():
@@ -935,6 +944,7 @@ def make_formate_toml(repo_path: pathlib.Path, templates: Environment) -> List[s
935944
def normalise_underscore(name: str) -> str:
936945
return normalize(name.strip()).replace('-', '_')
937946

947+
isort_config["known_first_party"] = sorted(set(filter(bool, map(normalise_underscore, known_first_party))))
938948
isort_config["known_third_party"] = sorted(set(filter(bool, map(normalise_underscore, known_third_party))))
939949

940950
hooks = {
@@ -1002,7 +1012,7 @@ def get_isort_config(repo_path: pathlib.Path, templates: Environment) -> Dict[st
10021012

10031013
known_third_party = [req.replace('-', '_') for req in sorted(all_requirements)]
10041014
isort["known_third_party"] = known_third_party
1005-
isort["known_first_party"] = templates.globals["import_name"]
1015+
isort["known_first_party"] = [templates.globals["import_name"]]
10061016

10071017
return isort
10081018

tests/test_files/test_testing_/test_make_formate_toml_case_1.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,4 @@ known_third_party = [
4646
"tox",
4747
"wheel",
4848
]
49-
known_first_party = "hello_world"
49+
known_first_party = [ "hello_world",]

tests/test_files/test_testing_/test_make_formate_toml_case_2.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,4 @@ known_third_party = [
4747
"tox",
4848
"wheel",
4949
]
50-
known_first_party = "hello_world"
50+
known_first_party = [ "hello_world",]

tests/test_files/test_testing_/test_make_formate_toml_case_3.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,4 @@ known_third_party = [
4747
"tox",
4848
"wheel",
4949
]
50-
known_first_party = "hello_world"
50+
known_first_party = [ "hello_world",]

0 commit comments

Comments
 (0)