Skip to content

Invalid code generated for nullable discriminated union #958

Closed
@codebutler

Description

@codebutler

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

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