Skip to content

Fix non-constant numbers in Boolean context - #583

Merged
dweindl merged 2 commits into
PEtab-dev:mainfrom
dweindl:fix-num2bool
Nov 16, 2026
Merged

dweindl merged 2 commits into
PEtab-dev:mainfrom
dweindl:fix-num2bool

Conversation

@dweindl

@dweindl dweindl commented Nov 12, 2026

Copy link
Copy Markdown
Member

Fix non-constant numeric expressions used as Booleans (operands of &&, ||, !, piecewise conditions) being treated as always true. Per the PEtab v2 spec, 0.0 is false and any other value is true.

num2bool now returns Ne(x, 0) instead of Piecewise((True, x != 0.0), (False, True)), where x != 0.0 was Python's structural inequality. It also converts bare symbols (previously passed through, since sp.Symbol is a sympy Boolean) and Boolean-valued piecewise operands.

For example, !(x + 1) was parsed to 0.0 and is now parsed to x + 1 == 0.

Fixes #581. The reverse direction (non-constant Booleans in numeric context) is #582 and not addressed here.

🤖 Generated with Claude Code

`num2bool` fell back to `Piecewise((True, x != 0.0), (False, True))`,
where `x != 0.0` is Python's structural inequality, so any non-constant
operand of `&&`, `||`, `!` or a `piecewise` condition was treated as
`true`. Return `Ne(x, 0)` instead.

Also convert symbols (which are sympy `Boolean`s as well) and
Boolean-valued `Piecewise` operands.

Fixes PEtab-dev#581.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@codecov-commenter

codecov-commenter commented Nov 12, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 79.50%. Comparing base (09c1998) to head (eb3ce15).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #583      +/-   ##
==========================================
+ Coverage   79.48%   79.50%   +0.01%     
==========================================
  Files          70       70              
  Lines        8278     8274       -4     
  Branches     1498     1496       -2     
==========================================
- Hits         6580     6578       -2     
+ Misses       1207     1206       -1     
+ Partials      491      490       -1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@dweindl
dweindl marked this pull request as ready for review November 12, 2026 13:32
@dweindl
dweindl requested a review from a team as a code owner November 12, 2026 13:32
@dweindl dweindl self-assigned this Nov 12, 2026
Comment thread petab/v1/math/SympyVisitor.py Outdated
Comment on lines 338 to 341
if x.is_zero is True:
return sp.false
if x.is_zero is False:
return sp.true

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be deleted? Since we now have return sp.Ne(x, 0)?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, it can be removed. Did it. Thanks.

`sp.Ne(x, 0)` already evaluates to `sp.true`/`sp.false` whenever
`x.is_zero` is decided.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@dweindl
dweindl enabled auto-merge (squash) November 16, 2026 13:21
@dweindl
dweindl merged commit ec0d6c1 into PEtab-dev:main Nov 16, 2026
7 checks passed
@dweindl
dweindl deleted the fix-num2bool branch November 16, 2026 19:31
dweindl added a commit to dweindl/libpetab-python that referenced this pull request Nov 17, 2026
Every pull request added its changelog entry at the same place in
`CHANGELOG.md`, so concurrent pull requests conflicted with each other.

Now, each pull request adds its entry as a separate file
`changelog.d/+<name>.<type>.md` (see `changelog.d/README.md`). At release
time, `towncrier build --version X.Y.Z` collects them into `CHANGELOG.md`.

The unreleased 0.11.0 entry is moved to `changelog.d/`, and entries are
added for PEtab-dev#583, PEtab-dev#584, PEtab-dev#588, PEtab-dev#590, PEtab-dev#592 and PEtab-dev#595, which did not add one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
dweindl added a commit to dweindl/libpetab-python that referenced this pull request Nov 17, 2026
Every pull request added its changelog entry at the same place in
`CHANGELOG.md`, so concurrent pull requests conflicted with each other.

Now, each pull request adds its entry as a separate file
`changelog.d/+<name>.<type>.md` (see `changelog.d/README.md`). At release
time, `towncrier build --version X.Y.Z` collects them into `CHANGELOG.md`.

The unreleased 0.11.0 entry is moved to `changelog.d/`, and entries are
added for PEtab-dev#583, PEtab-dev#584, PEtab-dev#588, PEtab-dev#590, PEtab-dev#592 and PEtab-dev#595, which did not add one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
dweindl added a commit to dweindl/libpetab-python that referenced this pull request Nov 17, 2026
Every pull request added its changelog entry at the same place in
`CHANGELOG.md`, so concurrent pull requests conflicted with each other.

Now, each pull request adds its entry as a separate file
`changelog.d/+<name>.<type>.md` (see `changelog.d/README.md`). At release
time, `towncrier build --version X.Y.Z` collects them into `CHANGELOG.md`.

The unreleased 0.11.0 entry is moved to `changelog.d/`, and entries are
added for PEtab-dev#583, PEtab-dev#584, PEtab-dev#588, PEtab-dev#590, PEtab-dev#592 and PEtab-dev#595, which did not add one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
dweindl added a commit to dweindl/libpetab-python that referenced this pull request Nov 17, 2026
…v#600)

Every pull request added its changelog entry at the same place in
`CHANGELOG.md`, so concurrent pull requests conflicted with each other.

Now, each pull request adds its changelog entry as a separate file
`changelog.d/+<name>.<type>.md` (see `changelog.d/README.md`). At
release time, `towncrier build --version X.Y.Z` collects them into
`CHANGELOG.md` and removes the fragments. The template in
`changelog.d/template.md.j2` reproduces the existing format (`**Fixes**`
etc., `* ` bullets with 2-space continuation lines). Entries are sorted
alphabetically within each category; grouping them (e.g., by PEtab
version or module) is done when compiling the release notes. The
configuration is in `pyproject.toml`, and towncrier is added to the
`test` dependency group. The release process in `doc/development.rst` is
updated accordingly.

Entries now link to the pull request only, without "by @user";
contributors can be credited in the release notes.

The unreleased 0.11.0 entry (PEtab-dev#578) is moved to `changelog.d/`, and
entries are added for PEtab-dev#583, PEtab-dev#584, PEtab-dev#588, PEtab-dev#590, PEtab-dev#592 and PEtab-dev#595, which did
not add one. The behaviour changes from PEtab-dev#584 (top-level Boolean
expressions are returned as `Piecewise`) and PEtab-dev#592 (stricter
`PySBModel.pattern_from_string`) are listed as breaking changes.

Open pull requests that edit `CHANGELOG.md` will need to move their
entry to a fragment file.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Non-constant numbers in Boolean context are parsed to constants

3 participants