Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various fixes to pytypes + typesystem #48

Merged
merged 3 commits into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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):
erezsh marked this conversation as resolved.
Show resolved Hide resolved
if isinstance(other, Type):
if other is self: # Optimization
return True
if other is self: # Optimization
return True
elif isinstance(other, (type, Type)):
erezsh marked this conversation as resolved.
Show resolved Hide resolved
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
Loading