Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wallagib opened this issue Feb 19, 2024 · 3 comments
Closed

Comments

@wallagib
Copy link

wallagib commented Feb 19, 2024

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.

@wallagib wallagib changed the title Python 3.9 has Subscripted Generics error in generated Union.py code [BUG] Python 3.9 has Subscripted Generics error in generated Union.py code Feb 19, 2024
@wallagib
Copy link
Author

I'm not sure, but I think this is the template that gets expanded to provide the if/else. In the generated code, the elif and else blocks are the same in this case, so I'm hoping there's a patch to be had.

https://github.com/openapi-generators/openapi-python-client/blob/main/openapi_python_client/templates/property_templates/union_property.py.jinja#L63

@dbanty
Copy link
Collaborator

dbanty commented Feb 20, 2024

I think this is a duplicate of #958 which I'm about to merge a fix for, so this will get closed with that. If there's a slightly different issue, though, please reopen!

dbanty added a commit that referenced this issue Feb 20, 2024
This PR was created by Knope. Merging it will create a new release

### Fixes

#### Remove spurious field_dict.update({}) for types without properties
(#969)

#### Fix invalid type check for nested unions

Nested union types (unions of unions) were generating `isinstance()`
checks that were not valid (at least for Python 3.9).

Thanks to @codebutler for PR #959 which fixes #958 and #967.

Co-authored-by: GitHub <[email protected]>
@wallagib
Copy link
Author

Yes, confirmed that 0.17.3 fixes this bug. Thank @codebutler and @dbanty !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants