Skip to content

Commit 488661f

Browse files
author
Andrei Neagu
committed
refactor
1 parent 2f29be2 commit 488661f

File tree

2 files changed

+26
-58
lines changed

2 files changed

+26
-58
lines changed
Lines changed: 12 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from collections.abc import Callable
22
from typing import Any, Self, TypeAlias
33

4-
from pydantic import BaseModel, PrivateAttr, TypeAdapter
4+
from pydantic import BaseModel, PrivateAttr
55

66
CompleteModelDict: TypeAlias = dict[str, Any]
77

@@ -12,44 +12,27 @@ class BaseUpdatableDisplayModel(BaseModel):
1212
default_factory=dict
1313
)
1414

15-
def _get_on_change_callbacks_to_run(
16-
self, updates: CompleteModelDict
17-
) -> list[Callable]:
15+
def _get_on_change_callbacks_to_run(self, update_obj: Self) -> list[Callable]:
1816
callbaks_to_run: list[Callable] = []
19-
for attribute, callback in self._on_value_change_subscribers.items():
20-
21-
if (
22-
attribute in updates
23-
and attribute in self.__dict__
24-
and _are_values_different(
25-
self.__dict__[attribute],
26-
updates[attribute],
27-
self.__class__.model_fields[attribute].annotation,
28-
)
29-
):
17+
18+
for attribute_name, callback in self._on_value_change_subscribers.items():
19+
if getattr(self, attribute_name) != getattr(update_obj, attribute_name):
3020
callbaks_to_run.append(callback)
3121

32-
for attribute, callback in self._on_type_change_subscribers.items():
33-
if (
34-
attribute in updates
35-
and attribute in self.__dict__
36-
and _are_types_different(
37-
self.__dict__[attribute],
38-
updates[attribute],
39-
self.__class__.model_fields[attribute].annotation,
40-
)
22+
for attribute_name, callback in self._on_type_change_subscribers.items():
23+
if type(getattr(self, attribute_name)) is not type(
24+
getattr(update_obj, attribute_name)
4125
):
4226
callbaks_to_run.append(callback)
4327

4428
return callbaks_to_run
4529

4630
def update(self, update_obj: Self) -> None:
47-
callbacks_to_run = self._get_on_change_callbacks_to_run(update_obj.__dict__)
31+
callbacks_to_run = self._get_on_change_callbacks_to_run(update_obj)
4832

49-
current = self.__dict__
50-
for attribute_name, value in update_obj.__dict__.items():
51-
if attribute_name in current and current[attribute_name] != value:
52-
setattr(self, attribute_name, value)
33+
for attribute_name, update_value in update_obj.__dict__.items():
34+
if getattr(self, attribute_name) != update_value:
35+
setattr(self, attribute_name, update_value)
5336

5437
for callback in callbacks_to_run:
5538
callback()
@@ -70,26 +53,3 @@ def on_value_change(self, attribute: str, callback: Callable) -> None:
7053
self._raise_if_attribute_not_declared_in_model(attribute)
7154

7255
self._on_value_change_subscribers[attribute] = callback
73-
74-
75-
def _are_values_different(
76-
current_value: BaseUpdatableDisplayModel | dict,
77-
update_value: dict,
78-
annotation: type | None,
79-
) -> bool:
80-
if isinstance(current_value, BaseUpdatableDisplayModel):
81-
return current_value != TypeAdapter(annotation).validate_python(update_value)
82-
83-
return current_value != update_value
84-
85-
86-
def _are_types_different(
87-
current_value: BaseUpdatableDisplayModel | dict,
88-
update_value: dict,
89-
annotation: type | None,
90-
) -> bool:
91-
if isinstance(current_value, BaseUpdatableDisplayModel):
92-
return type(current_value) is not type(
93-
TypeAdapter(annotation).validate_python(update_value)
94-
)
95-
return type(current_value) is not type(update_value)

services/dynamic-scheduler/tests/unit/api_frontend/_common/test_base_display_model.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,11 @@ class RenderOnPropertyTypeChange(BaseUpdatableDisplayModel):
4949
"age": 30,
5050
"companion": {"name": "Fluffy", "species": "cat"},
5151
},
52-
{"age": 30, "companion": {"name": "Fido", "species": "dog"}},
52+
{
53+
"name": "Alice",
54+
"age": 30,
55+
"companion": {"name": "Fido", "species": "dog"},
56+
},
5357
{
5458
"name": "Alice",
5559
"age": 30,
@@ -87,7 +91,11 @@ class RenderOnPropertyTypeChange(BaseUpdatableDisplayModel):
8791
"age": 30,
8892
"companion": {"name": "Fluffy", "species": "cat"},
8993
},
90-
{"age": 31, "companion": {"name": "Fido", "species": "dog"}},
94+
{
95+
"name": "Alice",
96+
"age": 31,
97+
"companion": {"name": "Fido", "species": "dog"},
98+
},
9199
{
92100
"name": "Alice",
93101
"age": 31,
@@ -125,7 +133,7 @@ class RenderOnPropertyTypeChange(BaseUpdatableDisplayModel):
125133
"age": 30,
126134
"companion": {"name": "Fluffy", "species": "cat"},
127135
},
128-
{"age": 31, "companion": {"name": "Charlie", "age": 25}},
136+
{"name": "Alice", "age": 31, "companion": {"name": "Charlie", "age": 25}},
129137
{"name": "Alice", "age": 31, "companion": {"name": "Charlie", "age": 25}},
130138
{"companion": 1},
131139
{},
@@ -147,16 +155,16 @@ def test_base_updatable_display_model(
147155
subscribed_on_type_changed: dict[str, Mock] = {}
148156
for attribute in on_type_change:
149157
mock = Mock()
150-
person.on_type_change("companion", mock)
158+
person.on_type_change(attribute, mock)
151159
subscribed_on_type_changed[attribute] = mock
152160

153161
subscribed_on_value_change: dict[str, Mock] = {}
154162
for attribute in on_value_change:
155163
mock = Mock()
156-
person.on_value_change("companion", mock)
164+
person.on_value_change(attribute, mock)
157165
subscribed_on_value_change[attribute] = mock
158166

159-
person.update(update_dict)
167+
person.update(TypeAdapter(class_).validate_python(update_dict))
160168
assert person.model_dump() == expected_dict
161169

162170
for attribute, mock in subscribed_on_type_changed.items():

0 commit comments

Comments
 (0)