Skip to content

Pass the target type to format_exception for standalone values - #834

Closed
Aschenbath wants to merge 1 commit into
python-attrs:mainfrom
Aschenbath:fix/401-primitive-type-notes
Closed

Aschenbath wants to merge 1 commit into
python-attrs:mainfrom
Aschenbath:fix/401-primitive-type-notes

Conversation

@Aschenbath

Copy link
Copy Markdown

With detailed validation, failures inside classes and iterables carry AttributeValidationNote/IterableValidationNotes, so transform_error can hand the target type to format_exception. A failure structuring a standalone value — the common case being a primitive like int at the top level — carried nothing, so the formatter received None and produced a bare "invalid value" with no type.

  • New cattrs.ValueValidationNote, attached by BaseConverter.structure when detailed validation is on and a failure escapes without a more specific note and is not a grouped validation error.
  • transform_error now reads that note in its standalone branch and passes the type to the formatter: converter.structure("x", int) now yields "invalid value for type, expected int @ $" (was "invalid value @ $"). Nested cases are unchanged.
  • format_exception's "expected an iterable" special case for TypeErrors now applies whenever the message matches, not only when no type is known. Previously a List[int] field given a non-iterable said "expected List" while the top-level equivalent said "expected an iterable"; the two now agree.

Tests: three new tests in tests/test_v.py (default formatter output for int/float, a custom formatter receiving the type, and no note without detailed validation). The full test suite passes on this machine (1050 tests across tests/ and tests/strategies/); the only exceptions are three version-gated modules (test_frozendicts_315.py, test_sentinels_315.py, test_gen_dict_649.py) that fail to import on Python 3.12 on unmodified upstream as well.

Fixes #401

@Aschenbath

Copy link
Copy Markdown
Author

Closing this as a duplicate — #832, opened a little earlier, implements the same change (passing the target type to format_exception for standalone values). Apologies for the noise.

@Aschenbath Aschenbath closed this Nov 4, 2026
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.

Pass type information to format_exception from transform_errors for primitive types

1 participant