Skip to content
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

Incorrect type annotations for untyped JSON schema arrays #2199

Open
ncoghlan opened this issue Nov 28, 2024 · 1 comment
Open

Incorrect type annotations for untyped JSON schema arrays #2199

ncoghlan opened this issue Nov 28, 2024 · 1 comment

Comments

@ncoghlan
Copy link
Contributor

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.

@ncoghlan
Copy link
Contributor Author

ncoghlan commented Dec 3, 2024

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant