openapi-generators / openapi-generators/openapi-python-client

Model named `Client` causes errors

Open
#1,045 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
2k
Forks
293
Avg merge
34m
Merged PRs (30d)
1

Description

Describe the bug
If a model of the Openapi App is called Client this name collides with the client.Client object in imports.

OpenAPI Spec File

{
  "openapi": "3.1.0",
  "info": {
    "title": "FastAPI",
    "version": "0.1.0"
  },
  "paths": {
    "/client": {
      "get": {
        "summary": "Client",
        "operationId": "client_client_get",
        "responses": {
          "200": {
            "description": "Successful Response",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Client"
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "Client": {
        "properties": {
          "foo": {
            "type": "string",
            "title": "Foo"
          },
          "bar": {
            "type": "integer",
            "title": "Bar"
          }
        },
        "type": "object",
        "required": [
          "foo",
          "bar"
        ],
        "title": "Client"
      }
    }
  }
}

Desktop (please complete the following information):

  • OS: [e.g. Ubuntu 22.04]
  • Python Version: [e.g. 3.10.12]
  • openapi-python-client version [e.g. 0.19.1]

Additional context
The generator outputs:

Generating fast-api-client
Error(s) encountered while generating, client was not created

ruff failed

fast_api_client/api/default/client_client_get.py:8:30: F811 Redefinition of unused `Client` from line 7
Found 16 errors (15 fixed, 1 remaining).

In the relevant code there are two imports:

from ...client import AuthenticatedClient, Client
from ...models.client import Client

As hinted by ruff' error message there are two imports that load a Client object but they are loading different objects.
If you ty to use the generated code:

from fast_api_client import Client
from fast_api_client.api.default import client_client_get

with Client(base_url="http://localhost:8000") as client:
    data = client_client_get.sync(client=client)

print(data)

It will still work properly as the only Client type that is actually used at runtime is the one from models which is imported last (the Client from client is only used for type-hinting).

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue with the provided OpenAPI spec, then inspect the generated fast_api_client/api/default/client_client_get.py imports. The fix is complete when the generated model and client types no longer collide and ruff passes without the F811 error.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
api, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.