Description
Initial Checks
- I confirm that I'm using Pydantic V2
Description
A field expecting a list of float number is valid for a numpy array of complex number, but a field expecting a float number is not valid for a complex number.
I would expect the behavior to be the same, i.e. the complex number is a valid float number.
The output of the example code below is (forget about the ComplexWarning
):
/x/python3.11/site-packages/pydantic/main.py:211: ComplexWarning: Casting complex values to real discards the imaginary part
validated_self = self.__pydantic_validator__.validate_python(data, self_instance=self)
Traceback (most recent call last):
File "/x/scratch_10.py", line 10, in <module>
Model(x=1j, y=array([1.]))
File "/x/python3.11/site-packages/pydantic/main.py", line 211, in __init__
validated_self = self.__pydantic_validator__.validate_python(data, self_instance=self)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
pydantic_core._pydantic_core.ValidationError: 1 validation error for Model
x
Input should be a valid number [type=float_type, input_value=1j, input_type=complex]
For further information visit https://errors.pydantic.dev/2.9/v/float_type
Example Code
from numpy import array
from pydantic import BaseModel
class Model(BaseModel):
x: float
y: list[float]
Model(x=1., y=array([1.]))
Model(x=1j, y=array([1.]))
Python, Pydantic & OS Version
pydantic version: 2.9.0
pydantic-core version: 2.23.2
pydantic-core build: profile=release pgo=false
install path: /x/python3.11/site-packages/pydantic
python version: 3.11.10 (main, Sep 9 2024, 00:00:00) [GCC 14.2.1 20240801 (Red Hat 14.2.1-1)]
platform: Linux-6.10.9-200.fc40.x86_64-x86_64-with-glibc2.39
related packages: typing_extensions-4.12.2 typing_extensions-4.12.2
commit: unknown