From 4d544994fd5aaa1dd469f3f4bcc48440aec75d24 Mon Sep 17 00:00:00 2001 From: Justin Black Date: Mon, 23 May 2022 15:55:49 -0700 Subject: [PATCH] [python-experimental] BoolSchema + NoneSchema improvements (#12411) * Moves is_none is_true is_false methods into base mixins, adds tests * Improves docstrings --- .../python-experimental/schemas.handlebars | 41 ++++++++++++------- .../petstore_api/schemas.py | 41 ++++++++++++------- .../tests_manual/test_boolean_enum.py | 2 + .../tests_manual/test_composed_bool.py | 19 ++++++--- .../tests_manual/test_nullable_string.py | 1 + 5 files changed, 68 insertions(+), 36 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/python-experimental/schemas.handlebars b/modules/openapi-generator/src/main/resources/python-experimental/schemas.handlebars index ecfaae2e66c8..f9a69ff2b751 100644 --- a/modules/openapi-generator/src/main/resources/python-experimental/schemas.handlebars +++ b/modules/openapi-generator/src/main/resources/python-experimental/schemas.handlebars @@ -471,10 +471,7 @@ class Singleton: """ _instances = {} - def __new__(cls, *args, **kwargs): - if not args: - raise ValueError('arg must be passed') - arg = args[0] + def __new__(cls, arg: typing.Any, **kwargs): key = (cls, arg) if key not in cls._instances: if arg in {None, True, False}: @@ -501,9 +498,6 @@ class NoneClass(Singleton): def NONE(cls): return cls(None) - def is_none(self) -> bool: - return True - def __bool__(self) -> bool: return False @@ -526,19 +520,36 @@ class BoolClass(Singleton): return key[1] raise ValueError('Unable to find the boolean value of this instance') - def is_true(self): - return bool(self) - - def is_false(self): - return bool(self) - class BoolBase: - pass + def is_true(self) -> bool: + """ + A replacement for x is True + True if the instance is a BoolClass True Singleton + """ + if not issubclass(self.__class__, BoolClass): + return False + return bool(self) + + def is_false(self) -> bool: + """ + A replacement for x is False + True if the instance is a BoolClass False Singleton + """ + if not issubclass(self.__class__, BoolClass): + return False + return bool(self) is False class NoneBase: - pass + def is_none(self) -> bool: + """ + A replacement for x is None + True if the instance is a NoneClass None Singleton + """ + if issubclass(self.__class__, NoneClass): + return True + return False class StrBase: diff --git a/samples/openapi3/client/petstore/python-experimental/petstore_api/schemas.py b/samples/openapi3/client/petstore/python-experimental/petstore_api/schemas.py index 96887c212fd7..10985d6986ca 100644 --- a/samples/openapi3/client/petstore/python-experimental/petstore_api/schemas.py +++ b/samples/openapi3/client/petstore/python-experimental/petstore_api/schemas.py @@ -478,10 +478,7 @@ class Singleton: """ _instances = {} - def __new__(cls, *args, **kwargs): - if not args: - raise ValueError('arg must be passed') - arg = args[0] + def __new__(cls, arg: typing.Any, **kwargs): key = (cls, arg) if key not in cls._instances: if arg in {None, True, False}: @@ -508,9 +505,6 @@ class NoneClass(Singleton): def NONE(cls): return cls(None) - def is_none(self) -> bool: - return True - def __bool__(self) -> bool: return False @@ -533,19 +527,36 @@ class BoolClass(Singleton): return key[1] raise ValueError('Unable to find the boolean value of this instance') - def is_true(self): - return bool(self) - - def is_false(self): - return bool(self) - class BoolBase: - pass + def is_true(self) -> bool: + """ + A replacement for x is True + True if the instance is a BoolClass True Singleton + """ + if not issubclass(self.__class__, BoolClass): + return False + return bool(self) + + def is_false(self) -> bool: + """ + A replacement for x is False + True if the instance is a BoolClass False Singleton + """ + if not issubclass(self.__class__, BoolClass): + return False + return bool(self) is False class NoneBase: - pass + def is_none(self) -> bool: + """ + A replacement for x is None + True if the instance is a NoneClass None Singleton + """ + if issubclass(self.__class__, NoneClass): + return True + return False class StrBase: diff --git a/samples/openapi3/client/petstore/python-experimental/tests_manual/test_boolean_enum.py b/samples/openapi3/client/petstore/python-experimental/tests_manual/test_boolean_enum.py index a8c835b1e4a6..811a18defed1 100644 --- a/samples/openapi3/client/petstore/python-experimental/tests_manual/test_boolean_enum.py +++ b/samples/openapi3/client/petstore/python-experimental/tests_manual/test_boolean_enum.py @@ -29,6 +29,8 @@ class TestBooleanEnum(unittest.TestCase): """Test BooleanEnum""" model = BooleanEnum(True) assert model is BooleanEnum.TRUE + assert model.is_true() + assert model.is_false() is False assert repr(model) == '' with self.assertRaises(petstore_api.ApiValueError): BooleanEnum(False) diff --git a/samples/openapi3/client/petstore/python-experimental/tests_manual/test_composed_bool.py b/samples/openapi3/client/petstore/python-experimental/tests_manual/test_composed_bool.py index 7f0b29ee909d..6e931e9a94ab 100644 --- a/samples/openapi3/client/petstore/python-experimental/tests_manual/test_composed_bool.py +++ b/samples/openapi3/client/petstore/python-experimental/tests_manual/test_composed_bool.py @@ -10,7 +10,6 @@ """ -import sys import unittest import petstore_api @@ -28,14 +27,22 @@ class TestComposedBool(unittest.TestCase): def test_ComposedBool(self): """Test ComposedBool""" - valid_values = [True, False] all_values = [None, True, False, 2, 3.14, '', {}, []] for value in all_values: - if value not in valid_values: - with self.assertRaises(petstore_api.ApiTypeError): - model = ComposedBool(value) + if isinstance(value, bool): + model = ComposedBool(value) + if value is True: + self.assertTrue(bool(model)) + self.assertTrue(model.is_true()) + self.assertFalse(model.is_false()) + else: + self.assertTrue(model.is_false()) + self.assertFalse(model.is_true()) + self.assertFalse(bool(model)) continue - model = ComposedBool(value) + with self.assertRaises(petstore_api.ApiTypeError): + ComposedBool(value) + continue if __name__ == '__main__': diff --git a/samples/openapi3/client/petstore/python-experimental/tests_manual/test_nullable_string.py b/samples/openapi3/client/petstore/python-experimental/tests_manual/test_nullable_string.py index 206e84f4b12b..d873e45531b6 100644 --- a/samples/openapi3/client/petstore/python-experimental/tests_manual/test_nullable_string.py +++ b/samples/openapi3/client/petstore/python-experimental/tests_manual/test_nullable_string.py @@ -36,6 +36,7 @@ class TestNullableString(unittest.TestCase): assert repr(inst) == '' inst = NullableString('approved') + assert inst.is_none() is False assert isinstance(inst, NullableString) assert isinstance(inst, Schema) assert isinstance(inst, str)