fix `ImportFromModule` is unhashable issue (#3362)

* ImportFromModule is unhashable
This fix makes the conversion to string prior to the deduplication to avoid this issue

* add type annotation for global_imports

* meet code formatting check

---------

Co-authored-by: zcipod <zcipod@gmail.com>
Co-authored-by: Li Jiang <bnujli@gmail.com>
This commit is contained in:
zcipod 2024-08-26 17:31:36 +10:00 committed by GitHub
parent 34b34d0203
commit 3aa0528a12
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 4 additions and 4 deletions

View File

@ -6,7 +6,7 @@ import inspect
from dataclasses import dataclass, field
from importlib.abc import SourceLoader
from textwrap import dedent, indent
from typing import Any, Callable, Generic, List, TypeVar, Union
from typing import Any, Callable, Generic, List, Set, TypeVar, Union
from typing_extensions import ParamSpec
@ -159,12 +159,12 @@ def _build_python_functions_file(
funcs: List[Union[FunctionWithRequirements[Any, P], Callable[..., Any], FunctionWithRequirementsStr]]
) -> str:
# First collect all global imports
global_imports = set()
global_imports: Set[str] = set()
for func in funcs:
if isinstance(func, (FunctionWithRequirements, FunctionWithRequirementsStr)):
global_imports.update(func.global_imports)
global_imports.update(map(_import_to_str, func.global_imports))
content = "\n".join(map(_import_to_str, global_imports)) + "\n\n"
content = "\n".join(global_imports) + "\n\n"
for func in funcs:
content += _to_code(func) + "\n\n"