Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix parsing of $not expression on command line #970

Merged
merged 9 commits into from
Feb 2, 2024
4 changes: 4 additions & 0 deletions signac/filterparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,8 @@

def _add_prefix(filter):
"""Add prefix "sp." to a (possibly nested) filter."""

# Logical operators ($and, $or, $not) should not be prefixed, but their values should.
for key, value in filter.items():
if key in ("$and", "$or"):
if isinstance(value, list) or isinstance(value, tuple):
Expand All @@ -203,6 +205,8 @@
raise ValueError(
"The argument to a logical operator must be a list or a tuple!"
)
elif key == "$not":
cbkerr marked this conversation as resolved.
Show resolved Hide resolved
yield key, dict(_add_prefix(value))

Check warning on line 209 in signac/filterparse.py

View check run for this annotation

Codecov / codecov/patch

signac/filterparse.py#L209

Added line #L209 was not covered by tests
elif "." in key and key.split(".", 1)[0] in ("sp", "doc"):
yield key, value
elif key in ("sp", "doc"):
Expand Down
3 changes: 3 additions & 0 deletions tests/test_find_command_line_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

from signac.filterparse import parse_filter_arg, parse_simple

# also used in test_shell
FILTERS = [
{"a": 0},
{"a.b": 0},
Expand Down Expand Up @@ -70,6 +71,8 @@ def _parse(args):

def test_interpret_json(self):
def _assert_equal(q):
# TODO: full code path not tested with this test.
# _assert_equal and _find_expression, are not tested
assert q == self._parse([json.dumps(q)])

for f in FILTERS:
Expand Down
7 changes: 7 additions & 0 deletions tests/test_shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from tempfile import TemporaryDirectory

import pytest
from test_find_command_line_interface import FILTERS
from test_project import WINDOWS, _initialize_v1_project, skip_windows_without_symlinks

import signac
Expand Down Expand Up @@ -280,6 +281,12 @@ def test_find(self):
== [job.id for job in project.find_jobs({"doc.b": i})][0]
)

# ensure that there are no errors due to adding sp and doc prefixes
# by testing on all the example complex expressions
for f in FILTERS:
command = "python -m signac find ".split() + [json.dumps(f)]
self.call(command).strip()

def test_diff(self):
self.call("python -m signac init".split())
project = signac.Project()
Expand Down
Loading