升级 marshmallow,运行 pytest 的时候有大量 warning 怎么快速修改正确? - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
featureoverload
V2EX    Python

升级 marshmallow,运行 pytest 的时候有大量 warning 怎么快速修改正确?

  •  
  •   featureoverload 2021-12-07 14:08:12 +08:00 2136 次点击
    这是一个创建于 1406 天前的主题,其中的信息可能已经有所发展或是发生改变。

    新版 marshmallow 将 Field 中,非保留的关键参数都建议放入到 metadata 参数中, 没有放入的会显示 warning 。

    示例代码:

    # schemas.py from marshmallow import Schema from marshmallow import fields class FooSchema(Schema): name = fields.String( required=True, title='foo', description='foo name') relatiOnship= fields.String( title='bar', description='foobar') 

    测试代码:

    from schemas import FooSchema def test_warn(): INPUT = { 'name': 'something', 'relationship': 'others' } schema = FooSchema() data = schema.load(INPUT) assert data == INPUT 

    示例代码文件结构:

    . ├── requirements.txt # marshmallow==3.3.0 ├── schemas.py └── test_warning.py 

    升级 marshmallow:

    $ pip install -U marshmallow ... Successfully installed marshmallow-3.14.1 

    使用 pytest 测试(出现 marshmallow warning ):

    pytest test_warning.py ================================================================ test session starts ================================================================= platform darwin -- Python 3.8.3, pytest-6.2.5, py-1.11.0, pluggy-1.0.0 rootdir: /private/tmp/_ collected 1 item test_warning.py . [100%] ================================================================== warnings summary ================================================================== .venv/lib/python3.8/site-packages/marshmallow/fields.py:218 /private/tmp/_/.venv/lib/python3.8/site-packages/marshmallow/fields.py:218: RemovedInMarshmallow4Warning: Passing field metadata as keyword arguments is deprecated. Use the explicit `metadata=...` argument instead. Additional metadata: {'title': 'foo', 'description': 'foo name'} warnings.warn( .venv/lib/python3.8/site-packages/marshmallow/fields.py:218 /private/tmp/_/.venv/lib/python3.8/site-packages/marshmallow/fields.py:218: RemovedInMarshmallow4Warning: Passing field metadata as keyword arguments is deprecated. Use the explicit `metadata=...` argument instead. Additional metadata: {'title': 'bar', 'description': 'foobar'} warnings.warn( -- Docs: https://docs.pytest.org/en/stable/warnings.html =========================================================== 1 passed, 2 warnings in 0.03s ============================================================ 

    修改为没有 warning 的代码:

    ... class FooSchema(Schema): name = fields.String( required=True, metadata={'title': 'foo', 'description': 'foo name'}) relatiOnship= fields.String( metadata={'title': 'bar', 'description': 'foobar'}) 

    问题:

    如果有很多个 fields.XXXFieldYYY(...) 代码, 一个一个手动修改起来太难了, 有没有什么快捷的方案一次性改好?

    1 条回复    2021-12-08 15:27:17 +08:00
    SmiteChow
        1
    SmiteChow  
       2021-12-08 15:27:17 +08:00
    有 Monkey patch

    marshmallow/fields.py:218

    的相关逻辑,使其不再执行
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     1258 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 23ms UTC 23:40 PVG 07:40 LAX 16:40 JFK 19:40
    Do have faith in what you're doing.
    ubao snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86