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
Unexpected casting order of List[Union[float, int]] when importing Pandas (1.2.4) #2835
Comments
Actually I guess this has nothing to do with pydantic. It's something weird from pandas from typing import List, Union
# Toggle import on and off to get different result
# import pandas as pd
class Model(object):
data: List[Union[float, int]]
print(Model.__annotations__['data']) It will print |
Thanks @leuduan for catching this! The side effect comes indeed from from pydantic import BaseModel
from typing import List, Union
import pandas as pd
class Model(BaseModel, smart_union=True):
data: List[Union[float, int]]
data = [0.1234, 0.4567]
assert Model(data=data).dict() == {'data': [0.1234, 0.4567]} |
Thanks both @leuduan @PrettyWood - good catch on the separation from Pydantic! Found an smaller example a smaller example which shows the side effect, from typing import List, Union
# Toggle comments for this to change behaviour
# def example(x: List[Union[int, float]]):
# pass
class Model(object):
data: List[Union[float, int]]
print(Model.__annotations__['data']) And an even smaller example showing how the ordering itself changes in
So it looks as soon as |
@PrettyWood it would probably be helpful to flag that this can occur in the docs - given https://docs.python.org/3/library/typing.html#typing.Union says
That the docs on https://pydantic-docs.helpmanual.io/usage/types/#unions should maybe have a warning about that |
it actually occurs in the Union arg directly
|
Related to pydantic#2835 but should be resolved by pydantic#2092
Just a quick note that I experienced the same issue with |
@asmodehn It's a tricky bug isn't it! |
Tricky to track down, but it boils down to this: The pydantic doc says:
However there is no such thing as "the order of the Union" as explained in python docs : https://docs.python.org/3/library/stdtypes.html#types-union int | str == str | int |
Checks
Bug
Output of
python -c "import pydantic.utils; print(pydantic.utils.version_info())"
:Output of
python -c "import pandas as pd; print(pd.__version__)"
What I would expect based on the behaviour of Unions:
What I see running the script above (with the
import pandas as pd
):What I see when removing the pandas import from above:
Notes
Dockerfile to reproduce
Dockerfile:
Results:
Weirdness with pandas - what is causing the issue?
Now the part which looses me completely
Added into this collapsible section as it seems like a red herring..
.venv
):python -m venv .venv
source .venv/bin/activate
pip install pandas==1.2.4 pydantic==1.8.2
.venv/lib/python3.7/site-packages/pandas/io/formats/format.py
And we now have the correct parsing again 🤔
Toggle the types on and off to get the correct results
The text was updated successfully, but these errors were encountered: