在 Python 3.6 中运行时根据联合类型检查变量

Check a variable against Union type at runtime in Python 3.6(在 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 不能与 isinstanceissubclass 一起使用.

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

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

How do I make a list of all members in a discord server using discord.py?(如何使用 discord.py 列出不和谐服务器中的所有成员?)
how to change discord.py bot activity(如何更改 discord.py 机器人活动)
Issues with getting VoiceChannel.members and Guild.members to return a full list(让 VoiceChannel.members 和 Guild.members 返回完整列表的问题)
Add button components to a message (discord.py)(将按钮组件添加到消息(discord.py))
on_message() and @bot.command issue(on_message() 和@bot.command 问题)
How to edit a message in discord.py(如何在 discord.py 中编辑消息)