【问题标题】:Passing field metadata as keyword arguments is deprecated不推荐将字段元数据作为关键字参数传递
【发布时间】:2021-12-03 03:25:39
【问题描述】:

在 pytest 测试执行期间,我注意到一个警告,指示如下:

 /usr/local/lib/python3.6/site-packages/marshmallow/fields.py:222: RemovedInMarshmallow4Warning: Passing field metadata as keyword arguments is deprecated. Use the explicit `metadata=...` argument instead. Additional metadata: {'places': 0}
    RemovedInMarshmallow4Warning,

module test_get_record_not_found
  /usr/local/lib/python3.6/site-packages/flask/json/__init__.py:211: DeprecationWarning: Importing 'itsdangerous.json' is deprecated and will be removed in ItsDangerous 2.1. Use Python's 'json' module instead.
    rv = _json.dumps(obj, **kwargs)

测试是简单的get contact not found:

def test_get_record_not_found(test_client,db_session):
    get_resp = test_client.get(
        f'/record/0')
    get_data = json.loads(get_resp.data.decode())

    assert get_resp.status_code == 404
    assert 'not found' in get_data['message']

你知道我怎样才能摆脱上述警告吗?

谢谢

【问题讨论】:

    标签: python marshmallow


    【解决方案1】:

    我们无法从 OP 看出,您需要显示更多代码。

    从错误消息中可以看出,您的代码中一定有一些地方可以定义这样的架构:

    class MySchema(ma.Schema):
        fields = ma.fields.Field(places=0)
    

    你应该把它改成:

    class MySchema(ma.Schema):
        fields = ma.fields.Field(metadata={"places":0})
    

    假设这确实是您想要做的。在构建模式时,这可能是一个错误。如果模式是动态构建的,我们需要知道如何构建。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-09-24
      • 1970-01-01
      • 1970-01-01
      • 2017-08-01
      • 2018-11-14
      • 1970-01-01
      相关资源
      最近更新 更多