如何获取 python uniontype 的子成员
当您使用类型提示来表示具有多个可能的类型的函数参数时,uniontype 类型将用于指示这些可能的类型。处理 uniontype 可能会很棘手,因为无法直接检查其中是否包含特定的类型。
要获取 uniontype 中的子成员,您可以执行以下操作:
from typing import union, callable from inspect import signature def check_type(parameter): return typing.get_args(parameter.annotation)
if isinstance("", parameter.annotation): pass
通过结合这两种方法,您可以获取 uniontype 的子成员并确定特定类型是否包含在其中。