From 7c491dfcb1448b102598f0e3e8e0468de6077cc6 Mon Sep 17 00:00:00 2001 From: Amandeep vishwkarma Date: Mon, 18 May 2026 20:43:07 +0530 Subject: [PATCH] Fix map/groupby filters ignoring default=None for missing attributes --- src/jinja2/filters.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/jinja2/filters.py b/src/jinja2/filters.py index c46e20c10..a1864b71c 100644 --- a/src/jinja2/filters.py +++ b/src/jinja2/filters.py @@ -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 @@ -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: @@ -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 @@ -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( @@ -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(