how to batch modify metadata arguments in Field() due to RemovedInMarshmallow4Warning after upgrade marshmallow

Viewed 348

after upgrade marshmallow, the non-reserved arguments(e.g. title, description) should passing in by metadata argument.

otherwise, it will pop RemovedInMarshmallow4Warning.

what if I have a lot of Field() call expression, how can I batch modify it quickly?

example code:

# 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')

test code(test case):

from schemas import FooSchema


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

files structure:

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

upgrade marshmallow and duplicate this problem:

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

$ 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 ===================

the expected code without warning:

...

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