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

TypedDict Generation not covering const required #2314

Open
Mazyod opened this issue Feb 9, 2025 · 0 comments
Open

TypedDict Generation not covering const required #2314

Mazyod opened this issue Feb 9, 2025 · 0 comments
Labels
bug Something isn't working help wanted Extra attention is needed

Comments

@Mazyod
Copy link

Mazyod commented Feb 9, 2025

Describe the bug
Generating TypedDict definitions of a schema with const and required yields NotRequired[str].

To Reproduce

Example schema:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "definitions": {
    "AndType": {
      "additionalProperties": false,
      "description": "...",
      "properties": {
        "kind": {
          "const": "and",
          "type": "string"
        }
      },
      "required": [
        "kind"
      ],
      "type": "object"
    }
  }
}

Used commandline:

$ datamodel-codegen \
  --input ./schema.json \
  --output ./schema.py \
  --output-model-type "typing.TypedDict" \
  --target-python-version "3.11"

Generated code:

# generated by datamodel-codegen:
#   filename:  schema.json
#   timestamp: 2025-02-09T17:42:53+00:00

from __future__ import annotations

from typing import Any, NotRequired, TypedDict

Model = Any


class AndType(TypedDict):
    kind: NotRequired[str]

Expected behavior
Generate kind: Literal["and"] since it is a required const property.

Version:

  • OS: macOS
  • Python version: 3.13.1
  • datamodel-code-generator version: 0.27.2
@gaborbernat gaborbernat added bug Something isn't working help wanted Extra attention is needed labels Feb 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants