44from ormar .queryset .utils import translate_list_to_dict
55from ormar .utils .rust_utils import HAS_RUST , ormar_rust_utils
66
7- if HAS_RUST :
7+ if HAS_RUST : # pragma: no cover
88 _rs_group_by_pk = ormar_rust_utils .group_by_pk
99 _rs_plan_merge = ormar_rust_utils .plan_merge_items_lists
1010
@@ -62,14 +62,14 @@ def merge_instances_list(cls, result_rows: list["Model"]) -> list["Model"]:
6262 """
6363 merged_rows : list ["Model" ] = []
6464
65- if HAS_RUST and result_rows :
65+ if HAS_RUST and result_rows : # pragma: no cover
6666 pks = [model .pk for model in result_rows ]
6767 index_groups = _rs_group_by_pk (pks )
6868 for group_indices in index_groups :
6969 group = [result_rows [i ] for i in group_indices ]
7070 model = cls ._recursive_add (group )[0 ]
7171 merged_rows .append (model )
72- else :
72+ else : # pragma: no cover
7373 grouped_instances : dict = {}
7474 for model in result_rows :
7575 grouped_instances .setdefault (model .pk , []).append (model )
@@ -161,7 +161,7 @@ def _merge_items_lists(
161161 :return: merged list of models
162162 :rtype: list[Model]
163163 """
164- if HAS_RUST :
164+ if HAS_RUST : # pragma: no cover
165165 current_pks = [getattr (m , "pk" , None ) for m in current_field ]
166166 other_pks = [getattr (m , "pk" , None ) for m in other_value ]
167167 plan = _rs_plan_merge (current_pks , other_pks )
@@ -183,26 +183,26 @@ def _merge_items_lists(
183183 else :
184184 value_to_set .append (cur_item )
185185 return value_to_set
186+ else : # pragma: no cover
187+ other_by_pk : dict = {}
188+ for idx , item in enumerate (other_value ):
189+ other_by_pk .setdefault (item .pk , idx )
186190
187- other_by_pk : dict = {}
188- for idx , item in enumerate (other_value ):
189- other_by_pk .setdefault (item .pk , idx )
190-
191- value_to_set = list (other_value )
192- for cur_field in current_field :
193- other_idx = other_by_pk .get (cur_field .pk )
194- if other_idx is not None :
195- old_value = other_value [other_idx ]
196- new_val = cls .merge_two_instances (
197- cur_field ,
198- cast ("Model" , old_value ),
199- relation_map = cur_field ._skip_ellipsis ( # type: ignore
200- relation_map , field_name , default_return = dict ()
201- ),
202- )
203- value_to_set = [x for x in value_to_set if x .pk != cur_field .pk ] + [
204- new_val
205- ]
206- else :
207- value_to_set .append (cur_field )
208- return value_to_set
191+ value_to_set = list (other_value )
192+ for cur_field in current_field :
193+ other_idx = other_by_pk .get (cur_field .pk )
194+ if other_idx is not None :
195+ old_value = other_value [other_idx ]
196+ new_val = cls .merge_two_instances (
197+ cur_field ,
198+ cast ("Model" , old_value ),
199+ relation_map = cur_field ._skip_ellipsis ( # type: ignore
200+ relation_map , field_name , default_return = dict ()
201+ ),
202+ )
203+ value_to_set = [x for x in value_to_set if x .pk != cur_field .pk ] + [
204+ new_val
205+ ]
206+ else :
207+ value_to_set .append (cur_field )
208+ return value_to_set
0 commit comments