Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 9 additions & 23 deletions src/marshmallow/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,6 @@ class Field(FieldABC):
Replace ``missing`` and ``default`` parameters with ``load_default`` and ``dump_default``.
"""

# Some fields, such as Method fields and Function fields, are not expected
# to exist as attributes on the objects to serialize. Set this to False
# for those fields
_CHECK_ATTRIBUTE = True

#: Default error messages for various kinds of errors. The keys in this dictionary
#: are passed to `Field.make_error`. The values are error messages passed to
#: :exc:`marshmallow.exceptions.ValidationError`.
Expand Down Expand Up @@ -300,15 +295,12 @@ def serialize(
:param accessor: Function used to access values from ``obj``.
:param kwargs: Field-specific keyword arguments.
"""
if self._CHECK_ATTRIBUTE:
value = self.get_value(obj, attr, accessor=accessor)
if value is missing_:
default = self.dump_default
value = default() if callable(default) else default
if value is missing_:
return value
else:
value = None
value = self.get_value(obj, attr, accessor=accessor)
if value is missing_:
default = self.dump_default
value = default() if callable(default) else default
if value is missing_:
return value
return self._serialize(value, attr, obj, **kwargs)

def deserialize(
Expand Down Expand Up @@ -1870,8 +1862,6 @@ class Method(Field):
Removed ``method_name`` parameter.
"""

_CHECK_ATTRIBUTE = False

def __init__(
self,
serialize: str | None = None,
Expand Down Expand Up @@ -1900,7 +1890,7 @@ def _bind_to_schema(self, field_name, parent):

super()._bind_to_schema(field_name, parent)

def _serialize(self, value, attr, obj, **kwargs):
def get_value(self, obj, attr, accessor=None, default=missing_):
if self._serialize_method is not None:
return self._serialize_method(obj)
return missing_
Expand Down Expand Up @@ -1934,8 +1924,6 @@ class Function(Field):
Removed ``func`` parameter.
"""

_CHECK_ATTRIBUTE = False

def __init__(
self,
serialize: (
Expand All @@ -1957,7 +1945,7 @@ def __init__(
self.serialize_func = serialize and utils.callable_or_raise(serialize)
self.deserialize_func = deserialize and utils.callable_or_raise(deserialize)

def _serialize(self, value, attr, obj, **kwargs):
def get_value(self, obj, attr, accessor=None, default=missing_):
return self._call_or_raise(self.serialize_func, obj, attr)

def _deserialize(self, value, attr, data, **kwargs):
Expand All @@ -1982,15 +1970,13 @@ class Constant(Field):
:param constant: The constant to return for the field attribute.
"""

_CHECK_ATTRIBUTE = False

def __init__(self, constant: typing.Any, **kwargs: Unpack[_BaseFieldKwargs]):
super().__init__(**kwargs)
self.constant = constant
self.load_default = constant
self.dump_default = constant

def _serialize(self, value, *args, **kwargs):
def get_value(self, obj, attr, accessor=None, default=missing_):
return self.constant

def _deserialize(self, value, *args, **kwargs):
Expand Down
Loading