Skip to content

Fix color parsing when TypeError - #312

Merged
yezz123 merged 1 commit into
pydantic:mainfrom
irtimir:fix-color-parsing
Apr 18, 2025
Merged

yezz123 merged 1 commit into
pydantic:mainfrom
irtimir:fix-color-parsing

Conversation

@irtimir

@irtimir irtimir commented Apr 18, 2025

Copy link
Copy Markdown
Contributor

Fixes: #311

@yezz123
yezz123 merged commit f85bc18 into pydantic:main Apr 18, 2025
yezz123 pushed a commit that referenced this pull request Jul 18, 2026
…reject (#409)

#312 widened `except ValueError` to `except (ValueError, TypeError)` in
color.py to fix #311 ("Unhandled validation error when parsing Color"),
because float() raises TypeError -- not ValueError -- for a value whose
*type* it cannot take at all. Two call sites with the identical construct
were not updated:

  Something(ulid=1.5)      -> TypeError: object of type 'float' has no len()
  Color((0, 0, 0, []))     -> TypeError: float() argument must be a string...

TypeError is not a subclass of ValueError, so neither except clause fires
and the error escapes validate_python raw.

- ulid.py:66     _validate_ulid is a wrap validator, so it sees the raw
                 value before the inner union_schema can reject it; the
                 else branch hands it to _ULID.from_bytes.
- color.py:391   parse_float_alpha's own docstring says it raises
                 PydanticCustomError "if the input value cannot be
                 successfully parsed as a float"; a list cannot be.

Same one-line widening as #312 in both, reusing each site's existing error.

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
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.

Unhandled validation error when parsing Color

2 participants