enums

Is it possible to pass kwargs to customised python enum

Is it possible to pass kwargs to customised python enum Question: You can customise an enum so the members have extra attributes. There are plenty of examples of that to be found. They all look something like this: class EnumWithAttrs(Enum): def __new__(cls, *args, **kwargs): value = len(cls.__members__) + 1 obj = object.__new__(cls) obj._value_ = value …

Total answers: 2

How to apply a method to all values in Enum class?

How to apply a method to all values in Enum class? Question: I have some Enums which may or may not have clashing names but are in different modules, so I want to apply some sort of prefix / suffix lambda to make them unique without having to manually specify it for each value. How …

Total answers: 1

Validate Pydantic dynamic float enum by name with OpenAPI description

Validate Pydantic dynamic float enum by name with OpenAPI description Question: Following on from this question and this discussion I am now trying to create a Pydantic BaseModel that has a field with a float Enum that is created dynamically and is validated by name. (Down the track I will probably want to use Decimal …

Total answers: 3

Python compare type of (str, enum) classes

Python compare type of (str, enum) classes Question: I have multiple enums defined with from enum import Enum class EnumA(str, Enum): RED = "red" class EnumB(str, Enum): BLUE = "blue" How do I compare the type of these classes/enums with say x=EnumA.RED? The following doesn’t work. type(x) is enum type(x) is EnumType type(x) is Enum …

Total answers: 3

How do I type hint for Enums in Python?

How do I type hint for Enums in Python? Question: I have a python function for which I want to use type hinting. There are two arguments. The first is any Enum class, the second optional arg is an element of that Enum. For example, say I have: class Foo(Enum): ALPHA = 1 BETA = …

Total answers: 1

Python mypy warning of incompatible type when using Enum Flag

Python mypy warning of incompatible type when using Enum Flag Question: I am trying to make a variable that can be from a set of enum values, and then select a specific one when using it elsewhere. from enum import Flag, auto class MyEnum(Flag): FOO: int = auto() BAR: int = auto() MOO: int = …

Total answers: 1

How to map Django TextChoices text to a choice?

How to map Django TextChoices text to a choice? Question: Suppose I have this code, inspired from the Django docs about enumeration types: class YearInSchool(models.TextChoices): FRESHMAN = ‘FR’, ‘Freshman’ SOPHOMORE = ‘SO’, ‘Sophomore’ JUNIOR = ‘JR’, ‘Junior’ SENIOR = ‘SR’, ‘Senior’ GRADUATE = ‘GR’, ‘Graduate’ Now suppose I have the string "Sophomore". How do I …

Total answers: 2

How to validate based on specific Enum member in a Fastapi Pydantic model

How to validate based on specific Enum member in a Fastapi Pydantic model Question: Here is my Pydantic model: from enum import Enum from pydantic import BaseModel class ProfileField(str, Enum): mobile = "mobile" email = "email" address = "address" interests ="interests" # need list of strings class ProfileType(str, Enum): primary = "primary" secondary = "secondary" …

Total answers: 1

Python compiler says I'm adding an extra argument to int in an Enum

Python compiler says I'm adding an extra argument to int in an Enum Question: I’m trying to create a custom enumerator that can replace an int, but has additional fields. from enum import IntEnum class MD_Fields(IntEnum): ACCOUNT = (0, "Account", True) M_DESCRIPT = (4, "Description", False) def __new__(cls, value: int, description: str, identifier: bool): obj …

Total answers: 1

How to type Enum created dynamically with a default value?

How to type Enum created dynamically with a default value? Question: Using the Functional API I want to type my enum so it would have a default known value. Example: class MyBaseClass: … class DerivedA(MyBaseClass): … class DerivedB(MyBaseClass): … DerivedChoice = enum.Enum(‘DerivedChoice’, {cls.__name__: cls for cls in (DerivedA, DerivedB)}) foo: DerivedChoice = DerivedChoice.DerivedB foo.value # …

Total answers: 1