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

feat(serializer): add support for FunctionType serialization #823

Merged
merged 4 commits into from
Oct 24, 2023
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
12 changes: 12 additions & 0 deletions src/syrupy/extensions/amber/serializer.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import collections
import inspect
import os
from collections import OrderedDict
from types import (
FunctionType,
GeneratorType,
MappingProxyType,
)
Expand Down Expand Up @@ -257,6 +259,8 @@ def _serialize(
serialize_method = cls.serialize_namedtuple
elif isinstance(data, (list, tuple, GeneratorType)):
serialize_method = cls.serialize_iterable
elif isinstance(data, FunctionType):
serialize_method = cls.serialize_function
return serialize_method(**serialize_kwargs)

@classmethod
Expand Down Expand Up @@ -337,6 +341,14 @@ def serialize_dict(
**kwargs,
)

@classmethod
def serialize_function(
cls, data: FunctionType, *, depth: int = 0, **kwargs: Any
) -> str:
return cls.__serialize_plain(
data=f"{data.__qualname__}{str(inspect.signature(data))}", depth=depth
ManiacDC marked this conversation as resolved.
Show resolved Hide resolved
)

@classmethod
def serialize_unknown(cls, data: Any, *, depth: int = 0, **kwargs: Any) -> str:
if data.__class__.__repr__ != object.__repr__:
Expand Down
12 changes: 11 additions & 1 deletion src/syrupy/extensions/json/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import datetime
import inspect
import json
from collections import OrderedDict
from types import GeneratorType
from types import (
FunctionType,
GeneratorType,
)
from typing import (
TYPE_CHECKING,
Any,
Expand Down Expand Up @@ -133,6 +137,12 @@ def _filter(
if isinstance(data, (datetime.datetime,)):
return data.strftime("%Y-%m-%dT%H:%M:%S.%f%z")

if isinstance(data, FunctionType):
return (
f"<{FunctionType.__name__} "
f"{data.__qualname__}{str(inspect.signature(data))}>"
)

if data.__class__.__repr__ != object.__repr__:
return repr(data)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,12 @@
# name: test_empty_snapshot.1
''
# ---
# name: test_function_in_file
"function_to_test(var1, var2='test_val', var3: str = 'test_val2', *, kwvar1, kwvar2='some_val') -> str"
noahnu marked this conversation as resolved.
Show resolved Hide resolved
# ---
# name: test_function_local
"test_function_local.<locals>.local_function_to_test(var1, var2='test_val', var3: str = 'test_val2', *, kwvar1, kwvar2='some_val') -> int"
# ---
# name: test_list[actual0]
list([
])
Expand Down
19 changes: 19 additions & 0 deletions tests/syrupy/extensions/amber/test_amber_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,3 +233,22 @@ def test_ordered_dict(snapshot):
def test_many_sorted(snapshot):
for i in range(25):
assert i == snapshot


def function_to_test(
var1, var2="test_val", var3: str = "test_val2", *, kwvar1, kwvar2="some_val"
) -> str:
return "2"


def test_function_in_file(snapshot):
assert snapshot == function_to_test


def test_function_local(snapshot):
def local_function_to_test(
var1, var2="test_val", var3: str = "test_val2", *, kwvar1, kwvar2="some_val"
) -> int:
return 1

assert snapshot == local_function_to_test
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"<function function_to_test(var1, var2='test_val', var3: str = 'test_val2', *, kwvar1, kwvar2='some_val') -> str>"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"<function test_function_local.<locals>.local_function_to_test(var1, var2='test_val', var3: str = 'test_val2', *, kwvar1, kwvar2='some_val') -> int>"
19 changes: 19 additions & 0 deletions tests/syrupy/extensions/json/test_json_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,3 +232,22 @@ def test_ordered_dict(snapshot_json):
d["b"] = 0
d["a"] = OrderedDict(b=True, a=False)
assert snapshot_json == d


def function_to_test(
var1, var2="test_val", var3: str = "test_val2", *, kwvar1, kwvar2="some_val"
) -> str:
return "2"


def test_function_in_file(snapshot_json):
assert snapshot_json == function_to_test


def test_function_local(snapshot_json):
def local_function_to_test(
var1, var2="test_val", var3: str = "test_val2", *, kwvar1, kwvar2="some_val"
) -> int:
return 1

assert snapshot_json == local_function_to_test
Loading