在 Python 3.6 中在运行时根据联合类型检查变量 [英] Check a variable against Union type at runtime in Python 3.6

查看:30
本文介绍了在 Python 3.6 中在运行时根据联合类型检查变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个函数装饰器,它使用 Python 3.6 类型提示来检查参数字典是否遵守类型提示,如果没有引发错误并清楚地描述问题,以用于 HTTP API.

问题在于,当函数具有使用 Union 类型的参数时,我无法在运行时根据它检查变量.

比如我有这个功能

from 打字 import Uniondef bark(myname: str, descr: Union[int, str], mynum: int = 3) ->字符串:返回 descr + myname * mynum

我能做到:

isinstance('Arnold', bark.__annotations__['myname'])

但不是:

isinstance(3, bark.__annotations__['descr'])

因为 Union 不能与 isinstanceissubclass 一起使用.

我找不到使用类型对象检查它的方法.我试图自己实现检查,但是 bark.__annotations__['descr'] 在 REPL 中显示为 typing.Union[int, str] 我不能在运行时访问类型列表,如果不使用检查 bark.__annotations__['descr'].__repr__() 的丑陋技巧.

是否有正确的方法来访问这些信息?还是故意在运行时不容易访问?

解决方案

你可以使用 Union__args__ 属性,它包含一个 tuple可能的内容:

<预><代码>>>>从打字进口联盟>>>x = 联合 [int, str]>>>x.__参数__(整数,字符串)>>>isinstance(3, x.__args__)真的>>>isinstance('a', x.__args__)真的

__args__ 参数没有记录,所以它可以被认为是搞乱了实现细节",但它似乎比解析 repr 更好.

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.

The problem is that when the function has a parameter using the Union type I can't check a variable against it at runtime.

For example, I have this function

from typing import Union
def bark(myname: str, descr: Union[int, str], mynum: int = 3) -> str:
    return descr + myname * mynum

I can do:

isinstance('Arnold', bark.__annotations__['myname'])

But not:

isinstance(3, bark.__annotations__['descr'])

Because Union cannot be used with isinstance or issubclass.

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?

解决方案

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

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 中在运行时根据联合类型检查变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆