Skip to content

[BUG] Generated python code is missing a return type #16440

Description

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

The generated api clients of the Python module produce a __init__ method which has type hints for all its parameters, but is missing the return type annotation.
E.g.: https://github.com/OpenAPITools/openapi-generator/blob/master/samples/client/echo_api/python/openapi_client/api/body_api.py#L45

Creating an instance of the said class and then run mypy over the file will result in the following error:

error: Call to untyped function "BodyApi" in typed context  [no-untyped-call]
openapi-generator version

7.0.0

OpenAPI declaration file content or url

I used the sample code as an example: https://github.com/OpenAPITools/openapi-generator/tree/master/samples/client/echo_api/python

Generation Details

¯_(ツ)_/¯

Steps to reproduce
  1. Checkout the repository
  2. cd to: samples/client/echo_api/python
  3. Change the supported python version to at least 3.8
  4. Create a new directory in samples/client/echo_api/python_test
  5. Create a pyproject.toml with the following contents:
[tool.poetry]
name = "python-test"
version = "0.1.0"
description = ""
authors = ["Your Name <you@example.com>"]
readme = "README.md"

[tool.poetry.dependencies]
python = "^3.10"
openapi-client = {path = "../python"}

[tool.poetry.group.dev.dependencies]
mypy = "^1.5.1"

[tool.mypy]
strict = true

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
  1. Create a test.py with the follwing contents:
from openapi_client.api.body_api import BodyApi

body_api = BodyApi()
  1. Call poetry run mypy test.py
Suggest a fix

Simply adding -> None as the return type of the __init__ function solves the problem.
Assuming you have the Steps to reproduce done:

  1. Create the following diff:
diff --git a/samples/client/echo_api/python/openapi_client/api/body_api.py b/samples/client/echo_api/python/openapi_client/api/body_api.py
index fa1953cecd3..e07517d2920 100644
--- a/samples/client/echo_api/python/openapi_client/api/body_api.py
+++ b/samples/client/echo_api/python/openapi_client/api/body_api.py
@@ -42,7 +42,7 @@ class BodyApi(object):
     Do not edit the class manually.
     """

-    def __init__(self, api_client=None):
+    def __init__(self, api_client=None) -> None:
         if api_client is None:
             api_client = ApiClient.get_default()
         self.api_client = api_client

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions