Skip to content

Commit 2ef95dc

Browse files
committed
Reduced method complexity and fixed test
1 parent 9a946b3 commit 2ef95dc

File tree

4 files changed

+22
-20
lines changed

4 files changed

+22
-20
lines changed

neomodel/async_/match.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -803,20 +803,21 @@ def _parse_q_filters(
803803
self, ident: str, q: Union[QBase, Any], source_class: type[AsyncStructuredNode]
804804
) -> tuple[str, str]:
805805
target: list[tuple[str, bool]] = []
806+
807+
def add_to_target(statement: str, connector: Q, optional: bool) -> None:
808+
if not statement:
809+
return
810+
if connector == Q.OR:
811+
statement = f"({statement})"
812+
target.append((statement, optional))
813+
806814
for child in q.children:
807815
if isinstance(child, QBase):
808816
q_childs, q_opt_childs = self._parse_q_filters(
809817
ident, child, source_class
810818
)
811-
if child.connector == Q.OR:
812-
if q_childs:
813-
q_childs = f"({q_childs})"
814-
if q_opt_childs:
815-
q_opt_childs = f"({q_opt_childs})"
816-
if q_childs:
817-
target.append((q_childs, False))
818-
if q_opt_childs:
819-
target.append((q_opt_childs, True))
819+
add_to_target(q_childs, child.connector, False)
820+
add_to_target(q_opt_childs, child.connector, True)
820821
else:
821822
kwargs = {child[0]: child[1]}
822823
filters = process_filter_args(source_class, kwargs)

neomodel/sync_/match.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -801,20 +801,21 @@ def _parse_q_filters(
801801
self, ident: str, q: Union[QBase, Any], source_class: type[StructuredNode]
802802
) -> tuple[str, str]:
803803
target: list[tuple[str, bool]] = []
804+
805+
def add_to_target(statement: str, connector: Q, optional: bool) -> None:
806+
if not statement:
807+
return
808+
if connector == Q.OR:
809+
statement = f"({statement})"
810+
target.append((statement, optional))
811+
804812
for child in q.children:
805813
if isinstance(child, QBase):
806814
q_childs, q_opt_childs = self._parse_q_filters(
807815
ident, child, source_class
808816
)
809-
if child.connector == Q.OR:
810-
if q_childs:
811-
q_childs = f"({q_childs})"
812-
if q_opt_childs:
813-
q_opt_childs = f"({q_opt_childs})"
814-
if q_childs:
815-
target.append((q_childs, False))
816-
if q_opt_childs:
817-
target.append((q_opt_childs, True))
817+
add_to_target(q_childs, child.connector, False)
818+
add_to_target(q_opt_childs, child.connector, True)
818819
else:
819820
kwargs = {child[0]: child[1]}
820821
filters = process_filter_args(source_class, kwargs)

test/async_/test_match_api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,7 @@ async def test_q_filters():
551551
):
552552
await Coffee.nodes.fetch_relations(Optional("species")).filter(
553553
Q(name="Latte") | Q(species__name="Robusta")
554-
)
554+
).all()
555555

556556
class QQ:
557557
pass

test/sync_/test_match_api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,7 @@ def test_q_filters():
547547
):
548548
Coffee.nodes.fetch_relations(Optional("species")).filter(
549549
Q(name="Latte") | Q(species__name="Robusta")
550-
)
550+
).all()
551551

552552
class QQ:
553553
pass

0 commit comments

Comments
 (0)