首页 > 编程语言 >python: enforcing type check on function using decorator

python: enforcing type check on function using decorator

时间:2023-06-15 20:22:09浏览次数:36  
标签:function return ty python enforcing args bound kwargs type

 

  def typeassert(*ty_args, **ty_kwargs):
        """
        利用装饰器对函数参数强制性类型检查
        enforcing type check on function using decorator
        :param ty_args:
        :param ty_kwargs:
        :return:
        """
        def decorate(func):
            # If in optimized mode, disable type checking
            if not __debug__:
                return func

            # Map function argument names to supplied types
            sig = signature(func)
            bound_types = sig.bind_partial(*ty_args, **ty_kwargs).arguments

            @wraps(func)
            def wrapper(*args, **kwargs):
                bound_values = sig.bind(*args, **kwargs)
                # Enforce type assertions across supplied arguments
                for name, value in bound_values.arguments.items():
                    if name in bound_types:
                        if not isinstance(value, bound_types[name]):
                            raise TypeError(
                                'Argument {} must be {}'.format(name, bound_types[name])
                            )
                return func(*args, **kwargs)

            return wrapper

        return decorate

    @typeassert(int, z=int)
    def spam(x, y, z=42):
        """

        :param y:
        :param z:
        :return:
        """
        print(x, y, z)

#https://github.com/yidao620c/python3-cookbook/blob/master/source/c09/p07_enforcing_type_check_on_function_using_decorator.rst

  

标签:function,return,ty,python,enforcing,args,bound,kwargs,type
From: https://www.cnblogs.com/geovindu/p/17484038.html

相关文章

  • Python下载安装
    Python下载的官网:WelcometoPython.org      会出现四个python文件将这个拖到桌面查看Python是否安装好两种方式:1.IDLE编辑器 2.使用运行对话框下载PychamPycham官网:下载PyCharm:JetBrains为专业开发者提供的PythonIDE 安装成功首......
  • 通过python封装接口获取淘宝商品页面数据、淘宝商品详情数据
    可以使用GET或POST方法,请求参数中应包含商品详情页面数据、标题、价格、图片、库存、销量等信息。解析返回的response中的HTML页面或JSON格式数据,提取需要的商品信息,如商品标题、价格、评价人数等。使用pandas库将提取的商品信息保存到数据框中,以方便后续处理和分析。......
  • python2安装mysqldb
     yuminstallmariadb-devel 或yuminstallmysql-devel 编辑_mysql.c注释2005行 ......
  • 基于python制作的做题软件
    启动文件主界面.py数据库格式IDTopicABCDFinishOrNotRightOrErrorRight_Answer目前User.py跟questions.py文件没有使用到目前的功能数据库存取问题数据错题保存以及读取随机获得问题重置所有题的状态软件截图工程链接https://gitee.com/song-min......
  • python篇-工业相机学习
    1,抠出屏的图importcv2fromPILimportImagedefgetCoordinate(img):rectangle=[]gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)#灰度图ret,binary=cv2.threshold(gray,0,255,cv2.THRESH_BINARY|cv2.THRESH_OTSU)#二值化element3=......
  • 轻松掌握Python+主流测试框架Requests接口自动化,快速转型自动化测试
    轻松掌握Python+主流测试框架Requests接口自动化,快速转型自动化测试最近几年,自动化测试已经成为了软件测试的主流趋势,而Python语言和Requests库作为主流测试框架,也成为了越来越多测试工程师的首选。使用Python+Requests接口自动化进行测试,不仅可以提高测试效率和覆盖面,还可以降低......
  • python篇:在编程过程中遇到的工具问题记录
    1,用pipinstallopencv-python安装cv2后,发现pycharm中importcv2不报错,但是cv2不能点出相关函数   1>使用pipuninstallopencv-python命令,卸载了通过pip安装的cv2包   2>在https://www.lfd.uci.edu/~gohlke/pythonlibs/下载对应的安装包,例如我的python是3.8,电脑是6......
  • 软件测试从小白进阶高手-Python自动化+Jmeter性能+App项目+接口测试
    软件测试从小白进阶高手-Python自动化+Jmeter性能+App项目+接口测试软件测试技能,包括Python自动化、Jmeter性能测试、App项目测试、接口测试。接下来,我将从每个技能点给出一些更详细的介绍。1.Python自动化测试Python已经成为测试人员的新宠,Python自动化测试的原因在于Python......
  • 数据结构(python版)—— 2、前期知识与算法分析
    从C转到python(一)C:helloWorld!#include<stdio.h>​intmain(){//sayhelloprintf("HelloWorld!\n")}1-Compile编译到机器码2-Link与各种库链接3-Execute执行目标程序Python:HelloWorld!defmain():#sayhelloprint("HelloWorld!"......
  • 「Python实用秘技14」快速优化Python导包顺序
    本文完整示例代码及文件已上传至我的Github仓库https://github.com/CNFeffery/PythonPracticalSkills这是我的系列文章「Python实用秘技」的第14期,本系列立足于笔者日常工作中使用Python积累的心得体会,每一期为大家带来一个几分钟内就可学会的简单小技巧。作为系列第1......