With the following python type definitions:
import pydantic
from typing import List, Union
import sys
import pprint
class TypeA(pydantic.BaseModel):
name:str = "Type A"
type_a_spesific:float = 1337
class TypeB(pydantic.BaseModel):
name:str = "Type B"
type_b_spesific:str = "bob"
class ReturnObject(pydantic.BaseModel):
parameters:List[Union[TypeA, TypeB] ]
parameters = list()
parameters.append(TypeA(name="item 1", type_a_spesific=69))
parameters.append(TypeB(name="item 2", type_b_spesific="lol"))
ret = ReturnObject(parameters = parameters)
print(f"Python version: {pprint.pformat(sys.version_info)}")
print(f"Pydantic version: {pydantic.__version__}")
print(ret.json(indent=3))
print(ret)
The version output looks like this:
Python version: sys.version_info(major=3, minor=11, micro=5, releaselevel='final', serial=0)
Pydantic version: 1.10.11
I would expect the parameter list to contain two objects with different type like this:
Expected
{
"parameters": [
{
"name": "item 1",
"type_a_spesific": 69
},
{
"name": "item 2",
"type_b_spesific": "lol"
}
]
}
parameters=[TypeA(name='item 1', type_a_spesific=69.0), TypeB(name='item 2', type_b_spesific="lol")]
However what comes out instead is this:
Actual
{
"parameters": [
{
"name": "item 1",
"type_a_spesific": 69.0
},
{
"name": "item 2",
"type_a_spesific": 1337
}
]
}
parameters=[TypeA(name='item 1', type_a_spesific=69.0), TypeA(name='item 2', type_a_spesific=1337)]
For some reason the List[ Union[ TypeA, TypeB ]] does not work. What gives?
.json
no longer supports keyword arguments, and is being deprecated, but without the indent argument, it gives the output you would expect – Blanklyprint(ret)
give you? – Blanklydocker run -it <container> -e bash
then you can use pip list, but you can also just checkimport pydantic; print(pydantic.__version__)
. Anyway, yeah, sounds like you have version lower than 2. You should probably update, 2 makes some changes though. I believe the most recent release is 2.4 – Blankly