Skip to content

feat: implement __sub__ and __rsub__ for Loss class - #152

Open
hemv-857 wants to merge 3 commits into
jankrepl:masterfrom
hemv-857:feat/add-sub-operator
Open

hemv-857 wants to merge 3 commits into
jankrepl:masterfrom
hemv-857:feat/add-sub-operator

Conversation

@hemv-857

Copy link
Copy Markdown

What

Implements __sub__ and __rsub__ on the Loss class so users can write:

combined = SomeLoss() - OtherLoss()
constant_sub = 5 - SomeLoss()

Previously the only way to achieve this was SomeLoss() + (-1) * OtherLoss().

Closes #68

Changes

  • Added __sub__ method to Loss class (follows __add__ pattern exactly)
  • Added __rsub__ method for constant - loss syntax
  • Added sub to parametrized arithmetic tests
  • Added sub to repr arithmetic tests
  • Added type error tests for subtraction with invalid types

Testing

All existing tests pass. New tests cover:

  • loss_a - loss_b (Loss - Loss)
  • loss - constant (Loss - int/float)
  • constant - loss (int/float - Loss via __rsub__)
  • TypeError on invalid operand types
  • Repr round-trip for subtraction

Enables subtraction syntax: SomeLoss() - OtherLoss() and
constant - SomeLoss().

Previously users had to hack around this with (-1) * SomeLoss().

Closes jankrepl#68
Copilot AI lite review requested due to automatic review settings September 21, 2026 19:15

Copilot AI 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.

Copilot review overview

🟡 Changes recommended

Unresolved moderate issues affect unary negation and nested-subtraction repr correctness; reverse-subtraction coverage and documentation also need updates.

Get a fresh assessment by requesting another Copilot review.

Review effort: Lite
Findings: 2 Medium severity · 1 Low severity

Open (3)
What changed in this PR

Adds subtraction support to Loss, including reflected constant subtraction, with expanded arithmetic and representation tests.

Changes:

  • Implements Loss.__sub__ and Loss.__rsub__.
  • Adds subtraction arithmetic, repr, and invalid-type test coverage.
File Review summary
tests/​test_losses.py Adds subtraction coverage, but lacks valid reverse-subtraction evaluation and representation tests.
deepdow/​losses.py Adds subtraction operators, but unary negation is unsupported, nested subtraction repr loses grouping, and the docstring does not match supported operand types.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread deepdow/losses.py
Comment thread deepdow/losses.py Outdated
Comment thread tests/test_losses.py
…b tests

- Add __neg__ for unary -SomeLoss() (the issue jankrepl#68 requirement)
- Parenthesize right operand in __sub__ repr: a - (b) instead of a - b
  so eval(repr(...)) round-trip preserves semantics for nested subtraction
- Parenthesize both operands in __rsub__ repr for consistency
- Add test_neg for unary negation
- Add test_rsub for constant - loss path with repr round-trip
- Add test_sub_repr_roundtrip for nested subtraction grouping
@hemv-857

Copy link
Copy Markdown
Author

Thanks for the review! All findings addressed:

  • Unary negation: Added __neg__ so -SomeLoss() now works. repr uses -() wrapping.
  • Repr grouping: Right operand in __sub__ is now parenthesized: a - (b) instead of a - b. This preserves eval(repr(...)) round-trip for nested subtraction like a - (b - c).
  • rsub test coverage: Added test_rsub that exercises constant - loss with evaluation and repr round-trip checks.
  • Also added test_sub_repr_roundtrip specifically for nested subtraction grouping.

Copilot AI 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.

Copilot review overview

🟡 Changes recommended

A test has undefined class names, and reflected subtraction mishandles Loss subclasses.

Get a fresh assessment by requesting another Copilot review.

Review effort: Lite
Findings: 1 High severity

Open (1)
Resolved since last review (3)
Previously missed (1)

In code that hasn't changed since last review

Medium severity Handle reflected subtraction for Loss subclasses

deepdow/​losses.py:369

__rsub__ rejects Loss operands, but Python can dispatch the reflected method for left_loss - right_loss when right_loss is a subclass of left_loss. In that case a valid Loss–Loss subtraction reaches this branch and raises TypeError; handle Loss by delegating to other.__sub__(self) before the numeric case (and add a regression test with a derived loss class).

Comment thread tests/test_losses.py Outdated
Comment on lines +453 to +454
b = Variance()
c = MaxDrawdown()
…asses in __rsub__

- Fix test_sub_repr_roundtrip: use correct class names (StandardDeviation,
  MaximumDrawdown) instead of undefined (Variance, MaxDrawdown)
- Handle Loss subclasses in __rsub__: when a Loss subclass is on the right
  side of subtraction, Python dispatches to __rsub__ on the subclass. Now
  handles Loss operands by delegating to other.__sub__(self) equivalent.
- Add test_rsub_with_loss_subclass regression test
@hemv-857

Copy link
Copy Markdown
Author

Thanks for the review! Both findings addressed:

  • Undefined class names: Fixed Variance → StandardDeviation and MaxDrawdown → MaximumDrawdown in test_sub_repr_roundtrip. Test now uses the actual imported class names.
  • Loss subclass dispatch in rsub: Added isinstance(other, Loss) check to __rsub__. When base_loss - subclass_loss and the subclass doesn't define __sub__, Python dispatches to subclass.__rsub__(base_loss). Now handles this case correctly. Added test_rsub_with_loss_subclass regression test.

@hemv-857
hemv-857 requested a balanced review from Copilot September 21, 2026 19:43

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@hemv-857

Copy link
Copy Markdown
Author

Hi! Just checking in on this PR — happy to make any changes if needed. Thanks for your time!

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.

Implement __sub__

2 participants