Skip to content

Non-constant numbers in Boolean context are parsed to constants #581

Open

Description

@dweindl

sympify_petab gets the result wrong when a non-constant numeric expression is used as a Boolean, i.e. as an operand of &&, ||, !, or as a piecewise condition. The PEtab v2 spec ("Boolean ↔ float conversion") says 0.0 is converted to false and any other value to true. Instead, the operand is always treated as true. This happens with both evaluate=True and evaluate=False:

from petab.v1.math import sympify_petab

sympify_petab("(x + 1) && (y > 2)")      # y > 2.0         (expected: (x + 1 != 0) && (y > 2))
sympify_petab("!(x + 1)")                # 0.0             (expected: x + 1 == 0)
sympify_petab("piecewise(1, x + 1, 0)")  # 1.0             (expected: 1 if x + 1 != 0 else 0)
sympify_petab("piecewise(1, x > 0, 0) && true")  # 1.0     (expected: x > 0)

The cause is the fallback in num2bool (petab/v1/math/SympyVisitor.py), sp.Piecewise((True, x != 0.0), (False, True)). There, x != 0.0 is Python's structural inequality, not sp.Ne, so it evaluates to True for any symbolic x.

In the same function, a bare symbol is passed through unchanged, because sp.Symbol is a subclass of sp.logic.boolalg.Boolean. As a result, x && true is parsed to x instead of x != 0, and x && y is parsed to And(x, y), which fails once numbers are substituted. Boolean-valued piecewise operands such as !piecewise(true, x > 0, false) are not handled correctly either.

petab.v2.math re-exports petab.v1.math, so both versions are affected.

The same bug in sbmlmath: dweindl/sbmlmath#52

🤖 Generated with AI assistance (Claude Code)

Activity

  1. self-assigned this
    on Nov 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions