fix(core): Fix incorrect type hints. (#5427)

This commit is contained in:
-LAN-
2024-06-20 15:16:21 +08:00
committed by GitHub
parent e4259a8f13
commit 23fa3dedc4
11 changed files with 166 additions and 117 deletions

View File

@@ -5,11 +5,7 @@ from types import ModuleType
from typing import AnyStr
def import_module_from_source(
module_name: str,
py_file_path: AnyStr,
use_lazy_loader: bool = False
) -> ModuleType:
def import_module_from_source(*, module_name: str, py_file_path: AnyStr, use_lazy_loader: bool = False) -> ModuleType:
"""
Importing a module from the source file directly
"""
@@ -17,9 +13,13 @@ def import_module_from_source(
existed_spec = importlib.util.find_spec(module_name)
if existed_spec:
spec = existed_spec
if not spec.loader:
raise Exception(f"Failed to load module {module_name} from {py_file_path}")
else:
# Refer to: https://docs.python.org/3/library/importlib.html#importing-a-source-file-directly
spec = importlib.util.spec_from_file_location(module_name, py_file_path)
if not spec or not spec.loader:
raise Exception(f"Failed to load module {module_name} from {py_file_path}")
if use_lazy_loader:
# Refer to: https://docs.python.org/3/library/importlib.html#implementing-lazy-imports
spec.loader = importlib.util.LazyLoader(spec.loader)
@@ -29,7 +29,7 @@ def import_module_from_source(
spec.loader.exec_module(module)
return module
except Exception as e:
logging.exception(f'Failed to load module {module_name} from {py_file_path}: {str(e)}')
logging.exception(f"Failed to load module {module_name} from {py_file_path}: {str(e)}")
raise e
@@ -43,15 +43,14 @@ def get_subclasses_from_module(mod: ModuleType, parent_type: type) -> list[type]
def load_single_subclass_from_source(
module_name: str,
script_path: AnyStr,
parent_type: type,
use_lazy_loader: bool = False,
*, module_name: str, script_path: AnyStr, parent_type: type, use_lazy_loader: bool = False
) -> type:
"""
Load a single subclass from the source
"""
module = import_module_from_source(module_name, script_path, use_lazy_loader)
module = import_module_from_source(
module_name=module_name, py_file_path=script_path, use_lazy_loader=use_lazy_loader
)
subclasses = get_subclasses_from_module(module, parent_type)
match len(subclasses):
case 1:

View File

@@ -1,15 +1,12 @@
import os
from collections import OrderedDict
from collections.abc import Callable
from typing import Any, AnyStr
from typing import Any
from core.tools.utils.yaml_utils import load_yaml_file
def get_position_map(
folder_path: AnyStr,
file_name: str = '_position.yaml',
) -> dict[str, int]:
def get_position_map(folder_path: str, *, file_name: str = "_position.yaml") -> dict[str, int]:
"""
Get the mapping from name to index from a YAML file
:param folder_path: