fix: include index/key context in deep_iterable and deep_mapping errors#1554
Open
lihan3238 wants to merge 8 commits intopython-attrs:mainfrom
Open
fix: include index/key context in deep_iterable and deep_mapping errors#1554lihan3238 wants to merge 8 commits intopython-attrs:mainfrom
lihan3238 wants to merge 8 commits intopython-attrs:mainfrom
Conversation
…r messages When inner validators fail in deep_iterable or deep_mapping, the error message now includes the index or key of the failing element instead of just the parent attribute name. Before: "'x' must be <class 'str'> (got 123)" After: "'x[1]' must be <class 'str'> (got 123)" Fixes python-attrs#1245
str(Exception) returns the repr of all args when there are multiple, which corrupted the error message. Use e.args[0] to get just the message and reconstruct with (new_msg,) + e.args[1:] to preserve the original args structure. Also update test_bad_exception_args to expect the improved error message format with index context.
for more information, see https://pre-commit.ci
for more information, see https://pre-commit.ci
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.
Fixes #1245 (also referenced in #1206)
Summary
When inner validators fail in
deep_iterableordeep_mapping, the error message now includes the index or key of the failing element instead of just the parent attribute name.Before
After
Changes
_DeepIterable.__call__: Wrap member validator call in try/except, replace attribute name with indexed name (x->x[1])_DeepMapping.__call__: Same for key and value validator calls, using key repr (m->m['key'])Test
Verified with:
deep_iterable+instance_of(str): error showsx[1]instead ofxdeep_mapping+instance_of(int)values: error showsm['b']instead ofm