deprecated_call: 是一个上下文管理器,可确保代码块触发 DeprecationWarning 或者 PendingDeprecationWarning; 源码:
deprecated_call 可以将函数当做参数传入,后面跟上参数。
import warnings
def api_call_v2():
warnings.warn('use v3 of this api', DeprecationWarning)
return 200
def test_deprecated_call():
with deprecated_call():
assert api_call_v2() == 200
import warnings
from pytest import deprecated_call
def api_call_v2(*args, **kwargs):
warnings.warn('use v3 of this api', DeprecationWarning)
return kwargs.get("b")
def test_deprecated_call():
assert deprecated_call(api_call_v2, 123, 456, a=1, b=2, c=3) == 2
这个函数最主要的其实是将被标记了 DeprecationWarning 或者 PendingDeprecationWarning 的函数能够正常被调用,不会在执行时出现 相应的 warning 信息。
直接调用,弹出警告:
使用 deprecated_call,没有警告显示:
说明:本篇参考官网并加入自己些许理解翻译而来,觉得有用,可以点赞和赞赏哦(^ v ^),谢谢支持;如果有不足地方,可留言评论。后续将继续更新。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有