Skip to content

Generated code lacks import of typing.cast for required const properties #1150

Closed

Description

@benweint

Describe the bug
When an input schema has a required const property, the generated code does makes calls to typing.cast, but does not import it, leading to failures when trying to run ruff over the generated code.

OpenAPI Spec File

openapi: 3.1.0
info:
  title: Pets Example
  version: 1.0.0
paths:
  /pets:
    post:
      requestBody:
        description: Create a new pet in the system
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/Pet"
      responses:
        "200":
          description: Return a 200 status to indicate that the pet was created successfully

components:
  schemas:
    Pet:
      required:
        - type
      properties:
        type:
          const: cat

Desktop (please complete the following information):

  • OS: macOS 14.6.1
  • Python Version: 3.12.6
  • openapi-python-client version: latest main (also present in latest release, 0.21.6)

Additional context

Removing type from the required field list in the example above makes the problem disappear - in this case, cast is still used, but is properly imported.

Activity

  1. added 2 commits that reference this issue on Oct 28, 2024
    0a0671b
    4ce002e
  2. added a commit that references this issue on Oct 28, 2024
    40d63f9
  3. added a commit that references this issue on May 13, 2025
    627ec1a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions