可以使用 inspect 模块的 signature 方法来实现

示例代码如下:

from typing import Union
from types import UnionType
from inspect import signature
from loguru import logger


def get_score(name: str | None = None) -> float | None:
    pass


typed_signature = signature(get_score).parameters.items()


for index, (parameter_name, parameter) in enumerate(typed_signature):
    logger.debug(parameter.default)

输出

2022-10-04 18:59:53.893 | DEBUG    | __main__:<module>:15 - None

universe_king
3.4k 声望680 粉丝