Skip to content
Open
Show file tree
Hide file tree
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
11 changes: 3 additions & 8 deletions src/marshmallow/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,10 +259,7 @@ def get_value(
self,
obj: typing.Any,
attr: str,
accessor: (
typing.Callable[[typing.Any, str, typing.Any], typing.Any] | None
) = None,
default: typing.Any = missing_,
accessor: (typing.Callable[[typing.Any, str], typing.Any] | None) = None,
) -> _InternalT:
"""Return the value for a given key from an object.

Expand All @@ -273,7 +270,7 @@ def get_value(
"""
accessor_func = accessor or utils.get_value
check_key = attr if self.attribute is None else self.attribute
return accessor_func(obj, check_key, default)
return accessor_func(obj, check_key)

def _validate(self, value: typing.Any) -> None:
"""Perform validation on ``value``. Raise a :exc:`ValidationError` if validation
Expand Down Expand Up @@ -315,9 +312,7 @@ def serialize(
self,
attr: str,
obj: typing.Any,
accessor: (
typing.Callable[[typing.Any, str, typing.Any], typing.Any] | None
) = None,
accessor: (typing.Callable[[typing.Any, str], typing.Any] | None) = None,
**kwargs,
):
"""Pulls the value for the given key from the object, applies the
Expand Down
4 changes: 2 additions & 2 deletions src/marshmallow/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -497,13 +497,13 @@ def handle_error(
Receives `many` and `partial` (on deserialization) as keyword arguments.
"""

def get_attribute(self, obj: typing.Any, attr: str, default: typing.Any):
def get_attribute(self, obj: typing.Any, attr: str):
"""Defines how to pull values from an object to serialize.

.. versionchanged:: 3.0.0a1
Changed position of ``obj`` and ``attr``.
"""
return get_value(obj, attr, default)
return get_value(obj, attr)

##### Serialization/Deserialization API #####

Expand Down
12 changes: 6 additions & 6 deletions src/marshmallow/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def pluck(dictlist: list[dict[str, typing.Any]], key: str):
# Various utilities for pulling keyed values from objects


def get_value(obj, key: str, default=missing):
def get_value(obj, key: str):
"""Helper for pulling a keyed value off various types of objects. Fields use
this method by default to access attributes of the source object. For object `x`
and attribute `i`, this method first tries to access `x[i]`, and then falls back to
Expand All @@ -107,20 +107,20 @@ def get_value(obj, key: str, default=missing):
"""
if "." in key:
for k in key.split("."):
obj = _get_value_for_key(obj, k, default)
obj = _get_value_for_key(obj, k)
else:
obj = _get_value_for_key(obj, key, default)
obj = _get_value_for_key(obj, key)
return obj


def _get_value_for_key(obj, key, default):
def _get_value_for_key(obj, key):
if not hasattr(obj, "__getitem__"):
return getattr(obj, key, default)
return getattr(obj, key, missing)

try:
return obj[key]
except (KeyError, IndexError, TypeError, AttributeError):
return getattr(obj, key, default)
return getattr(obj, key, missing)


def set_value(dct: dict[str, typing.Any], key: str, value: typing.Any):
Expand Down
12 changes: 6 additions & 6 deletions tests/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -2274,8 +2274,8 @@ class SerializerD(SerializerB, SerializerC):
assert SerializerD._declared_fields == expected


def get_from_dict(schema, obj, key, default=None):
return obj.get("_" + key, default)
def get_from_dict(schema, obj, key):
return obj.get("_" + key)


class TestGetAttribute:
Expand All @@ -2284,8 +2284,8 @@ class UserDictSchema(Schema):
name = fields.Str()
email = fields.Email()

def get_attribute(self, obj, attr, default):
return get_from_dict(self, obj, attr, default)
def get_attribute(self, obj, attr):
return get_from_dict(self, obj, attr)

user_dict = {"_name": "joe", "_email": "joe@shmoe.com"}
schema = UserDictSchema()
Expand All @@ -2302,8 +2302,8 @@ class UserDictSchema(Schema):
name = fields.Str()
email = fields.Email()

def get_attribute(self, obj, attr, default):
return get_from_dict(self, obj, attr, default)
def get_attribute(self, obj, attr):
return get_from_dict(self, obj, attr)

user_dicts = [
{"_name": "joe", "_email": "joe@shmoe.com"},
Expand Down
10 changes: 5 additions & 5 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ def test_get_value_from_object(obj):
assert utils.get_value(obj, "y") == 42


def test_get_value_from_namedtuple_with_default():
def test_get_value_from_namedtuple():
p = PointNT(x=42, y=None)
# Default is only returned if key is not found
assert utils.get_value(p, "z", default=123) == 123
# since 'y' is an attribute, None is returned instead of the default
assert utils.get_value(p, "y", default=123) is None
# missing is only returned if key is not found
assert utils.get_value(p, "z") == utils.missing
# since 'y' is an attribute, None is returned instead of missing
assert utils.get_value(p, "y") is None


class Triangle:
Expand Down