-
Notifications
You must be signed in to change notification settings - Fork 22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
BUG: Order matters in Union type hints? #285
Comments
Can smart union help resolve this? https://docs.pydantic.dev/1.10/usage/model_config/#smart-union |
I am out of office until the end of the week and have no access to computer. I wonder if it behaves the same with v0.0.19 and Pydantic v2, which performs strict type checking by default. Also, what is the error message? |
Upgrading to pydantic v2 does seem to alleviate the issue. There was no error message per-se, rather validation errors.
|
The error message is generated by this code: bluesky-queueserver/bluesky_queueserver/manager/profile_ops.py Lines 2426 to 2428 in a6d9b48
The problem with Pydantic 1 is that it is focused on data conversion and does not do strict type checking. As long as data can be converted to the target type, Pydantic 1 is happy. For example, if a parameter type is It may be possible to improve the code by implementing strict type checking with Pydantic 1 (using custom validators?), but I am not sure it is worth working on it considering that this is the default behavior of Pydantic 2 . |
I'd advocate for at least pinning pydantic > 2 if you choose not to address this for pydantic 1. If an environment somehow solves for pydantic 1 (as ours does currently), this behavior would still be present and very problematic. |
I think pinning is a good idea, but I would wait until other packages can work with Pydantic 2. I am not sure Tiled works with Pydantic 2. |
Expected Behavior
Order shouldn't matter for Union type hints (eg
Union[int, float]
vsUnion[float, int]
Current Behavior
I'll be a bit verbose here as I'm investigating this a bit myself
Say you have a plan that takes an argument with typing
Union[float, int]
:one would expect that both these items would verify successfully:
My quick testing reveals:
Union[int, float]
Union[float, int]
It appears that when creating the pydantic model, pydantic attempts to cast the value to the first type in the Union, creating a mismatch between the value in the model and the provided value
Details
This could cause issues if we have something like
Union[str, int]
as well, as pydantic would always cast your input to a string and fail to match the input.Possible Solution
Always put float before int? be reaaaaally clear in documentation? Is this something that pydantic v2 addresses?
Context
annotating plans and having them not validate
Your Environment
bluesky_queueserver=0.0.18, though I don't think this was addressed in 0.0.19?
The text was updated successfully, but these errors were encountered: