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

2021-12-07 14:08:12 +08:00
 featureoverload

新版 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(...) 代码, 一个一个手动修改起来太难了, 有没有什么快捷的方案一次性改好?

1743 次点击
所在节点    Python
1 条回复
SmiteChow
2021-12-08 15:27:17 +08:00
有 Monkey patch

marshmallow/fields.py:218

的相关逻辑,使其不再执行

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/820602

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX