astuple: forward filter and recurse into attrs nested in dict values#1577
Open
HrachShah wants to merge 4 commits into
Open
astuple: forward filter and recurse into attrs nested in dict values#1577HrachShah wants to merge 4 commits into
HrachShah wants to merge 4 commits into
Conversation
os.environb yields bytes on POSIX and the documented use case
for to_bool is reading values out of environment variables. The
truthy and falsy lookup tuples only contain str and int, so a
bytes('true') input raised 'Cannot convert value to bool: b\'true\''
even though its ASCII-decoded value is the canonical truthy
literal.
Decode bytes and bytearray as ASCII before the lowercase + lookup
step, matching the str path. Non-ASCII bytes raise ValueError
('Cannot convert value to bool: ...') the same way an unknown
string does, since the decoded text would still not match any
known literal. Unknown byte values raise the same ValueError.
The dict-handling branch in attrs.astuple recursively serialized attrs instances found as keys and values but only passed tuple_factory and retain_collection_types, dropping both recurse and filter. A user filter that was meant to drop fields from a nested attrs class therefore had no effect when the instance lived inside a dict, while the parallel list branch a few lines above did forward both. Pass recurse=True and filter=filter to the recursive astuple calls so the documented filter contract holds for dicts as well, and add a regression test in TestAsTuple covering dict and OrderedDict containers with a filter that drops a field on the nested attrs class.
for more information, see https://pre-commit.ci
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
attrs.astuple has a dict-handling branch that recursively serializes attrs instances found as keys and values. The recursive call only forwarded tuple_factory and retain_collection_types, silently dropping both recurse and filter. As a result, a user-supplied filter that was meant to drop fields from a nested attrs class had no effect when the instance lived inside a dict, while the parallel list branch a few lines above did forward both.
Fix
Pass recurse=True and filter=filter to the two recursive astuple calls in the dict branch of astuple.
Test
Added TestAsTuple.test_dicts_filter_applies_to_values parametrized over dict and OrderedDict. The test asserts that a filter dropping the 'a' attribute of an inner attrs class still drops it when the inner instance is nested as a dict value. Without the fix, the test fails because the inner 'a' field survives into the resulting tuple.
All 55 tests in tests/test_funcs.py pass. The unrelated tests/test_converters.py::TestPipe::test_wrapped_annotation failure exists on main already and is not caused by this change.