Forward ignore_extra to nested key/value types in CheckedPMap.create (fixes #326)#327
Open
Sanjays2402 wants to merge 1 commit into
Open
Forward ignore_extra to nested key/value types in CheckedPMap.create (fixes #326)#327Sanjays2402 wants to merge 1 commit into
Sanjays2402 wants to merge 1 commit into
Conversation
CheckedPMap.create(source_data, ignore_extra=True) did not pass
ignore_extra down to the nested checked key/value type .create() calls,
so an extra field in a nested checked value (or key) still raised
AttributeError even though ignore_extra=True was requested:
class Rec(PRecord):
a = field()
class M(CheckedPMap):
__key_type__ = str
__value_type__ = Rec
M.create({'k': {'a': 1, 'extra': 2}}, ignore_extra=True)
# AttributeError: 'extra' is not among the specified fields for Rec
Root cause: in CheckedPMap.create the recursive
checked_key_type.create(key) / checked_value_type.create(value) calls
omit ignore_extra. The sibling helper _checked_type_create used by
CheckedPVector / CheckedPSet already threads ignore_extra through, so
CheckedPMap was the only collection dropping it.
Fix: pass ignore_extra=ignore_extra to both the key and value
create() calls, mirroring _checked_type_create. Behavior with the
default ignore_extra=False is unchanged (extra fields still raise).
Added regression tests covering ignore_extra threading to the value
type and to the key type (via a hashable pmap raw key that is converted
through KeyRecord.create), plus a test asserting the default still
raises. The two ignore_extra=True tests fail without this change and
pass with it.
Fixes tobgu#326
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.
Summary
CheckedPMap.create(source_data, ignore_extra=True)does not forwardignore_extrato the nested checked key/value.create()calls, so an extra field in a nested checked value (or key) still raisesAttributeErroreven thoughignore_extra=Truewas explicitly requested.Fixes #326.
Reproduction (before)
The same call succeeds for the sibling collections
CheckedPVector/CheckedPSet, which honorignore_extra:Root cause
CheckedPMap.create(inpyrsistent/_checked_types.py) recursively converts nested data viachecked_key_type.create(key)/checked_value_type.create(value)but omitsignore_extra:The sibling helper
_checked_type_create(used byCheckedPVector/CheckedPSetviacreate = classmethod(_checked_type_create)) already threads it through:So
CheckedPMapwas the only checked collection dropping the flag.Fix
Pass
ignore_extra=ignore_extrato both the key and valuecreate()calls, mirroring_checked_type_create. Behavior with the defaultignore_extra=Falseis unchanged — extra fields still raise.After
Tests
Added three regression tests to
tests/checked_map_test.py:test_create_ignore_extra_forwarded_to_value_type— extra field in a nested checked value.test_create_ignore_extra_forwarded_to_key_type— extra field in a nested checked key (using a hashablepmapraw key that is converted throughKeyRecord.create, so the key.create()path is actually exercised).test_create_ignore_extra_false_still_raises— the defaultignore_extra=Falsestill raisesAttributeError.The two
ignore_extra=Truetests were verified to fail without the source change and pass with it (source-file stash proof):Full suite:
640 passed, 1 skipped(no regressions).Diff is +42/−3 (2 logical source lines + 3 tests).