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
I have searched GitHub for a duplicate issue and I'm sure this is something new
I have searched Google & StackOverflow for a solution and couldn't find anything
I have read and followed the docs and still think this is a bug
I am confident that the issue is with pydantic (not my code, or another library in the ecosystem like FastAPI or mypy)
Description
Consider the code block below where the Transaction model can accept one single user or a dictionary of users. The initialization works with no problem
frompydanticimportBaseModel, Field, SecretStrfromtypingimportUnion, DictclassUser(BaseModel):
id: intusername: str="dsds"password: SecretStrclassConfig:
"""Pydantic configuration"""arbitrary_types_allowed=Truevalidate_all=True# Validate the default parameters toovalidate_assignment=Falseextra="forbid"# Forbid passing keyword arguments that are not registeredclassTransaction(BaseModel):
id: struser: Union[Dict[str, User], User]
value: intTransaction(id='1234567890',
user={"user_1": dict(
id=45,
username='JohnDoe',
password='hashedpassword'
)},
value=9876543210)
ValidationError Traceback (most recent call last)
<ipython-input-37-f42c1f766053> in <module>
27 password='hashedpassword'
28 )},
---> 29 value=9876543210)
30
31 # Transaction(id='1234567890',
~\RMS-Python\envs\bhp3.7\lib\site-packages\pydantic\main.cp37-win_amd64.pyd in pydantic.main.BaseModel.__init__()
ValidationError: 4 validation errors for Transaction
user -> user_1 -> id
value is not a valid integer (type=type_error.integer)
user -> id
field required (type=value_error.missing)
user -> password
field required (type=value_error.missing)
user -> user_1
extra fields not permitted (type=value_error.extra)
Not sure if this is something that can be handled by discriminator?
pydantic version info
# Name Version Build Channel
pydantic 1.10.2 py37h51bd9d9_0 conda-forge
I'll note that, while this particular case may be difficult to handle cleanly, #6915 if merged might provide a way to improve the error messages here, though you'd have to have a clear way to distinguish whether a value was meant to be a dict or the model itself.
Thanks for looking into this 👍. The main logic from my understanding is that a Union can support multiple type validation scenarios but once the initialization is done and it is decided which type from the Union has been used, then the error message should correspond to what that has been set.
What I ended up doing is to check the data type prior to pydnatic initialization and switch the subfields and I wonder if that can ne handled by pydantic.init()
Initial Checks
Description
Consider the code block below where the Transaction model can accept one single user or a dictionary of users. The initialization works with no problem
But, if there is a validation issue, then multiple errors are generated as it check over the union of types
Not sure if this is something that can be handled by
discriminator
?pydantic version info
Example Code
No response
Python, Pydantic & OS Version
Affected Components
.dict()
and.json()
construct()
, pickling, private attributes, ORM modeThe text was updated successfully, but these errors were encountered: