Skip to content
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
13 changes: 10 additions & 3 deletions localstack_snapshot/snapshots/prototype.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,14 +183,21 @@ def _convert_object_to_dict(obj_):
return obj_.value
elif hasattr(obj_, "__dict__"):
# This is an object - let's try to convert it to a dictionary
# A naive approach would be to use the '__dict__' object directly, but that only lists the attributes
# A naive approach would be to use the '__dict__' object directly, but that only lists the attributes.
# In order to also serialize the properties, we use the __dir__() method
# Filtering by everything that is not a method gives us both attributes and properties
# We also (still) skip private attributes/properties, so everything that starts with an underscore
# We also (still) skip private attributes/properties, so everything that starts with an underscore.
return {
k: _convert_object_to_dict(getattr(obj_, k))
for k in obj_.__dir__()
if not k.startswith("_") and type(getattr(obj_, k, "")).__name__ != "method"
if (
# Skip private attributes
not k.startswith("_")
# Skip everything that's not a method
and type(getattr(obj_, k, "")).__name__ != "method"
# Skip everything that refers to itself (identity functions), as that leads to recursion
and getattr(obj_, k) != obj_
)
}
return obj_

Expand Down
15 changes: 15 additions & 0 deletions tests/test_snapshots.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,21 @@ def __init__(self, name):
sm.match_object("key_a", CustomObject(name="myname"))
sm._assert_all()

def test_match_object_with_identity_function(self):
class CustomObject:
def __init__(self, name):
self.name = name

@property
def me_myself_and_i(self):
# This would lead to a RecursionError, so we cannot snapshot this method
return self

sm = SnapshotSession(scope_key="A", verify=True, base_file_path="", update=False)
sm.recorded_state = {"key_a": {"name": "myname"}}
sm.match_object("key_a", CustomObject(name="myname"))
sm._assert_all()

def test_match_object_change(self):
class CustomObject:
def __init__(self, name):
Expand Down