koxudaxi / datamodel-code-generator

Pydantic model and dataclasses.dataclass generator for easy conversion of JSON, OpenAPI, JSON Schema, and YAML data sources.
https://koxudaxi.github.io/datamodel-code-generator/
MIT License
2.64k stars 294 forks source link

Add support for base class for enums #1313

Open rezondrey opened 1 year ago

rezondrey commented 1 year ago

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.

GeorgeFischhof commented 2 months ago

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

BR, George