Skip to content

Commit

Permalink
Fix eq/hash for copies of types
Browse files Browse the repository at this point in the history
  • Loading branch information
erezsh committed Oct 15, 2024
1 parent 7ff7204 commit 5d42d92
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
9 changes: 9 additions & 0 deletions runtype/base_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ def __mul__(self, other: _Type):
def __le__(self, other):
return NotImplemented

def __hash__(self):
# __eq__ defined in eq()
return hash(type(self))


class AnyType(Type):
"""Represents the Any type.
Expand Down Expand Up @@ -293,6 +298,10 @@ def ge(self, other):
def eq(self, other):
return NotImplemented

@dp
def eq(self: Type, other: Type):
return type(other) is type(self)

@dp
def eq(self: SumType, other: SumType):
return self.types == other.types
Expand Down
9 changes: 9 additions & 0 deletions runtype/pytypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,15 @@ def cast_from(self, obj):

return obj

def __eq__(self, other):
if type(other) != type(self):
return False
return self.kernel == other.kernel

def __hash__(self):
return hash((type(self), self.kernel))


class TupleType(PythonType):
def test_instance(self, obj, sampler=None):
return isinstance(obj, tuple)
Expand Down

0 comments on commit 5d42d92

Please sign in to comment.