Skip to content

Commit

Permalink
Further tests for bound methods and numpy objects
Browse files Browse the repository at this point in the history
Tests that the reduce protocol is properly implemented, and
that issue #107 is fixed.
  • Loading branch information
William Grant committed Feb 16, 2023
1 parent 22f01c5 commit a636a3a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
14 changes: 12 additions & 2 deletions typed_python/compiler/tests/numpy_interaction_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,20 @@ def test_listof_from_sliced_numpy_array():
assert ListOf(int)(y) == [0, 2]


def test_can_serialize_numpy_ufunc():
def test_can_serialize_numpy_ufuncs():
assert numpy.sin == SerializationContext().deserialize(SerializationContext().serialize(numpy.sin))
assert numpy.max == SerializationContext().deserialize(SerializationContext().serialize(numpy.max))


def test_can_serialize_numpy_array():
def test_can_serialize_numpy_array_from_builtin():
x = numpy.ones(10)
assert (x == SerializationContext().deserialize(SerializationContext().serialize(x))).all()


def test_can_serialize_numpy_array_from_list():
x = numpy.array([1, 2, 3])
assert (x == SerializationContext().deserialize(SerializationContext().serialize(x))).all()


def test_can_serialize_numpy_array_constructor():
assert numpy.array == SerializationContext().deserialize(SerializationContext().serialize(numpy.array))
23 changes: 23 additions & 0 deletions typed_python/types_serialization_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1339,6 +1339,29 @@ def f(self, x=10):

self.assertLess(currentMemUsageMb(), usage+.5)

def test_serialize_class_with_bound_methods(self):
class SomeClass:
pass

class SomeSubclass(SomeClass):
def __init__(self, x):
self.x = x

class ClassWithBoundMethod(Class, Final):
x = Member(OneOf(None, SomeClass))

def __init__(self):
self.x = None

def increment(self, y):
if self.x is None:
self.x = SomeSubclass(y)
else:
self.x = SomeSubclass(self.x.x + y)

self.assertEqual(ClassWithBoundMethod, ping_pong(ClassWithBoundMethod))
self.assertEqual(ClassWithBoundMethod.increment, ping_pong(ClassWithBoundMethod.increment))

def test_serialize_named_tuples_with_extra_fields(self):
T1 = NamedTuple(x=int)
T2 = NamedTuple(x=int, y=float, z=str)
Expand Down

0 comments on commit a636a3a

Please sign in to comment.