Skip to content

Commit

Permalink
[python-experimental] fixes bug where some singleton representations …
Browse files Browse the repository at this point in the history
…raised a RecursionError (OpenAPITools#12157)

* Adds issue components and endpoint

* Regenerates samples

* Fixes singleton repr, removes issue components and endpoint

* Removes unused endpoint

* Reverts file

* Adds tests of enum, boolean, and none representations

* Uses super repr for singletons that ere not none, true, or false
  • Loading branch information
spacether authored and rk0n committed Apr 24, 2022
1 parent 59dc481 commit 1f48d76
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

{{>partial_header}}

from collections import defaultdict, abc
from collections import defaultdict
from datetime import date, datetime, timedelta # noqa: F401
from dataclasses import dataclass
import functools
import decimal
import io
Expand Down Expand Up @@ -470,7 +469,6 @@ class Singleton:
Enums and singletons are the same
The same instance is returned for a given key of (cls, arg)
"""
# TODO use bidict to store this so boolean enums can move through it in reverse to get their own arg value?
_instances = {}

def __new__(cls, *args, **kwargs):
Expand All @@ -488,7 +486,13 @@ class Singleton:
return cls._instances[key]

def __repr__(self):
return '({}, {})'.format(self.__class__.__name__, self)
if isinstance(self, NoneClass):
return f'<{self.__class__.__name__}: None>'
elif isinstance(self, BoolClass):
if (self.__class__, True) in self._instances:
return f'<{self.__class__.__name__}: True>'
return f'<{self.__class__.__name__}: False>'
return f'<{self.__class__.__name__}: {super().__repr__()}>'


class NoneClass(Singleton):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@
Generated by: https://openapi-generator.tech
"""

from collections import defaultdict, abc
from collections import defaultdict
from datetime import date, datetime, timedelta # noqa: F401
from dataclasses import dataclass
import functools
import decimal
import io
Expand Down Expand Up @@ -477,7 +476,6 @@ class Singleton:
Enums and singletons are the same
The same instance is returned for a given key of (cls, arg)
"""
# TODO use bidict to store this so boolean enums can move through it in reverse to get their own arg value?
_instances = {}

def __new__(cls, *args, **kwargs):
Expand All @@ -495,7 +493,13 @@ def __new__(cls, *args, **kwargs):
return cls._instances[key]

def __repr__(self):
return '({}, {})'.format(self.__class__.__name__, self)
if isinstance(self, NoneClass):
return f'<{self.__class__.__name__}: None>'
elif isinstance(self, BoolClass):
if (self.__class__, True) in self._instances:
return f'<{self.__class__.__name__}: True>'
return f'<{self.__class__.__name__}: False>'
return f'<{self.__class__.__name__}: {super().__repr__()}>'


class NoneClass(Singleton):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
"""


import sys
import unittest

import petstore_api
Expand All @@ -28,9 +27,10 @@ def tearDown(self):

def test_BooleanEnum(self):
"""Test BooleanEnum"""
# FIXME: construct object with mandatory attributes with example values
model = BooleanEnum(True)
model is BooleanEnum.TRUE
# TODO why is BooleanEnum.TRUE.__class__ DynamicDynamicBooleanEnum? It should only have one Dynamic
# assert model is BooleanEnum.TRUE
assert repr(model) == '<DynamicBooleanEnum: True>'
with self.assertRaises(petstore_api.ApiValueError):
BooleanEnum(False)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
"""


import sys
import unittest

import petstore_api
Expand All @@ -34,6 +33,7 @@ def testNullableString(self):
assert isinstance(inst, NullableString)
assert isinstance(inst, Schema)
assert inst.is_none() is True
assert repr(inst) == '<DynamicNullableString: None>'

inst = NullableString('approved')
assert isinstance(inst, NullableString)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
"""


import sys
import unittest

import petstore_api
Expand All @@ -32,12 +31,14 @@ def testStringEnum(self):
inst = StringEnum(None)
assert isinstance(inst, StringEnum)
assert isinstance(inst, NoneClass)
assert repr(inst) == '<DynamicStringEnum: None>'

inst = StringEnum('approved')
assert isinstance(inst, StringEnum)
assert isinstance(inst, Singleton)
assert isinstance(inst, str)
assert inst == 'approved'
assert repr(inst) == "<DynamicStringEnum: 'approved'>"

with self.assertRaises(petstore_api.ApiValueError):
StringEnum('garbage')
Expand Down

0 comments on commit 1f48d76

Please sign in to comment.