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

Further tests for bound methods and numpy objects #437

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
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
21 changes: 20 additions & 1 deletion typed_python/compiler/tests/numpy_interaction_test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typed_python import ListOf, Entrypoint
from typed_python import ListOf, Entrypoint, SerializationContext
import numpy
import numpy.linalg

Expand Down Expand Up @@ -44,3 +44,22 @@ def test_listof_from_sliced_numpy_array():
y = x[::2]

assert ListOf(int)(y) == [0, 2]


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_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