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
When I pass a List[str] with the string being 2 characters long (e.g. ["ab"]) to a field which is annotated with Union[Dict[str, str], List[Any]], I get a dictionary {"a": "b"} instead of ["ab"].
values={'L': '1'} # Unexpected - why do I get a dict?
values=['L11'] # expected value, but surprising after seeing output (1)
values=['L1'] # expected value, but surprising after seeing output (1)
The text was updated successfully, but these errors were encountered:
Hello @MartinThoma
Yes it's a very common issue on pydantic as it tries to coerce the value with types inside the Unionin order (so the first one will take precedence over the second one if possible).
So here it first tries to coerce ["L1"] with Dict[str, str], which works because dict(["11"]) == {'1': '1'}.
I'm working on a way to prevent this in #2092. The whole behaviour will change in v2.
Please have a look at linked issues to this PR if you want more information
Checks
Versions
Output of
python -c "import pydantic.utils; print(pydantic.utils.version_info())"
:Bug
When I pass a List[str] with the string being 2 characters long (e.g.
["ab"]
) to a field which is annotated withUnion[Dict[str, str], List[Any]]
, I get a dictionary{"a": "b"}
instead of["ab"]
.Example:
Results in:
The text was updated successfully, but these errors were encountered: