11from collections .abc import Callable
22from typing import Any , Self , TypeAlias
33
4- from pydantic import BaseModel , PrivateAttr , TypeAdapter
4+ from pydantic import BaseModel , PrivateAttr
55
66CompleteModelDict : 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 )
0 commit comments