datamodel-code-generator
datamodel-code-generator copied to clipboard
Incorrect type annotations for untyped JSON schema arrays
Describe the bug
When a JSON schema field is declared as "type": "array" with no further details, the generated data model code fails typechecking:
src/_models/__init__.py:1990: error: Missing type parameters for generic type "List" [type-arg]
src/_models/__init__.py:1991: error: Missing type parameters for generic type "List" [type-arg]
To Reproduce
Example schema snippet:
"system/rpc/listDownloadedModels/returns": {
"type": "array"
},
Used commandline:
$ datamodel-codegen --input /path/to/schema.json --input-file-type jsonschema --output /path/to/src/_models/__init__.py \
--output-model-type pydantic_v2.BaseModel --use-annotated --use-union-operator
The generated model that fails typechecking:
class SystemRpcListDownloadedModelsReturns(RootModel[List]):
root: List
Expected behavior
Untyped arrays should be effectively typed as List[Any] rather than List
Version:
- OS: Linux
- Python version: 3.12
- datamodel-code-generator version: 0.26.3
Additional context
--use-generic-container-types just shifted the error to complaining about Sequence instead.
--use-standard-collections crashed mypy outright, so I didn't investigate that any further.
The offending schema field not having any type information at all regarding the permitted elements is probably a bug in its own right, but not one that has anything to do with datamodel-code-generator.
I've since tried the msgspec output format on this problem, and it also does the wrong thing:
SystemRpcListDownloadedModelsReturns = List
So presumably the problem is occurring earlier, when the JSON schema entry is converted into a Python type hint (a bare array should be mapping to List[Any] rather than List)
I'm having the same problem, on v0.31.2, with Python 3.13.
Unlike the above, MyPy doesn't crash when I use --use-standard-collections, but either way I get bare list or List, and no [Any].
I'm using the swagger spec output by a FastAPI app that itself uses Pydantic models to generate the schema. These problematic fields are defined as list[Any] in the original models.