Describe the bug
generated pydantic code is faulty when the field name matches the generated enum name for a given field.
To Reproduce
Example schema:
{
"properties": {
"a_int": {
"default": 1,
"section": "numeric",
"title": "A Int",
"type": "integer"
},
"DEnum": {
"enum": [
"yellow",
"red",
"violet"
],
"type": "string",
"default": "yellow",
"description": "pick colour"
}
},
"required": [
"DEnum"
],
"title": "Test",
"type": "object"
}
Used commandline:
$ datamodel-codegen --input test.json --input-file-type jsonschema --output model.py
generates model.py
:
# generated by datamodel-codegen:
# filename: test.json
# timestamp: 2024-09-19T14:16:11+00:00
from __future__ import annotations
from enum import Enum
from typing import Optional
from pydantic import BaseModel, Field
class DEnum(Enum):
yellow = 'yellow'
red = 'red'
violet = 'violet'
class Test(BaseModel):
a_int: Optional[int] = Field(1, title='A Int')
DEnum: DEnum = Field(..., description='pick colour')
then when try to use this file you get an error:
python
Python 3.12.3 | packaged by conda-forge | (main, Apr 15 2024, 18:38:13) [GCC 12.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from model import Test
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/jovyan/xlsxdatagrid/tests/model.py", line 19, in <module>
class Test(BaseModel):
File "/home/jovyan/miniforge3/envs/xlsxdatagrid-dev/lib/python3.12/site-packages/pydantic/_internal/_model_construction.py", line 197, in __new__
set_model_fields(cls, bases, config_wrapper, types_namespace)
File "/home/jovyan/miniforge3/envs/xlsxdatagrid-dev/lib/python3.12/site-packages/pydantic/_internal/_model_construction.py", line 474, in set_model_fields
fields, class_vars = collect_model_fields(cls, bases, config_wrapper, types_namespace, typevars_map=typevars_map)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/jovyan/miniforge3/envs/xlsxdatagrid-dev/lib/python3.12/site-packages/pydantic/_internal/_fields.py", line 229, in collect_model_fields
field_info = FieldInfo.from_annotated_attribute(ann_type, default)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/jovyan/miniforge3/envs/xlsxdatagrid-dev/lib/python3.12/site-packages/pydantic/fields.py", line 351, in from_annotated_attribute
raise PydanticUserError(
pydantic.errors.PydanticUserError: Error when building FieldInfo from annotated attribute. Make sure you don't have any field name clashing with a type annotation
For further information visit https://errors.pydantic.dev/2.7/u/unevaluable-type-annotation
>>>
Expected behavior
The issue seems to be because of the name and field name overlap of DEnum.
It would be great if it was smart enough not to let that happen... or else allow the user an argument to append some text onto Enum names or add a variable to the jsonschema that sets the enum name.
Version:
Additional context
If you can suggest workarounds that would also be appreciated
Aside
the default value also doesn't appear to be coming through
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too