是否可以在运行时获取 typing.List 的类型参数?

Is it possible to get the type paramater of a typing.List at runtime?

假设我有一个 typing.List[str] 对象,是否可以在运行时从类型提示对象中获取类型参数?我想要一个本质上 return str class 的字段或方法。

我做了一些挖掘,虽然没有记录或官方,但使用 __args__ 作品。

>>> Union[str, bool].__args__
(<class 'str'>, <class 'bool'>)
>>> List[str].__args__
(<class 'str'>,)

出于某种原因,一个 int 和一个 bool 的联合只是一个 int,我不敢在这个模块的混乱中进一步挖掘。

>>> Union[int, bool]
<class 'int'>
>>> Union[bool, int]
<class 'int'>