Fix map and groupby filters ignoring default=None for missing attributes#2174
Closed
amanv0007 wants to merge 1 commit into
Closed
Fix map and groupby filters ignoring default=None for missing attributes#2174amanv0007 wants to merge 1 commit into
map and groupby filters ignoring default=None for missing attributes#2174amanv0007 wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #2165.
Problem
When using
map(attribute="foo", default=None)on a list of objects that lack thefooattribute, the filter returnsUndefinedinstead ofNone. This happens becausemake_attrgetterusesdefault is not Noneto decide whether to substituteUndefinedvalues, making it impossible to distinguish "no default was given" from "the user explicitly passeddefault=None".The same issue affects
groupbywhich also passesdefaultthrough tomake_attrgetter.Fix
Introduce a module-level
_MISSINGsentinel object and use it as the default value for thedefaultparameter inmake_attrgetter,prepare_map, and thegroupbyfilter functions. The guard becomesdefault is not _MISSINGinstead ofdefault is not None, allowingNoneto be a valid user-supplied default.Testing
[{}] | map(attribute="foo", default=None)now correctly yields[None]instead of[Undefined].default=0,default=False, anddefault="bar"still work correctly.defaultentirely still returnsUndefined(unchanged behavior).