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
14 changes: 9 additions & 5 deletions src/jinja2/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,15 @@ def ignore_case(value: V) -> V:
return value


_MISSING = object()
"""Sentinel to distinguish 'no default given' from ``default=None``."""


def make_attrgetter(
environment: "Environment",
attribute: str | int | None,
postprocess: t.Callable[[t.Any], t.Any] | None = None,
default: t.Any | None = None,
default: t.Any = _MISSING,
) -> t.Callable[[t.Any], t.Any]:
"""Returns a callable that looks up the given attribute from a
passed object with the rules of the environment. Dots are allowed
Expand All @@ -72,7 +76,7 @@ def attrgetter(item: t.Any) -> t.Any:
for part in parts:
item = environment.getitem(item, part)

if default is not None and isinstance(item, Undefined):
if default is not _MISSING and isinstance(item, Undefined):
item = default

if postprocess is not None:
Expand Down Expand Up @@ -1199,7 +1203,7 @@ def sync_do_groupby(
environment: "Environment",
value: "t.Iterable[V]",
attribute: str | int,
default: t.Any | None = None,
default: t.Any = _MISSING,
case_sensitive: bool = False,
) -> "list[_GroupTuple]":
"""Group a sequence of objects by an attribute using Python's
Expand Down Expand Up @@ -1283,7 +1287,7 @@ async def do_groupby(
environment: "Environment",
value: "t.AsyncIterable[V] | t.Iterable[V]",
attribute: str | int,
default: t.Any | None = None,
default: t.Any = _MISSING,
case_sensitive: bool = False,
) -> "list[_GroupTuple]":
expr = make_attrgetter(
Expand Down Expand Up @@ -1720,7 +1724,7 @@ def prepare_map(
) -> t.Callable[[t.Any], t.Any]:
if not args and "attribute" in kwargs:
attribute = kwargs.pop("attribute")
default = kwargs.pop("default", None)
default = kwargs.pop("default", _MISSING)

if kwargs:
raise FilterArgumentError(
Expand Down
Loading