Skip to content

fix(#61): SymmetryLossWarning points to user call site#97

Merged
spMohanty merged 3 commits into
mainfrom
fix/issue-61-warning-stacklevel
May 22, 2026
Merged

fix(#61): SymmetryLossWarning points to user call site#97
spMohanty merged 3 commits into
mainfrom
fix/issue-61-warning-stacklevel

Conversation

@spMohanty
Copy link
Copy Markdown
Collaborator

Summary

  • Closes #61. SymmetryLossWarning previously pointed at src/flopscope/_budget.py:174 (inside _counted_wrapper.wrapped) instead of the user's +/sum()/[...] line, making symmetry-loss diagnostics hard to act on.
  • Replaces the fixed stacklevel=3 in _warn_symmetry_loss with a small _user_stacklevel() helper that walks the call stack to the first frame outside the flopscope/ package.
  • Robust to future wrapper-layer changes — no manual stacklevel bookkeeping at the eight existing call sites.

Root cause

The eight call sites of _warn_symmetry_loss traverse different numbers of wrapper frames before reaching warnings.warn:

Call site Frames to user code
Binary ops (_pointwise.py:475, 482) 5
Binary multi (_pointwise.py:561, 568) 5
Reductions (_pointwise.py:951, 954) 5
Slicing (_symmetric.py:670, 677) 3

No single constant works for all paths, and per-site stacklevels would be fragile to refactoring (adding/removing a decorator silently shifts the wrong number of frames).

Test plan

  • Issue reproducer now reports the user line (was src/flopscope/_budget.py:174).
  • New tests/test_issue_61_symmetry_warning_location.py covers binary add, reduction, and slicing — asserts w.filename == __file__ and explicitly rejects paths under flopscope/.
  • Symmetry suite (tests/test_issue_66_newaxis_symmetry.py tests/test_symmetric*.py): 418 reports, 0 failed.
  • Full local suite: 11,341 reports, 0 failed.
  • Pre-push CI (lint + typecheck + tests + numpy-compat + docs-build): all checks passed.

spMohanty added 3 commits May 23, 2026 01:25
Replace the fixed `stacklevel=3` in `_warn_symmetry_loss` with a small
frame-walking helper that finds the first frame outside `flopscope/`.
The previous constant landed inside `_counted_wrapper.wrapped` in
`_budget.py` instead of the user's `+`/`sum()`/`[...]` line, making
symmetry-loss diagnostics hard to act on.

The helper is robust to future changes in the number of decorator
layers between user code and the warn site — no manual stacklevel
bookkeeping at call sites.
Three cases covering the call paths that currently traverse different
numbers of wrapper frames:
- binary `A + B` with mismatched symmetries (5 frames deep)
- reduction `A.sum(axis=...)` over a symmetric axis (5 frames)
- slicing a `SymmetricTensor` that breaks the group (3 frames)

Each test asserts `w.filename == __file__` and explicitly rejects any
filename under the `flopscope` package directory, so a future wrapper
addition that breaks the helper fails loudly here.
@spMohanty spMohanty merged commit b724174 into main May 22, 2026
17 checks passed
@spMohanty spMohanty deleted the fix/issue-61-warning-stacklevel branch May 22, 2026 23:34
spMohanty added a commit that referenced this pull request May 23, 2026
spMohanty added a commit that referenced this pull request May 23, 2026
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.

SymmetryLossWarning stack trace should point to user call location

1 participant