Check a variable against Union type at runtime in Python 3.6(在 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.
我找不到使用类型对象检查它的方法.我尝试自己实施检查,但是当 bark.__annotations__['descr'] 在 REPL 中显示为 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 中运行时根据联合类型检查变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 Python 3.6 中运行时根据联合类型检查变量
基础教程推荐
- 在 Python 中将货币解析为数字 2022-01-01
- kivy 应用程序中的一个简单网页作为小部件 2022-01-01
- Kivy 使用 opencv.调整图像大小 2022-01-01
- Python 中是否有任何支持将长字符串转储为块文字或折叠块的 yaml 库? 2022-01-01
- matplotlib 设置 yaxis 标签大小 2022-01-01
- 在 Django Admin 中使用内联 OneToOneField 2022-01-01
- 对多索引数据帧的列进行排序 2022-01-01
- Python,确定字符串是否应转换为 Int 或 Float 2022-01-01
- 比较两个文本文件以找出差异并将它们输出到新的文本文件 2022-01-01
- 究竟什么是“容器"?在蟒蛇?(以及所有的 python 容器类型是什么?) 2022-01-01
