-
-
Notifications
You must be signed in to change notification settings - Fork 304
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Invalid enum names not properly handled (e.g.: "mro") #873
Comments
@DavideWalder |
@koxudaxi |
@DavideWalder $ datamodel-codegen --input schema.json --input-file-type jsonschema --output model.py
$ python -c "import model"
$ echo $?
0
$ datamodel-codegen --version
0.17.0 model.py # generated by datamodel-codegen:
# filename: schema.json
# timestamp: 2023-01-31T13:13:09+00:00
from __future__ import annotations
from enum import Enum
from typing import List, Optional
from pydantic import BaseModel
class EnumWithInvalidValue(Enum):
mro_ = 'mro'
class ModelItem(BaseModel):
enum_with_invalid_value: Optional[EnumWithInvalidValue] = None
class Model(BaseModel):
__root__: List[ModelItem] Have you tried the other input or the same one? |
Found the issue: input: openapi: 3.0.3
components:
schemas:
TheEnum:
type: string
enum:
- MRO
- mro output: # generated by datamodel-codegen:
# filename: datasync.yaml
# timestamp: 2023-01-31T13:27:19+00:00
from __future__ import annotations
from enum import Enum
class TheEnum(Enum):
mro = 'MRO'
mro_ = 'mro' The underscore is suffixed only when the enum value is lowercase |
Thank you for explaining the problem. class TheEnum(Enum):
mro_ = 'MRO'
mro_1 = 'mro' |
Also, this brings up the question of what to do when two entries differ by casing like class TheEnum(Enum):
MRO_ = 'MRO'
mro_ = 'mro' This would also avoid the issue where changing the order from openapi: 3.0.3
components:
schemas:
TheEnum:
type: string
enum:
- MRO
- mro openapi: 3.0.3
components:
schemas:
TheEnum:
type: string
enum:
- mro
- MRO would change the enum names. I see that keeping the casing would be a non-backward compatible change though |
@DavideWalder without options class InvalidEnum(Enum):
MRO = 'MRO'
mro_ = 'mro'
class InvalidEnum(Enum):
mro_1 = 'MRO'
mro_ = 'mro'
class InvalidEnum(Enum):
MRO = 'MRO'
MRO_ = 'mro' |
@DavideWalder |
Describe the bug
Enum member names can't be
mro
, since this clashes with the method resolution order method.To Reproduce
Example schema:
Used commandline:
Expected behavior
The name should be adapted. According to PEP-8, the preferred solution is to suffix with an underscore (
"mro_"
instead of"mro"
). Since this is only the enum name it should not have any impact on parsing.Version:
The text was updated successfully, but these errors were encountered: