Skip to content

[BUG] Python client doesn't pass style checks #21936

@artem-ilin

Description

@artem-ilin

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator?
  • 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

Freshly created python api client doesn't pass style checks with fixes by ruff. ruff is a popular tool for python linting and formatting.

openapi-generator version

7.15.0
also current master

OpenAPI declaration file content or url

from project samlpes

Generation Details

In python a preffered way to check types equality is using is operator or using a issubclass().

>>> class T(str): ...
>>> T == str
False
>>> T is str
False
>>> str == str
True
>>> str is str
True
>>> issubclass(T, str)
True
>>> issubclass(str, str)
True
>>> 
Steps to reproduce
git clone [email protected]:OpenAPITools/openapi-generator.git
cd openapi-generator/samples/client/echo_api/python
docker run -v .:/io --rm ghcr.io/astral-sh/ruff check --fix

actual output:

warning: Invalid rule code provided to `# noqa` at setup.py:16: H301
E721 Use `is` and `is not` for type comparisons, or `isinstance()` for isinstance checks
   --> openapi_client/api_client.py:459:14
    |
457 |         if klass in self.PRIMITIVE_TYPES:
458 |             return self.__deserialize_primitive(data, klass)
459 |         elif klass == object:
    |              ^^^^^^^^^^^^^^^
460 |             return self.__deserialize_object(data)
461 |         elif klass == datetime.date:
    |

Found 56 errors (55 fixed, 1 remaining).

elxpected output: all errors fixed

Related issues/PRs

#21935

Suggest a fix

change == operator with issubclass() calls (it is already used for Enum type)

if klass in self.PRIMITIVE_TYPES:
return self.__deserialize_primitive(data, klass)
elif klass == object:
return self.__deserialize_object(data)
elif klass == datetime.date:
return self.__deserialize_date(data)
elif klass == datetime.datetime:
return self.__deserialize_datetime(data)
elif klass == decimal.Decimal:
return decimal.Decimal(data)
elif issubclass(klass, Enum):
return self.__deserialize_enum(data, klass)
else:
return self.__deserialize_model(data, klass)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions