Skip to content

Fix map and groupby filters ignoring default=None for missing attributes#2174

Closed
amanv0007 wants to merge 1 commit into
pallets:mainfrom
amanv0007:fix-map-default-none
Closed

Fix map and groupby filters ignoring default=None for missing attributes#2174
amanv0007 wants to merge 1 commit into
pallets:mainfrom
amanv0007:fix-map-default-none

Conversation

@amanv0007

Copy link
Copy Markdown

Fixes #2165.

Problem

When using map(attribute="foo", default=None) on a list of objects that lack the foo attribute, the filter returns Undefined instead of None. This happens because make_attrgetter uses default is not None to decide whether to substitute Undefined values, making it impossible to distinguish "no default was given" from "the user explicitly passed default=None".

The same issue affects groupby which also passes default through to make_attrgetter.

Fix

Introduce a module-level _MISSING sentinel object and use it as the default value for the default parameter in make_attrgetter, prepare_map, and the groupby filter functions. The guard becomes default is not _MISSING instead of default is not None, allowing None to be a valid user-supplied default.

Testing

  • All 911 existing tests pass without modification.
    • Manually verified that [{}] | map(attribute="foo", default=None) now correctly yields [None] instead of [Undefined].
    • Verified that default=0, default=False, and default="bar" still work correctly.
    • Verified that omitting default entirely still returns Undefined (unchanged behavior).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[{}] | map(attribute = "foo", default = None) still fails

2 participants