简体   繁体   中英

Using complex data structures inside Enums with pydantic

I'm currently using pydantic's .parse_raw() function to serialize a json into a python object and I seem to be having difficulty grasping the way you would implement a complex Enum into the process.

The issue can be presented like so:

from enum import Enum
from pydantic import BaseModel

class D(BaseModel):
    other_thing: str

class C(BaseModel):
    other_stuff: str

class B(Enum):
    stuff:  C
    stuff2: D


class A(BaseModel):
    thing: str
    thing2: B


my_object = A.parse_raw('''{
    "thing":"00",
    "thing2": {
        "stuff" : {
            "other_stuff": "my_value"
        }
    }
}''')

print(my_object.thing2.stuff.other_stuff)

Throws

pydantic.error_wrappers.ValidationError: 1 validation error for A
thing2
  value is not a valid enumeration member; permitted:  (type=type_error.enum; enum_values=[])

If i understand correctly, Enum requires me to set specific values for its members like

class B(Enum):
    stuff =  C(other_stuff='123')
    stuff2 = D(other_thing='321')

but with a complex data structure that would be impossible. I have came up with a hacky way to fix this like this:

from typing import Optional
class B(BaseModel):
    stuff: Optional[C]
    stuff2: Optional[D]

However this does not ensure that i am only getting one value there and I end with a few populated None fields.

What am I missing here ?

I'm currently using pydantic's .parse_raw() function to serialize a json into a python object and I seem to be having difficulty grasping the way you would implement a complex Enum into the process.

The issue can be presented like so:

from enum import Enum
from pydantic import BaseModel

class D(BaseModel):
    other_thing: str

class C(BaseModel):
    other_stuff: str

class B(Enum):
    stuff:  C
    stuff2: D


class A(BaseModel):
    thing: str
    thing2: B


my_object = A.parse_raw('''{
    "thing":"00",
    "thing2": {
        "stuff" : {
            "other_stuff": "my_value"
        }
    }
}''')

print(my_object.thing2.stuff.other_stuff)

Throws

pydantic.error_wrappers.ValidationError: 1 validation error for A
thing2
  value is not a valid enumeration member; permitted:  (type=type_error.enum; enum_values=[])

If i understand correctly, Enum requires me to set specific values for its members like

class B(Enum):
    stuff =  C(other_stuff='123')
    stuff2 = D(other_thing='321')

but with a complex data structure that would be impossible. I have came up with a hacky way to fix this like this:

from typing import Optional
class B(BaseModel):
    stuff: Optional[C]
    stuff2: Optional[D]

However this does not ensure that i am only getting one value there and I end with a few populated None fields.

What am I missing here ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM