[python-experimental] fixes bug where some singleton representations raised a RecursionError (#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
This commit is contained in:
Justin Black
2022-04-17 14:06:17 -07:00
committed by GitHub
parent e2e7609bac
commit 6a77660b08
5 changed files with 22 additions and 13 deletions

View File

@@ -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
@@ -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):
@@ -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):

View File

@@ -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
@@ -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):
@@ -495,7 +493,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):

View File

@@ -10,7 +10,6 @@
"""
import sys
import unittest
import petstore_api
@@ -28,9 +27,10 @@ class TestBooleanEnum(unittest.TestCase):
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)

View File

@@ -10,7 +10,6 @@
"""
import sys
import unittest
import petstore_api
@@ -34,6 +33,7 @@ class TestNullableString(unittest.TestCase):
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)

View File

@@ -10,7 +10,6 @@
"""
import sys
import unittest
import petstore_api
@@ -32,12 +31,14 @@ class TestStringEnum(unittest.TestCase):
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')