Skip to content

Tag: enums

Export Pydantic Model, with all ENUM values expanded

I have a pydantic model ModelX, with member Enums defined. The script should run as is. I want to export this model as a JSON with the list of all Enum values. Something like I am looking to return this Json as a response of my api call, all Enum values, it can be under “allOf” key or some other

unexpected behavior with EnumMeta class

I defined an IntEnum for some weather codes and provided a metaclass. as show above it functions as expected when passed into a dict. but if I change the method name of names to keys like … I get an unexpected result It was my understanding that dict calls the __iter__ method under the hood to create a …

Whats the use of values in enum in Python?

I’m working with enum lately and I dont really get the utility of them in some cases. I hope my question is not too trivial or too stupid, and I would really love to better understand the logic behind this python structure. One common use I found online or in some pieces of code I have been working on l…

Pydantic/SQLAlchemy: How to work with enums?

What is the best way to convert a sqlalchemy model to a pydantic schema (model) if it includes an enum field? Sqlalchemy Pydantic Conversion This leads to the error value is not a valid enumeration member; permitted: ‘CREATED’, ‘UPDATED’ (type=type_error.enum; enum_values=[<StateEnu…

How to implement a factory class?

I want to be able to create objects based on an enumeration class, and use a dictionary. Something like this: But I get the error: For some reason the dictionary can’t be created. Where am I going wrong here? Answer After looking at Bruce Eckel’s book I came up with this: This gets the user to sel…