如果传递给带注释的函数的参数类型与指定的类型不匹配,是否有可能使 python 抛出错误? [英] Is it possible to make python throw errors if the type of the argument passed to the annotated function doesn't match the one specified?

查看:48
本文介绍了如果传递给带注释的函数的参数类型与指定的类型不匹配,是否有可能使 python 抛出错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

python3.5 的新特性之一是类型提示.例如下面的代码现在有效:

One of the new features in python3.5 is type hinting. For example the code below is valid now:

def greeting(name: str) -> str:
    return 'Hello ' + name

但是,据我了解,它本身不会检查任何内容,并且其解释方式也与此完全相同:

But, as I understand, it doesn't check anything by itself and also is interpreted absolutely the same way as this:

def greeting(name):
    return 'Hello ' + name

并且主要是为了帮助静态分析器(并使代码更易于理解)而实现的.但是,当无效类型的参数传递给具有带注释参数类型的函数时,是否有(或计划在未来实现)任何方式(可能通过使用某些第三方库)使 python 抛出错误(仅使用类型提示语法)?

and was implemented mostly to help static analyzers (and to make code that is easier to understand). But is there (or is planned to be implemented in the future) any way (maybe by using some third-party libraries) to make python throw errors when an argument of an invalid type is passed to a function with annotated argument types (using only type-hinting syntax)?

推荐答案

类型提示实现 PEP 0484 明确列为非目标:

Type hints implement PEP 0484 which explicitly lists as a non-goal:

虽然提议的打字模块将包含一些构建块运行时类型检查——特别是 get_type_hints() 函数-- 必须开发第三方包来实现特定的运行时类型检查功能,例如使用装饰器或元类.使用类型提示提高性能优化留给读者作为练习.

While the proposed typing module will contain some building blocks for runtime type checking -- in particular the get_type_hints() function -- third party packages would have to be developed to implement specific runtime type checking functionality, for example using decorators or metaclasses. Using type hints for performance optimizations is left as an exercise for the reader.

由此看来,Python 开发人员似乎没有计划添加您所寻求的功能.引用提到了装饰器,这似乎是要走的路.从概念上讲,它看起来很简单——装饰器将在要装饰的函数上使用 get_type_hints() 并遍历参数,根据任何提示检查它们的类型,如果有冲突则抛出错误或者只是将参数传递给函数.这将类似于 pzelasko 的答案,但装饰器使用提示来自动处理样板代码.最简单的方法是简单地审查参数,但您也应该能够制作一个装饰器,如果返回类型与提示冲突,则会引发错误.我还没有 Python 3.5,也没有时间去学习它——但对于想要学习装饰器和类型提示的人来说,这似乎是一个很好的学习练习.也许您可以成为 PEP 所指的第三方"之一.

From this it seems to follow that the Python developers have no plan to add the functionality that you seek. The quote mentions decorators and that does seem the way to go. In concept it seems straightforward -- the decorator would use get_type_hints() on the function to be decorated and would iterate through the arguments, checking their types against any hints, either throwing an error if there is a clash or simply passing on the arguments to the function. This would be similar to pzelasko's answer but with the decorator using the hints to automatically handle the boiler-plate code. The simplest approach would be to simply vet the arguments, though you should also be able to make a decorator which would raise an error if the return type clashes with the hint. I don't yet have Python 3.5 and don't have the time to pursue it -- but it seems like a good learning exercise for someone who wants to learn about both decorators and type hints. Perhaps you can be one of the "third parties" the PEP alludes to.

这篇关于如果传递给带注释的函数的参数类型与指定的类型不匹配,是否有可能使 python 抛出错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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