Skip to main content
 首页 » 编程设计

python之如何使棉花糖在序列化时引发 AttributeErrors

2025年01月19日8lovecherry

当数据缺少属性时,如何让 marshmallow make 引发异常?

考虑以下示例:

In [8]: import marshmallow 
 
In [9]: class Foo(marshmallow.Schema): 
   ...:     bar = marshmallow.fields.Str(required=True) 
   ...: 
 
In [10]: class Bar: 
    ...:     pass 
    ...: 
 
In [11]: bar = Bar() 
 
In [12]: Foo().dumps(bar) 
Out[12]: MarshalResult(data='{}', errors={}) 
 
In [13]: bar.bar 
--------------------------------------------------------------------------- 
AttributeError                            Traceback (most recent call last) 
<ipython-input-13-6901e83d9f0c> in <module>() 
----> 1 bar.bar 
 
AttributeError: 'Bar' object has no attribute 'bar' 

更糟糕的是,这是:

In [14]: foo.loads(foo.dumps(bar).data) 
Out[14]: UnmarshalResult(data={}, errors={'bar': ['Missing data for required field.']}) 

请您参考如下方法:

以下引自此issue解释您在问题的第一部分中描述的行为。

This is by design. Primarily for performance reasons, validation only happens on deserialization (load and validate). The data passed to dump is assumed to be valid.

期望是,如果您需要验证,例如,您将显式调用 validate()从你的例子:

In [10]: foo.validate(foo.dump(foo).data) 
--------------------------------------------------------------------------- 
ValidationError                           Traceback (most recent call last) 
<ipython-input-40-4dfb5988e928> in <module>() 
----> 1 foo.validate(foo.dump(foo).data) 
 
... 
 
ValidationError: {'bar': ['Missing data for required field.']} 

关于在反序列化过程中缺少验证,对于您正在使用的 marshmallow 版本,您必须明确声明架构为“严格”,以便自动进行验证。在您的示例中,这可以通过以下方式完成:

In [22]: class Foo(marshmallow.Schema): 
    ...:     bar = marshmallow.fields.Str(required=True) 
    ...:     class Meta: 
    ...:         strict = True 
 
In [23]: foo = Foo() 
 
In [24]: foo.loads(foo.dumps(bar).data) 
--------------------------------------------------------------------------- 
... 
ValidationError: {'bar': ['Missing data for required field.']} 

如 Issue 598 中所述,此行为已更改,最新版本的 marshamallow 默认为此“严格”行为;它还删除了 MarshalResult/UnmarshalResult 包装器。