@@ -901,12 +901,14 @@ def make_formate_toml(repo_path: pathlib.Path, templates: Environment) -> List[s
901
901
:param templates:
902
902
"""
903
903
904
+ known_first_party = set ()
904
905
known_third_party = set ()
905
906
906
907
isort_file = PathPlus (repo_path / ".isort.cfg" )
907
908
formate_file = PathPlus (repo_path / "formate.toml" )
908
909
909
910
isort_config = get_isort_config (repo_path , templates )
911
+ known_first_party .update (isort_config ["known_first_party" ])
910
912
known_third_party .update (isort_config ["known_third_party" ])
911
913
912
914
if formate_file .is_file ():
@@ -919,13 +921,20 @@ def make_formate_toml(repo_path: pathlib.Path, templates: Environment) -> List[s
919
921
isort = ConfigUpdater ()
920
922
isort .read (str (isort_file ))
921
923
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 ))
924
932
925
933
isort_file .unlink (missing_ok = True )
926
934
927
935
if "hooks" in formate_config and "isort" in formate_config ["hooks" ]:
928
936
if "kwargs" in formate_config ["hooks" ]["isort" ]:
937
+ known_first_party .update (formate_config ["hooks" ]["isort" ]["kwargs" ].get ("known_first_party" , ()))
929
938
known_third_party .update (formate_config ["hooks" ]["isort" ]["kwargs" ].get ("known_third_party" , ()))
930
939
931
940
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
935
944
def normalise_underscore (name : str ) -> str :
936
945
return normalize (name .strip ()).replace ('-' , '_' )
937
946
947
+ isort_config ["known_first_party" ] = sorted (set (filter (bool , map (normalise_underscore , known_first_party ))))
938
948
isort_config ["known_third_party" ] = sorted (set (filter (bool , map (normalise_underscore , known_third_party ))))
939
949
940
950
hooks = {
@@ -1002,7 +1012,7 @@ def get_isort_config(repo_path: pathlib.Path, templates: Environment) -> Dict[st
1002
1012
1003
1013
known_third_party = [req .replace ('-' , '_' ) for req in sorted (all_requirements )]
1004
1014
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" ] ]
1006
1016
1007
1017
return isort
1008
1018
0 commit comments