From 7120786e1d5ba719462f1cf1d5a8bcd21d249fd3 Mon Sep 17 00:00:00 2001 From: Matthieu Monsch <1216372+mtth@users.noreply.github.com> Date: Mon, 17 Jul 2023 20:16:04 -0700 Subject: [PATCH] Fix subtraction precedence (#99) --- opvious/modeling/ast.py | 2 +- pyproject.toml | 2 +- tests/test_modeling.py | 18 ++++++++++++++++++ 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/opvious/modeling/ast.py b/opvious/modeling/ast.py index 60f13f2..61c5f4f 100644 --- a/opvious/modeling/ast.py +++ b/opvious/modeling/ast.py @@ -228,7 +228,7 @@ def render(self, _precedence=0) -> str: "mul": (4, 4, 4), "add": (1, 1, 1), "mod": (3, 3, 3), - "sub": (1, 2, 2), + "sub": (1, 2, 1), "div": (0, 0, 5), "pow": (0, 0, 5), } diff --git a/pyproject.toml b/pyproject.toml index dadf7d4..f293fdf 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api" [tool.poetry] name = "opvious" -version = "0.16.5rc1" +version = "0.16.5rc2" description = "Opvious Python SDK" authors = ["Opvious Engineering "] readme = "README.md" diff --git a/tests/test_modeling.py b/tests/test_modeling.py index 36f7213..3b39c41 100644 --- a/tests/test_modeling.py +++ b/tests/test_modeling.py @@ -559,3 +559,21 @@ def at_most_five_shifts_per_week(self): text = spec.sources[0].text assert r"\sum_{x \in \{ d \ldots d + 6 \}}" in text assert spec.annotation.issue_count == 0 + + @pytest.mark.asyncio + async def test_sub_precedence(self): + class _Model(om.Model): + actual = om.Variable.indicator() + + def opposite(self): + return 1 - self.actual() + + @om.objective + def minimize(self): + return self.actual() - self.opposite() + + model = _Model() + spec = await client.annotate_specification(model.specification()) + text = spec.sources[0].text + assert r"\alpha - \left(1 - \alpha\right)" in text + assert spec.annotation.issue_count == 0