Skip to content

Commit

Permalink
Merge pull request #48 from erezsh/dec2023
Browse files Browse the repository at this point in the history
Various fixes to pytypes + typesystem
  • Loading branch information
erezsh authored Jan 2, 2024
2 parents b4c0519 + 803a8f5 commit 5985e97
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 20 deletions.
25 changes: 5 additions & 20 deletions runtype/base_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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


Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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):
Expand All @@ -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):
Expand Down
5 changes: 5 additions & 0 deletions runtype/pytypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -427,6 +429,7 @@ def cast_from(self, obj):


String = _String(str)
Bool = _Bool(bool)
Int = _Int(int)
Float = _Float(float)
NoneType = _NoneType()
Expand All @@ -443,6 +446,8 @@ def cast_from(self, obj):
frozenset: FrozenSet,
dict: Dict,
tuple: Tuple,
bool: Bool,
None: NoneType,
int: Int,
str: String,
float: Float,
Expand Down

0 comments on commit 5985e97

Please sign in to comment.