Skip to content

Commit ae1db7c

Browse files
committed
update coverage and lock
1 parent 23e4cdf commit ae1db7c

7 files changed

Lines changed: 845 additions & 754 deletions

File tree

ormar/fields/parsers.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
from ormar.utils.rust_utils import HAS_RUST, ormar_rust_utils
1616

17-
if HAS_RUST:
17+
if HAS_RUST: # pragma: no cover
1818
_rs_encode_bytes = ormar_rust_utils.encode_bytes
1919
_rs_decode_bytes = ormar_rust_utils.decode_bytes
2020
_rs_encode_json = ormar_rust_utils.encode_json
@@ -34,7 +34,7 @@ def encode_decimal(value: decimal.Decimal, precision: Optional[int] = None) -> f
3434
)
3535

3636

37-
def _py_encode_bytes(
37+
def _py_encode_bytes( # pragma: no cover
3838
value: Union[str, bytes], represent_as_string: bool = False
3939
) -> str:
4040
if represent_as_string:
@@ -46,21 +46,23 @@ def _py_encode_bytes(
4646
return value
4747

4848

49-
def _py_decode_bytes(value: str, represent_as_string: bool = False) -> bytes:
49+
def _py_decode_bytes(
50+
value: str, represent_as_string: bool = False
51+
) -> bytes: # pragma: no cover
5052
if represent_as_string:
5153
return value if isinstance(value, bytes) else base64.b64decode(value)
5254
return value if isinstance(value, bytes) else value.encode("utf-8")
5355

5456

55-
def _py_encode_json(value: Any) -> Optional[str]:
57+
def _py_encode_json(value: Any) -> Optional[str]: # pragma: no cover
5658
if isinstance(value, (datetime.date, datetime.datetime, datetime.time)):
5759
value = value.isoformat()
5860
value = json.dumps(value) if not isinstance(value, str) else re_dump_value(value)
5961
value = value.decode("utf-8") if isinstance(value, bytes) else value
6062
return value
6163

6264

63-
def re_dump_value(value: str) -> Union[str, bytes]:
65+
def re_dump_value(value: str) -> Union[str, bytes]: # pragma: no cover
6466
"""
6567
Re-dumps value due to different string representation in orjson and json
6668
:param value: string to re-dump

ormar/models/helpers/models.py

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from ormar.models.helpers.pydantic import populate_pydantic_default_values
77
from ormar.utils.rust_utils import HAS_RUST, ormar_rust_utils
88

9-
if HAS_RUST:
9+
if HAS_RUST: # pragma: no cover
1010
_rs_group_related_list = ormar_rust_utils.group_related_list
1111

1212
if TYPE_CHECKING: # pragma no cover
@@ -115,23 +115,24 @@ def group_related_list(list_: list) -> dict:
115115
:return: list converted to dictionary to avoid repetition and group nested models
116116
:rtype: dict[str, list]
117117
"""
118-
if HAS_RUST:
118+
if HAS_RUST: # pragma: no cover
119119
return _rs_group_related_list(list_)
120-
groups: dict[str, list[str]] = {}
121-
for item in list_:
122-
head, _, tail = item.partition("__")
123-
groups.setdefault(head, [])
124-
if tail:
125-
groups[head].append(tail)
126-
127-
result_dict: dict[str, Any] = {}
128-
for key in sorted(groups):
129-
children = sorted(groups[key])
130-
if any("__" in x for x in children):
131-
result_dict[key] = group_related_list(children)
132-
else:
133-
result_dict.setdefault(key, []).extend(children)
134-
return dict(sorted(result_dict.items(), key=lambda item: len(item[1])))
120+
else: # pragma: no cover
121+
groups: dict[str, list[str]] = {}
122+
for item in list_:
123+
head, _, tail = item.partition("__")
124+
groups.setdefault(head, [])
125+
if tail:
126+
groups[head].append(tail)
127+
128+
result_dict: dict[str, Any] = {}
129+
for key in sorted(groups):
130+
children = sorted(groups[key])
131+
if any("__" in x for x in children):
132+
result_dict[key] = group_related_list(children)
133+
else:
134+
result_dict.setdefault(key, []).extend(children)
135+
return dict(sorted(result_dict.items(), key=lambda item: len(item[1])))
135136

136137

137138
def config_field_not_set(model: type["Model"], field_name: str) -> bool:

ormar/models/mixins/merge_mixin.py

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from ormar.queryset.utils import translate_list_to_dict
55
from 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

ormar/queryset/queries/prefetch_query.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from ormar.queryset.utils import translate_list_to_dict
1010
from ormar.utils.rust_utils import HAS_RUST, ormar_rust_utils
1111

12-
if HAS_RUST:
12+
if HAS_RUST: # pragma: no cover
1313
_RsUniqueList = ormar_rust_utils.UniqueList
1414
_rs_hash_item = ormar_rust_utils.hash_item
1515

@@ -21,7 +21,7 @@
2121
logger = logging.getLogger(__name__)
2222

2323

24-
class _PyUniqueList(list):
24+
class _PyUniqueList(list): # pragma: no cover
2525
"""
2626
Simple subclass of list that prevents the duplicates.
2727
Uses a set for O(1) membership checks instead of O(n) list scan.
@@ -413,7 +413,7 @@ def _instantiate_models(self) -> None:
413413
)
414414
self.models.append(instance)
415415

416-
def _hash_item(self, item: Union[dict, list]) -> tuple:
416+
def _hash_item(self, item: Union[dict, list]) -> tuple: # pragma: no cover
417417
"""
418418
Converts model dictionary or list into a hashable tuple to allow its use
419419
as a dictionary key - used to ensure unique instances of related models.

ormar/queryset/utils.py

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44

55
from ormar.utils.rust_utils import HAS_RUST, ormar_rust_utils
66

7-
if HAS_RUST:
7+
if HAS_RUST: # pragma: no cover
88
_rs_translate_list_to_dict = ormar_rust_utils.translate_list_to_dict
99

1010
if TYPE_CHECKING: # pragma no cover
1111
from ormar import BaseField, Model
1212

1313

14-
def check_node_not_dict_or_not_last_node(
14+
def check_node_not_dict_or_not_last_node( # pragma: no cover
1515
part: str, is_last: bool, current_level: Any
1616
) -> bool:
1717
"""
@@ -55,24 +55,25 @@ def translate_list_to_dict( # noqa: CCR001
5555
:return: converted to dictionary input list
5656
:rtype: dict
5757
"""
58-
if HAS_RUST:
58+
if HAS_RUST: # pragma: no cover
5959
return _rs_translate_list_to_dict(list(list_to_trans), default)
60-
is_ellipsis = default is ...
61-
new_dict: dict = dict()
62-
for path in list_to_trans:
63-
current_level = new_dict
64-
parts = path.split("__")
65-
def_val: Any = default if is_ellipsis else copy.deepcopy(default)
66-
for ind, part in enumerate(parts):
67-
is_last = ind == len(parts) - 1
68-
if check_node_not_dict_or_not_last_node(
69-
part=part, is_last=is_last, current_level=current_level
70-
):
71-
current_level[part] = dict()
72-
elif part not in current_level:
73-
current_level[part] = def_val
74-
current_level = current_level[part]
75-
return new_dict
60+
else: # pragma: no cover
61+
is_ellipsis = default is ...
62+
new_dict: dict = dict()
63+
for path in list_to_trans:
64+
current_level = new_dict
65+
parts = path.split("__")
66+
def_val: Any = default if is_ellipsis else copy.deepcopy(default)
67+
for ind, part in enumerate(parts):
68+
is_last = ind == len(parts) - 1
69+
if check_node_not_dict_or_not_last_node(
70+
part=part, is_last=is_last, current_level=current_level
71+
):
72+
current_level[part] = dict()
73+
elif part not in current_level:
74+
current_level[part] = def_val
75+
current_level = current_level[part]
76+
return new_dict
7677

7778

7879
def convert_set_to_required_dict(set_to_convert: set) -> dict:

ormar/utils/rust_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Centralized import for the optional ormar_rust_utils Rust accelerator."""
22

3-
try:
3+
try: # pragma: no cover
44
import ormar_rust_utils
55

66
HAS_RUST = True

0 commit comments

Comments
 (0)