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

Dataclass code gen error: TypeError: non-default argument 'XYZ' follows default argument #1966

Open
JoanPuig opened this issue May 16, 2024 · 0 comments

Comments

@JoanPuig
Copy link

Describe the bug
Generated classes create code that errors with TypeError: non-default argument 'XYZ' follows default argument.

To Reproduce

Example schema:
openapi: 3.0.0
components:
  schemas:
    Error:
      type: object
      description: Parent error component inherited by all error types.
      properties:
        message:
          type: string
    InternalServerError:
      description: Internal Server Error
      allOf:
        - $ref: '#/components/schemas/Error'
        - type: object
          required: [code]
          properties:
            code:
              $ref: '#/components/schemas/InternalServerErrorCode'
    InternalServerErrorCode:
      type: string
      enum: [INTERNAL_SERVER_ERROR]
    ServiceUnavailableError:
      description: Service Unavailable Error
      allOf:
        - $ref: '#/components/schemas/Error'
        - type: object
          required: [code]
          properties:
            code:
              $ref: '#/components/schemas/ServiceUnavailableErrorCode'
    ServiceUnavailableErrorCode:
      type: string
      enum: [SERVICE_UNAVAILABLE]

Used commandline:

datamodel-codegen --input .\spec.yml --input-file-type openapi --output .\model.py --output-model-type dataclasses.dataclass --use-generic-container-types --use-generic-container-types --enable-version-header --target-python-version 3.11 --field-constraints --use-annotated  --use-default

Generated code:

from __future__ import annotations

from dataclasses import dataclass
from enum import Enum
from typing import Optional


@dataclass
class Error:
    message: Optional[str] = None


class InternalServerErrorCode(Enum):
    INTERNAL_SERVER_ERROR = 'INTERNAL_SERVER_ERROR'


class ServiceUnavailableErrorCode(Enum):
    SERVICE_UNAVAILABLE = 'SERVICE_UNAVAILABLE'


@dataclass
class InternalServerError(Error):
    code: InternalServerErrorCode


@dataclass
class ServiceUnavailableError(Error):
    code: ServiceUnavailableErrorCode

Which is inconsistent and will lead to the following error:

TypeError: non-default argument 'code' follows default argument

This can be solved by adding kw_only argument as to the dataclass annotation as follows: @DataClass(kw_only=True) as described in:
https://stackoverflow.com/questions/69711886/python-dataclasses-inheritance-and-default-values

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