Add __repr__ to Invalid so it surfaces the path and error type (#358) - #546
Merged
Merged
Conversation
Invalid.__str__ includes the path (and error_type), but there was no
__repr__, so repr() fell back to the default and showed only the bare
message -- e.g. TypeInvalid('expected int') instead of the path-qualified
message. This made the errors in a MultipleInvalid uninformative when
inspected or logged. Add a __repr__ that surfaces the same context str
does, matching the existing MultipleInvalid.__repr__. Fixes alecthomas#358.
alecthomas
approved these changes
Jul 4, 2026
alecthomas
approved these changes
Jul 4, 2026
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 #358.
Problem
Invalid.__str__produces an informative message including the path and error type:But
Invalidhas no__repr__, sorepr()falls back to the default exception repr and drops that context:This matters in practice:
MultipleInvalid.__repr__renders its.errorslist, so inspecting or logging aMultipleInvalidshows a list of uninformative sub-error reprs.Fix
Give
Invalida__repr__that surfaces the same context__str__already does — matching the__repr__thatMultipleInvalidalready defines:Result:
No validation behavior changes — only the repr of the error objects.
Testing
test_invalid_repr_includes_path_and_type, asserting the repr includes the path/type and that it surfaces inside theMultipleInvalidrepr. It fails onmaster(TypeInvalid('expected int')) and passes with this change.170 passed).blackandflake8are clean on the changed files.README.md:93) fails on this machine under Python 3.14; it fails identically without this change (the tox matrix is 3.9–3.12) and is untouched here.Disclosure: this change was prepared with the assistance of an AI tool (Claude Code). I verified the behavior, added and ran the test, ran the suite and linters, and take responsibility for the contribution and will respond to review feedback personally.