8000 Invalid code generated for nullable discriminated union · Issue #958 · openapi-generators/openapi-python-client · GitHub
[go: up one dir, main page]

Skip to content

Invalid code generated for nullable discriminated union #958

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
codebutler opened this issue Feb 9, 2024 · 0 comments · Fixed by #959 or #970
Closed

Invalid code generated for nullable discriminated union #958

codebutler opened this issue Feb 9, 2024 · 0 comments · Fixed by #959 or #970

Comments

@codebutler
Copy link
Contributor
codebutler commented Feb 9, 2024

Describe the bug

Given the schema below, using the generated code like:

from test_client.models.demo import Demo
from test_client.models.a import A

Demo(example_union=A()).to_dict()

fails with:

Traceback (most recent call last):
  File "/Users/eric/Desktop/test/test.py", line 4, in <module>
    Demo(example_union=A()).to_dict()
  File "/Users/eric/Desktop/test/test_client/models/demo.py", line 32, in to_dict
    elif isinstance(self.example_union, Union["A", "B"]):
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/eric/.pyenv/versions/3.12.1/lib/python3.12/typing.py", line 1564, in __instancecheck__
    return self.__subclasscheck__(type(obj))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/eric/.pyenv/versions/3.12.1/lib/python3.12/typing.py", line 1568, in __subclasscheck__
    if issubclass(cls, arg):
       ^^^^^^^^^^^^^^^^^^^^
TypeError: issubclass() arg 2 must be a class, a tuple of classes, or a union

OpenAPI Spec File

openapi: 3.0.3
info:
  title: Test
  version: 0.0.0
  description: test
paths: {}
components:
  schemas:
    Demo:
      type: object
      properties:
        example_union:
          allOf:
            - $ref: '#/components/schemas/ExampleUnion'
          nullable: true # <-- bug does not happen if this line is removed
    A:
      type: object
      properties:
        type:
          type: string
    B:
      type: object
      properties:
        type:
          type: string
    ExampleUnion:
      oneOf:
      - $ref: '#/components/schemas/A'
      - $ref: '#/components/schemas/B'
      discriminator:
        propertyName: type
        mapping:
          a: '#/components/schemas/A'
          b: '#/components/schemas/B'

Desktop

  • OS: macOS 14.3
  • Python Version: 3.12.1
  • openapi-python-client version: 0.17.2

Additional context

The failing generated code is:

isinstance(self.example_union, Union["A", "B"])

Using instanceof on a Union with quoted types is not allowed:

>>> class A:
...     pass
...
>>> class B:
...     pass
...
>>> from typing import Union
>>> isinstance(None, Union[A, B])
False
>>> isinstance(None, Union["A", "B"])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/eric/.pyenv/versions/3.12.1/lib/python3.12/typing.py", line 1564, in __instancecheck__
    return self.__subclasscheck__(type(obj))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/eric/.pyenv/versions/3.12.1/lib/python3.12/typing.py", line 1568, in __subclasscheck__
    if issubclass(cls, arg):
       ^^^^^^^^^^^^^^^^^^^^
TypeError: issubclass() arg 2 must be a class, a tuple of classes, or a union
codebutler pushed a commit to codebutler/openapi-python-client that referenced this issue Feb 12, 2024
github-merge-queue bot pushed a commit that referenced this issue Feb 20, 2024
fixes #958

---------

Co-authored-by: Dylan Anthony <43723790+dbanty@users.noreply.github.com>
Co-authored-by: Dylan Anthony <dbanty@users.noreply.github.com>
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 <github-actions@github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant
0