datamodel-code-generator icon indicating copy to clipboard operation
datamodel-code-generator copied to clipboard

Add support for base class for enums

Open rezondrey opened this issue 2 years ago • 1 comments

Is your feature request related to a problem? Please describe.

Python 3.11 has introduced some breaking changes for enums when used in f-strings.

class MyEnum(str, Enum):
    VALUE = "value"

# Prior python 3.11:    
print(f'{MyEnum.VALUE}')
    # => "value"
# Since python 3.11:
print(f'{MyEnum.VALUE}')
    # => "MyEnum.VALUE"

This behavior is different when using the new StrEnum:

 from enum import StrEnum

class MyEnum(StrEnum):
    VALUE = "value"
    
# Python 3.11:
print(f'{MyEnum.VALUE}')
    # => "value"

This blog describes the problem: https://blog.pecar.me/python-enum

Describe the solution you'd like Add support for additional argument, or extend "--use-subclass-enum" to allow choosing between (str, Enum) and StrEnum

Or maybe even allow selecting base class for enums, for people using custom enum packages such as https://pypi.org/project/StrEnum/

Describe alternatives you've considered A clear and concise description of any alternative solutions or features you've considered.

Additional context Add any other context or screenshots about the feature request here.

rezondrey avatar May 11 '23 10:05 rezondrey

Hi, I installed version 0.25.8 and found the same: datamodel-code-generator creates classes from (str, Enum) instead of StrEnum

BR, George

GeorgeFischhof avatar Jul 08 '24 14:07 GeorgeFischhof