Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add __repr__ to Invalid so it surfaces the path and error type (#358) - #546

Merged
alecthomas merged 1 commit into
alecthomas:masterfrom
apoorvdarshan:invalid-repr-path
Jul 5, 2026
Merged

Add __repr__ to Invalid so it surfaces the path and error type (#358)#546
alecthomas merged 1 commit into
alecthomas:masterfrom
apoorvdarshan:invalid-repr-path

Conversation

@apoorvdarshan

Copy link
Copy Markdown
Contributor

Fixes #358.

Problem

Invalid.__str__ produces an informative message including the path and error type:

expected int for dictionary value @ data['k']

But Invalid has no __repr__, so repr() falls back to the default exception repr and drops that context:

>>> repr(err)
TypeInvalid('expected int')     # path and error_type lost

This matters in practice: MultipleInvalid.__repr__ renders its .errors list, so inspecting or logging a MultipleInvalid shows a list of uninformative sub-error reprs.

Fix

Give Invalid a __repr__ that surfaces the same context __str__ already does — matching the __repr__ that MultipleInvalid already defines:

def __repr__(self) -> str:
    return '%s(%r)' % (self.__class__.__name__, str(self))

Result:

>>> repr(err)
TypeInvalid("expected int for dictionary value @ data['k']")
>>> repr(multiple)
MultipleInvalid([TypeInvalid("expected int for dictionary value @ data['k']")])

No validation behavior changes — only the repr of the error objects.

Testing

  • Added test_invalid_repr_includes_path_and_type, asserting the repr includes the path/type and that it surfaces inside the MultipleInvalid repr. It fails on master (TypeInvalid('expected int')) and passes with this change.
  • Full suite passes (170 passed). black and flake8 are clean on the changed files.
  • One unrelated doctest (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.

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
alecthomas merged commit 6cf9450 into alecthomas:master Jul 5, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ValueInvalid has default repr, which contains less information than str

2 participants