Python UnionType类型提示中如何可靠地检查子成员类型?(类型.可靠.成员.提示.检查...)

wufei1232025-03-08python3

python uniontype类型提示中如何可靠地检查子成员类型?

深入解析Python UnionType子成员类型检查

本文探讨如何在Python中可靠地检查函数参数类型提示中是否包含特定类型,尤其是在使用UnionType时。这对于静态代码分析和类型检查至关重要。

直接检查简单类型(如int、str)比较容易,但UnionType(例如int | str)则需要更细致的处理。UnionType表示参数可接受多种类型,简单的in操作符或迭代无法直接判断其是否包含特定类型。

例如,函数get_score的name参数类型提示为str | None,inspect.signature返回的annotation为types.UnionType。我们需要确定该UnionType是否包含str类型。

关键在于使用typing.get_args函数。它能获取泛型类型的参数。对于UnionType,它返回一个包含所有子类型的元组。因此,我们可以这样判断UnionType是否包含str类型:

from typing import Union, Callable, get_args
from types import UnionType
from inspect import signature
from loguru import logger

def check_func_args_hints(func: Callable) -> bool:
    for param in signature(func).parameters.values():
        if param.name != 'name':
            continue

        logger.debug(f"Annotation: {param.annotation}")
        logger.debug(f"Annotation Type: {type(param.annotation)}")
        if isinstance(param.annotation, UnionType):
            if str in get_args(param.annotation):
                logger.info("name parameter supports str")
                return True
            else:
                logger.info("name parameter does not support str")
                return False
        elif param.annotation is str:  # 使用 is 而不是 == 进行类型比较
            logger.info("name parameter supports str")
            return True
        else:
            logger.info("name parameter does not support str")
            return False

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

check_func_args_hints(get_score)

def another_func(name: int):
    pass

check_func_args_hints(another_func)

代码首先使用signature获取函数参数信息,然后检查参数名是否为name。如果是,则检查annotation的类型。如果是UnionType,则使用get_args获取子类型并判断是否包含str。否则,直接判断annotation是否为str。(代码已优化,使用 is 替代 == 进行类型比较,更准确)

另一种方法是使用isinstance:

isinstance("", param.annotation)

这种方法简洁,但可靠性取决于UnionType的定义和isinstance的实现,可能不如typing.get_args可靠。因此,typing.get_args是更推荐的方法。

以上就是Python UnionType类型提示中如何可靠地检查子成员类型?的详细内容,更多请关注知识资源分享宝库其它相关文章!

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。