[python-experimental] BoolSchema + NoneSchema improvements (#12411)

* Moves is_none is_true is_false methods into base mixins, adds tests

* Improves docstrings
This commit is contained in:
Justin Black
2022-05-23 15:55:49 -07:00
committed by GitHub
parent 74f377ec40
commit 4d544994fd
5 changed files with 68 additions and 36 deletions

View File

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

View File

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

View File

@@ -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) == '<DynamicBooleanEnum: True>'
with self.assertRaises(petstore_api.ApiValueError):
BooleanEnum(False)

View File

@@ -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__':

View File

@@ -36,6 +36,7 @@ class TestNullableString(unittest.TestCase):
assert repr(inst) == '<DynamicNullableString: None>'
inst = NullableString('approved')
assert inst.is_none() is False
assert isinstance(inst, NullableString)
assert isinstance(inst, Schema)
assert isinstance(inst, str)