From a67d0f5ad412cadbe82b115086e0c6945f60f179 Mon Sep 17 00:00:00 2001 From: Erez Shinan Date: Sun, 31 Dec 2023 11:05:17 +0700 Subject: [PATCH 1/3] pytypes: Added Bool --- runtype/pytypes.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/runtype/pytypes.py b/runtype/pytypes.py index 5a3b004..d6b21d5 100644 --- a/runtype/pytypes.py +++ b/runtype/pytypes.py @@ -333,6 +333,8 @@ def validate_instance(self, obj, sampler=None): Literal = OneOf Type = TypeType(PythonDataType(type)) +class _Bool(PythonDataType): + pass class _Number(PythonDataType): def __call__(self, min=None, max=None): @@ -427,6 +429,7 @@ def cast_from(self, obj): String = _String(str) +Bool = _Bool(bool) Int = _Int(int) Float = _Float(float) NoneType = _NoneType() @@ -443,6 +446,7 @@ def cast_from(self, obj): frozenset: FrozenSet, dict: Dict, tuple: Tuple, + bool: Bool, int: Int, str: String, float: Float, From c5f0898c509827d66acdd4ae443f993ddd312941 Mon Sep 17 00:00:00 2001 From: Erez Shinan Date: Sun, 31 Dec 2023 12:15:36 +0700 Subject: [PATCH 2/3] type system: Fix for Any type --- runtype/base_types.py | 25 +++++-------------------- 1 file changed, 5 insertions(+), 20 deletions(-) diff --git a/runtype/base_types.py b/runtype/base_types.py index 59ee9d2..ed5af1c 100644 --- a/runtype/base_types.py +++ b/runtype/base_types.py @@ -49,9 +49,11 @@ def __ge__(self, other): return NotImplemented def __le__(self, other): - if isinstance(other, Type): - if other is self: # Optimization - return True + if other is self: # Optimization + return True + elif isinstance(other, (type, Type)): + if not isinstance(other, SumType): + return False return NotImplemented @@ -74,9 +76,6 @@ def __le__(self, other): return super().__le__(other) def __ge__(self, other): - # XXX hack - if isinstance(other, AnyType): - return False return NotImplemented @@ -177,12 +176,6 @@ class ContainerType(DataType): def __getitem__(self, other): return GenericType(self, other) - def __le__(self, other): - # XXX hack - if isinstance(other, AnyType): - return True - return super().__le__(other) - class GenericType(ContainerType): """Implements a generic type. i.e. a container for items of a specific type. @@ -226,10 +219,6 @@ def __le__(self, other): elif isinstance(other, DataType): return self.base <= other - elif isinstance(other, AnyType): - # HACK - return True - return NotImplemented def __ge__(self, other): @@ -239,10 +228,6 @@ def __ge__(self, other): elif isinstance(other, DataType): return self.base >= other - elif isinstance(other, AnyType): - # HACK - return False - return NotImplemented def __hash__(self): From 803a8f50e435e70efbdec8f54412379a4d63ac5a Mon Sep 17 00:00:00 2001 From: Erez Shinan Date: Tue, 2 Jan 2024 11:32:58 +0700 Subject: [PATCH 3/3] pytypes: Added None as a to_canon type --- runtype/pytypes.py | 1 + 1 file changed, 1 insertion(+) diff --git a/runtype/pytypes.py b/runtype/pytypes.py index d6b21d5..c556574 100644 --- a/runtype/pytypes.py +++ b/runtype/pytypes.py @@ -447,6 +447,7 @@ def cast_from(self, obj): dict: Dict, tuple: Tuple, bool: Bool, + None: NoneType, int: Int, str: String, float: Float,