Skip to content

fix(math/object_transform): cond/ifelse, collectors, VariableView edge cases#840

Merged
chaoming0625 merged 1 commit into
masterfrom
fix/audit-20260619-math-object-transform
Jun 18, 2026
Merged

fix(math/object_transform): cond/ifelse, collectors, VariableView edge cases#840
chaoming0625 merged 1 commit into
masterfrom
fix/audit-20260619-math-object-transform

Conversation

@chaoming0625

@chaoming0625 chaoming0625 commented Jun 18, 2026

Copy link
Copy Markdown
Member

Fresh review of brainpy/math/object_transform. Four Medium correctness/robustness fixes:

  • bm.cond now accepts constant (non-callable) branches as documented.
  • bm.ifelse now accepts a scalar bool/array condition as documented.
  • Collector.__sub__ raises the descriptive ValueError (not a bare KeyError).
  • VariableView.value setter mirrors the hardened Variable setter (unwraps State, canonicalizes numpy dtype, handles plain inputs).

Regression tests added (231 passed in-scope). Findings: docs/issues-found-20260619-math-object-transform.md.

Summary by Sourcery

Improve robustness and documented-behavior compliance of math object-transform utilities and add regression coverage for discovered edge cases.

Bug Fixes:

  • Allow bm.cond to accept constant (non-callable) branches in line with its documented API.
  • Allow bm.ifelse to accept a scalar boolean or boolean array as the conditions argument without crashing.
  • Ensure Collector.sub raises a descriptive ValueError when attempting to remove a value object that is not present, instead of a bare KeyError.
  • Align VariableView.value setter behavior with Variable.value by unwrapping state-like inputs, canonicalizing numpy dtypes, and using safe shape/dtype checks to avoid AttributeError.

Documentation:

  • Add an audit report documenting the 2026-06-19 review of brainpy/math/object_transform, including fixed and recorded issues.

Tests:

  • Add regression tests for bm.cond handling of constant versus callable branches.
  • Add regression tests for bm.ifelse with scalar Python bool and scalar array bool conditions.
  • Add regression tests covering VariableView.value setter behavior with Python lists, numpy dtype canonicalization, and State unwrapping.
  • Add regression test verifying Collector.sub error behavior when subtracting a missing value object.

…ollectors, VariableView

- bm.cond crashed on constant (non-callable) branches despite docstring
  allowing ArrayType/float/int/bool; wrap constants into callables (Medium)
- bm.ifelse crashed (len() on bool) when `conditions` was a scalar bool/array;
  normalize scalar to a 1-element list (Medium)
- Collector.__sub__ raised a bare KeyError instead of the descriptive
  ValueError used by every other not-found path (Medium)
- VariableView.value setter crashed on plain inputs and never unwrapped
  State / canonicalized numpy dtype; mirror the hardened Variable setter (Medium)

Findings recorded in docs/issues-found-20260619-math-object-transform.md
@chaoming0625 chaoming0625 merged commit 564aed2 into master Jun 18, 2026
1 of 4 checks passed
@sourcery-ai

sourcery-ai Bot commented Jun 18, 2026

Copy link
Copy Markdown

Reviewer's Guide

Refines math/object_transform edge-case behavior and error handling for cond/ifelse, Collector.sub, and VariableView.value, and adds regression tests plus an issues-found audit doc.

Sequence diagram for bm.cond with constant branches

sequenceDiagram
    actor User
    participant bm_cond as bm_cond
    participant _make_branch as _make_branch
    participant brainstate_cond as brainstate_transform_cond

    User->>bm_cond: cond(pred, true_fun, false_fun, operands)
    bm_cond->>bm_cond: operands = _unwrap_state_operands(operands)
    bm_cond->>_make_branch: _make_branch(true_fun)
    alt branch_is_callable
        _make_branch-->>bm_cond: wrapped_true_fun
    else branch_is_constant
        _make_branch-->>bm_cond: wrapped_constant_true_fun
    end
    bm_cond->>_make_branch: _make_branch(false_fun)
    alt branch_is_callable
        _make_branch-->>bm_cond: wrapped_false_fun
    else branch_is_constant
        _make_branch-->>bm_cond: wrapped_constant_false_fun
    end
    bm_cond->>brainstate_cond: cond(pred, wrapped_true_fun, wrapped_false_fun, operands)
    brainstate_cond-->>User: result
Loading

File-Level Changes

Change Details Files
Normalize and extend VariableView.value setter to match Variable.value robustness and dtype handling.
  • Unwrap brainstate.State and Array inputs before validation.
  • Convert numpy.ndarray inputs to jnp.asarray prior to shape/dtype checks.
  • Use jnp.shape and _get_dtype for shape/dtype comparison instead of accessing .shape/.dtype on raw inputs.
  • Align error messages and dtype canonicalization with Variable.value and assign unwrapped value directly into backing array slice.
brainpy/math/object_transform/variables.py
brainpy/math/object_transform/object_transform_fixes_test.py
Make bm.cond accept constant branches and bm.ifelse accept scalar-bool conditions per their documented contracts.
  • Wrap non-callable cond branches into callables that return the unwrapped constant, reusing warp_to_no_state_input_output.
  • Normalize scalar conditions in ifelse into a single-element list before mutually exclusive condition conversion.
  • Add regression tests for cond constant branches and callable branches mutating state.
  • Add regression tests for ifelse with scalar Python bool and scalar array bool conditions.
brainpy/math/object_transform/controls.py
brainpy/math/object_transform/controls_test.py
Harden Collector.sub missing-value handling to raise consistent ValueError instead of raw KeyError.
  • Guard id_to_keys lookup with .get when subtracting by value object and raise descriptive ValueError when not found.
  • Add regression test ensuring subtracting a missing value object raises ValueError, matching missing-key behavior.
brainpy/math/object_transform/collectors.py
brainpy/math/object_transform/collectors_test.py
Document the audit and remaining low-priority findings for math/object_transform.
  • Add markdown audit report summarizing prior fixes, new medium-priority fixes, and recorded low-priority issues.
  • Cross-check against previous issues-found document and record verification status of earlier items.
docs/issues-found-20260619-math-object-transform.md

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@github-actions github-actions Bot added documentation Improvements or additions to documentation tests labels Jun 18, 2026

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Hey - I've found 2 issues

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="brainpy/math/object_transform/controls.py" line_range="250-251" />
<code_context>
+    # it into a one-element list so it flows through the conversion below;
+    # otherwise ``brainstate.transform.ifelse`` would call ``len()`` on the
+    # scalar and raise ``TypeError: object ... has no len()``.
+    if not isinstance(conditions, (list, tuple)):
+        conditions = [conditions]
+
     # Convert if-elif-else chain to mutually exclusive conditions
</code_context>
<issue_to_address>
**suggestion:** Normalising non-sequence `conditions` to a one-element list changes behaviour for other iterable types (e.g. generators); consider a more precise type check.

The current `if not isinstance(conditions, (list, tuple))` branch fixes the scalar case but also wraps generators and other non-list/tuple sequences, changing them from multiple conditions into a single one.

To keep behaviour consistent for genuine sequences, consider checking for “is a scalar/non-sequence” rather than “is not list/tuple”. For example, you could detect `collections.abc.Sequence` (excluding `str`/`bytes`/`np.ndarray`) or explicitly handle `np.bool_` and scalar arrays, so real sequences still flow through unchanged while scalars avoid the `len()` error.

Suggested implementation:

```python
    branches = [make_callable(branch) for branch in branches]

    # A single condition may be passed as a bare scalar bool/array (the
    # docstring types ``conditions`` as ``bool, sequence of bool``). Normalise
    # true scalars into a one-element list so they flow through the conversion
    # below; otherwise ``brainstate.transform.ifelse`` would call ``len()`` on
    # the scalar and raise ``TypeError: object ... has no len()``. Existing
    # iterable conditions (lists/tuples/generators/etc.) are left unchanged so
    # they still represent multiple conditions.
    is_numpy_scalar_bool = False
    try:
        import numpy as np  # use existing import if already present
        if isinstance(conditions, np.bool_) or (
            isinstance(conditions, np.ndarray) and conditions.ndim == 0 and conditions.dtype == bool
        ):
            is_numpy_scalar_bool = True
    except Exception:
        # NumPy not available or not used; fall back to pure-Python scalars only
        pass

    if isinstance(conditions, bool) or is_numpy_scalar_bool:
        conditions = [conditions]

    # Convert if-elif-else chain to mutually exclusive conditions
    if isinstance(conditions, (list, tuple)) and len(conditions) > 0:
        conditions = list(conditions)

```

1. If this file does not already import NumPy as `np`, you may want to move the `import numpy as np` to the module top-level instead of keeping the local import inside the function, to match existing style/import conventions.
2. If other scalar condition types (e.g. `np.int_` or numeric scalars) should also be normalised, extend the `is_numpy_scalar_bool` logic and/or the `isinstance(conditions, bool)` check accordingly (e.g. using `numbers.Integral`), but the current change focuses on boolean scalars to match the documented type.
</issue_to_address>

### Comment 2
<location path="brainpy/math/object_transform/collectors_test.py" line_range="154-160" />
<code_context>
         c - ['nope']


+def test_sub_with_list_missing_value_raises():
+    # P4-M3: removing a *value* object that is not present must raise the same
+    # descriptive ValueError as the string-key path, not a bare KeyError(id).
+    present = object()
+    absent = object()
+    c = Collector({'a': present})
+    with pytest.raises(ValueError):
+        c - [absent]
+
</code_context>
<issue_to_address>
**suggestion (testing):** Strengthen the regression by checking the `ValueError` message content for missing values.

Since this already covers the missing-value path for `Collector.__sub__`, you can make the regression stronger by also asserting on the error message to distinguish it from a generic `ValueError`:

```python
with pytest.raises(ValueError, match=r"Cannot remove .* do not find it in"):
    c - [absent]
```

This also keeps the behavior aligned with the string-key path.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment on lines +250 to +251
if not isinstance(conditions, (list, tuple)):
conditions = [conditions]

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

suggestion: Normalising non-sequence conditions to a one-element list changes behaviour for other iterable types (e.g. generators); consider a more precise type check.

The current if not isinstance(conditions, (list, tuple)) branch fixes the scalar case but also wraps generators and other non-list/tuple sequences, changing them from multiple conditions into a single one.

To keep behaviour consistent for genuine sequences, consider checking for “is a scalar/non-sequence” rather than “is not list/tuple”. For example, you could detect collections.abc.Sequence (excluding str/bytes/np.ndarray) or explicitly handle np.bool_ and scalar arrays, so real sequences still flow through unchanged while scalars avoid the len() error.

Suggested implementation:

    branches = [make_callable(branch) for branch in branches]

    # A single condition may be passed as a bare scalar bool/array (the
    # docstring types ``conditions`` as ``bool, sequence of bool``). Normalise
    # true scalars into a one-element list so they flow through the conversion
    # below; otherwise ``brainstate.transform.ifelse`` would call ``len()`` on
    # the scalar and raise ``TypeError: object ... has no len()``. Existing
    # iterable conditions (lists/tuples/generators/etc.) are left unchanged so
    # they still represent multiple conditions.
    is_numpy_scalar_bool = False
    try:
        import numpy as np  # use existing import if already present
        if isinstance(conditions, np.bool_) or (
            isinstance(conditions, np.ndarray) and conditions.ndim == 0 and conditions.dtype == bool
        ):
            is_numpy_scalar_bool = True
    except Exception:
        # NumPy not available or not used; fall back to pure-Python scalars only
        pass

    if isinstance(conditions, bool) or is_numpy_scalar_bool:
        conditions = [conditions]

    # Convert if-elif-else chain to mutually exclusive conditions
    if isinstance(conditions, (list, tuple)) and len(conditions) > 0:
        conditions = list(conditions)
  1. If this file does not already import NumPy as np, you may want to move the import numpy as np to the module top-level instead of keeping the local import inside the function, to match existing style/import conventions.
  2. If other scalar condition types (e.g. np.int_ or numeric scalars) should also be normalised, extend the is_numpy_scalar_bool logic and/or the isinstance(conditions, bool) check accordingly (e.g. using numbers.Integral), but the current change focuses on boolean scalars to match the documented type.

Comment on lines +154 to +160
def test_sub_with_list_missing_value_raises():
# P4-M3: removing a *value* object that is not present must raise the same
# descriptive ValueError as the string-key path, not a bare KeyError(id).
present = object()
absent = object()
c = Collector({'a': present})
with pytest.raises(ValueError):

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

suggestion (testing): Strengthen the regression by checking the ValueError message content for missing values.

Since this already covers the missing-value path for Collector.__sub__, you can make the regression stronger by also asserting on the error message to distinguish it from a generic ValueError:

with pytest.raises(ValueError, match=r"Cannot remove .* do not find it in"):
    c - [absent]

This also keeps the behavior aligned with the string-key path.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant