在Python 3.6中运行时根据Union类型检查变量 [英] Check a variable against Union type at runtime in Python 3.6
问题描述
我正在尝试编写一个使用Python 3.6类型提示的函数装饰器,以检查参数字典是否尊重类型提示,并且如果未引发错误并带有对问题的清晰描述,则将其用于HTTP API.
I'm trying to write a function decorator that uses Python 3.6 type hints to check that a dictionary of arguments respects the type hints and if not raise an error with a clear description of the problem, to be used for HTTP APIs.
问题在于,当函数具有使用Union
类型的参数时,我无法在运行时针对该变量检查变量.
The problem is that when the function has a parameter using the Union
type I can't check a variable against it at runtime.
例如,我有此功能
from typing import Union
def bark(myname: str, descr: Union[int, str], mynum: int = 3) -> str:
return descr + myname * mynum
我可以做到:
isinstance('Arnold', bark.__annotations__['myname'])
但不是:
isinstance(3, bark.__annotations__['descr'])
因为Union
不能与isinstance
或issubclass
一起使用.
Because Union
cannot be used with isinstance
or issubclass
.
我找不到使用类型对象检查它的方法.
我尝试自己执行检查,但是在REPL中bark.__annotations__['descr']
显示为typing.Union[int, str]
时,如果不使用检查bark.__annotations__['descr'].__repr__()
的丑陋技巧,我将无法在运行时访问类型列表.
I couldn't find a way to check it using the type object.
I tried to implement the check by myself but while bark.__annotations__['descr']
is shown as typing.Union[int, str]
in the REPL I can't access the list of the types at runtime, if not using the ugly hack of examining bark.__annotations__['descr'].__repr__()
.
是否有访问此信息的正确方法?还是故意在运行时不容易访问它?
Is there a proper way to access this information? Or is it deliberately intended to not be easily accessible at runtime?
推荐答案
您可以使用Union
的__args__
属性,该属性包含tuple
可能的内容:
You could use the __args__
attribute of Union
which holds a tuple
of the "possible contents:
>>> from typing import Union
>>> x = Union[int, str]
>>> x.__args__
(int, str)
>>> isinstance(3, x.__args__)
True
>>> isinstance('a', x.__args__)
True
未记录__args__
参数,因此可以将其视为与实现详细信息打交道",但它似乎是比解析repr
更好的方法.
The __args__
argument is not documented so it could be considered "messing with implementation details" but it seems like a better way than parsing the repr
.
这篇关于在Python 3.6中运行时根据Union类型检查变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!