Skip to content

[BUG] Python 3.9 has Subscripted Generics error in generated Union.py code #967

Closed
@wallagib

Description

@wallagib

Describe the bug
Python 3.9.18 has an error with the to_dict() function generated for a Union[]
Python 3.10 does not have an issue.

The generated code that errors out is:

def to_dict(self) -> Dict[str, Any]:
    task_id: Union[None, Union[int, str], Unset]
    if isinstance(self.task_id, Unset):
        task_id = UNSET
    elif isinstance(self.task_id, Union[int, str]):
        task_id = self.task_id
    else:
        task_id = self.task_id

The following code can be run with the openapi spec:

from todo_client.models.listable import Listable
x = Listable(task_id=1)
x.to_dict()

This is the error that shows in Python 3.9
TypeError: Subscripted generics cannot be used with class and instance checks

At the very least, the pyproject.toml should be updated to show that Python 3.9 is no longer completely supported. Ideally, we'd have a patch since 3.9 is not EOL yet.

OpenAPI Spec File
bad-openapi.json

Desktop (please complete the following information):

  • OS: MacOS M1 14.3
  • Python Version: 3.9.18 (originally seen on 3.9.6)
  • openapi-python-client version 0.17.2

Additional Context
This does not happen in openapi-python-client version 0.13.2, which I was upgrading from.

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