The "preserve the type of the input whenever possible" union handling is inconsistent · Issue #7097 · pydantic/pydantic · GitHub
More Web Proxy on the site http://driver.im/
You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Code that relies on the strict left-to-right behavior of union validation/parsing is broken in V2, with no available workaround. Not only that, but the subtle change creates inconsistencies where identical data in python or JSON form parses differently. In the example code, all of those would have parsed as UUIDs in V1 (behavior that our code relies on), but now, it parses as string in certain scenarios, but not in others.
It is difficult to migrate with this highly inconsistent and frankly baffling behavior. Using unions like the one in the example with FastAPI as path parameters or query parameters used to be a useful way to detect UUIDs, but now, FastAPI always returns a string.
Possibly related: my work in pydantic/pydantic-core#867 will improve the smart unions a bit further. I'd propose that a better behaviour here with "smart unions" would be that this validates as a str in both cases , because the input is a str. I think this should be the case once my PR merges.
I would support adding a way to get back to "dumb" left-to-right validation, even if the smart behaviour remains the default. If there isn't a known way already (@samuelcolvin@dmontagu@adriangb ?), I'd be tempted to add smart_union back to Field, but give it a default of True this time.
Uh oh!
There was an error while loading. Please reload this page.
Initial Checks
Description
Code that relies on the strict left-to-right behavior of union validation/parsing is broken in V2, with no available workaround. Not only that, but the subtle change creates inconsistencies where identical data in python or JSON form parses differently. In the example code, all of those would have parsed as UUIDs in V1 (behavior that our code relies on), but now, it parses as string in certain scenarios, but not in others.
It is difficult to migrate with this highly inconsistent and frankly baffling behavior. Using unions like the one in the example with FastAPI as path parameters or query parameters used to be a useful way to detect UUIDs, but now, FastAPI always returns a string.
Example Code
Python, Pydantic & OS Version
Selected Assignee: @dmontagu
The text was updated successfully, but these errors were encountered: