Skip to content

Commit b85a2e1

Browse files
committed
fix(get_instance): return None with invalid id
1 parent e844011 commit b85a2e1

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

src/trame_dataclass/v2.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ def decode_dataclass_dict(data):
442442
def get_instance(instance_id: str):
443443
# print(f"get_instance({instance_id})")
444444
# print(" => ", INSTANCES[instance_id])
445-
return INSTANCES[instance_id]
445+
return INSTANCES.get(instance_id)
446446

447447

448448
class ServerOnly:

tests/test_v2.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
StateDataModel,
66
Sync,
77
TypeValidation,
8+
get_instance,
89
)
910

1011

@@ -82,3 +83,12 @@ class BasicSerial(StateDataModel):
8283
assert isinstance(set_as_list, list)
8384
for obj in data.f:
8485
assert obj._id in set_as_list
86+
87+
88+
def test_get_instance():
89+
class Dummy(StateDataModel):
90+
a = Sync(int, 0)
91+
92+
inst = Dummy()
93+
assert get_instance(inst._id) is inst
94+
assert get_instance("not an id") is None

0 commit comments

Comments
 (0)