Skip to content

Move same magic methods from Expr and GenExpr to ExprLike#1204

Open
Zeroto521 wants to merge 23 commits into
scipopt:masterfrom
Zeroto521:ExprLike
Open

Move same magic methods from Expr and GenExpr to ExprLike#1204
Zeroto521 wants to merge 23 commits into
scipopt:masterfrom
Zeroto521:ExprLike

Conversation

@Zeroto521
Copy link
Copy Markdown
Contributor

@Zeroto521 Zeroto521 commented Apr 6, 2026

Move magic methods (__radd__, __sub__, __rsub__, __rmul__, __richcmp__, __neg__, and __rtruediv__) to ExprLike base class. To simplify the code.

Move common operator dunder methods (__neg__, __radd__, __sub__, __rsub__, __rmul__, __richcmp__) into the base ExprLike class and remove their duplicate implementations from Expr and GenExpr. This consolidates operator behavior across expression types, forwarding rich comparisons to _expr_richcmp and reducing code duplication for negation, arithmetic and reflected operations.
Move and centralize operator overloads for ExprLike: __radd__, __sub__, __rsub__, __rmul__, __neg__ and __richcmp__ are added earlier in the class and the duplicate implementations later in the file were removed. This refactor cleans up the class definition and avoids duplicated method definitions.
Consolidate reflected true-division handling by adding __rtruediv__ to the ExprLike base class and removing duplicate implementations from Expr and GenExpr. The reflected division now uniformly uses buildGenExprObj(other) / self, reducing code duplication and ensuring consistent behavior across expression types.
Document a refactor that moves several dunder methods (__radd__, __sub__, __rsub__, __rmul__, __richcmp__, __neg__, __rtruediv__) into the ExprLike base class to centralize operator behavior. This change only updates CHANGELOG.md to record the API/internal restructuring.
Copilot AI review requested due to automatic review settings April 6, 2026 12:06
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Refactors expression operator overloading by centralizing shared magic methods in the ExprLike base class to reduce duplication between Expr and GenExpr.

Changes:

  • Add shared operator magic methods (__radd__, __sub__, __rsub__, __rmul__, __rtruediv__, __richcmp__, __neg__) to ExprLike.
  • Remove the duplicated implementations of those magic methods from Expr and GenExpr.
  • Document the refactor in the changelog.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
src/pyscipopt/expr.pxi Moves duplicated operator overload implementations into ExprLike and deletes them from Expr/GenExpr.
CHANGELOG.md Notes the operator-method move in the “Changed” section.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/pyscipopt/expr.pxi Outdated
Comment thread CHANGELOG.md Outdated
Zeroto521 and others added 6 commits April 7, 2026 20:49
Add the positional-only marker ('/') to several operator method signatures in ExprLike to prevent passing the operand as a keyword and to align with CPython semantics. Affected methods: __radd__, __sub__, __rsub__, __rmul__, and __rtruediv__. This is a signature-level change only and should not alter runtime behavior.
Update src/pyscipopt/scip.pyi: add missing arithmetic/operator dunder declarations (__radd__, __sub__, __rsub__, __rmul__, __rtruediv__, __neg__) to the ExprLike base stub and remove redundant/operator declarations from Expr and GenExpr. This consolidates common operator signatures in the base protocol, reduces duplication, and improves typing/stub consistency.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Delete an extraneous blank line between the end of ExprLike and the @disjoint_base decorator/Expr class in src/pyscipopt/scip.pyi to tidy file formatting. No functional changes.
@Joao-Dionisio
Copy link
Copy Markdown
Member

Can you please fix the conflicts @Zeroto521 ? I think it's missing the NotImplemented guards of #1182.

Zeroto521 added 3 commits May 20, 2026 22:20
Replace Incomplete with object for several ExprLike operator stubs (__radd__, __sub__, __rsub__, __rmul__, __rtruediv__, __neg__) to relax/standardize return and operand typing. Also add missing __rtruediv__ stubs to Expr and GenExpr. These changes improve the type stubs for static type checkers and better reflect Python operator semantics.
@Zeroto521
Copy link
Copy Markdown
Contributor Author

Can you please fix the conflicts @Zeroto521 ? I think it's missing the NotImplemented guards of #1182.

Done

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

Comment thread src/pyscipopt/scip.pyi Outdated
Comment thread src/pyscipopt/scip.pyi
@Joao-Dionisio
Copy link
Copy Markdown
Member

Joao-Dionisio commented May 20, 2026

Here is what my Claude flagged:

1. Likely perf regression on Expr.__neg__. The previous Expr.__neg__ was a single dict comprehension (Expr({v:-c for v,c in self.terms.items()})). The inherited ExprLike.__neg__ is self * -1.0, which routes through __mul__. Since __sub__ calls -other, negation is on the hot path and this partially undoes the speedups from #1205. Either keep Expr.__neg__ overridden with the fast path, or post a benchmark showing the impact is negligible.

2. Stub typings inconsistent. In scip.pyi, the new ExprLike magic methods are typed (other: object, /) -> object, while the rest of the file uses Incomplete for arithmetic operators (__add__, __mul__, etc.). Result: expr + 1 becomes object to type checkers, defeating downstream inference. Switch to Incomplete for consistency.

3. Duplicate __rtruediv__ in stubs. After this PR, Expr and GenExpr both declare __rtruediv__ in scip.pyi and inherit the identical signature from ExprLike. The subclass declarations can be dropped.

Zeroto521 added 5 commits May 23, 2026 12:01
Add the positional-only marker (/) to the ExprLike.__neg__ signature in src/pyscipopt/expr.pxi. This enforces that the unary negation method cannot be called with keyword arguments and aligns the signature with Python/Cython expectations, avoiding potential warnings or misuse.
Update type stubs in src/pyscipopt/scip.pyi to change return types of several ExprLike dunder operators from object to Incomplete for improved typing precision. Affected methods: __radd__, __sub__, __rsub__, __rmul__, __rtruediv__, and __neg__. This is a type-only change with no runtime behavior modifications.
Add explicit GenExpr return annotations for __rtruediv__ across implementation and typing stub. Updated src/pyscipopt/expr.pxi to annotate ExprLike, Expr and GenExpr __rtruediv__ methods with -> GenExpr, and updated src/pyscipopt/scip.pyi to change the stub return types from object to GenExpr for Expr.__rtruediv__ and GenExpr.__rtruediv__. This improves static typing consistency between the Cython implementation and the Python stubs.
Update negation operator type hints to return Union[Expr, GenExpr].

- src/pyscipopt/expr.pxi: add a Cython return annotation for __neg__.
- src/pyscipopt/scip.pyi: change __neg__ from Incomplete to Union[Expr, GenExpr].

These changes improve static typing and IDE support by accurately describing the result of unary negation on expression-like objects.
Update typing in src/pyscipopt/scip.pyi: change ExprLike.__rtruediv__ return type from Incomplete to GenExpr and remove duplicate __rtruediv__ declarations from Expr and GenExpr. This consolidates the right-division signature in the base ExprLike class to avoid conflicting or redundant stub definitions and improve type consistency.
Comment thread src/pyscipopt/expr.pxi
Comment on lines -699 to -700
def __neg__(self):
return -1.0 * self
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This should be a bit faster now.

  • master PR calls __neg__ on GenExpr: GenExpr.__neg__()(-1.0).__mul__(GenExpr)GenExpr.__rmul__(-1.0)
  • this PR calls __neg__ on GenExpr via C-API: GenExpr.__neg__()GenExpr.__rmul__(-1.0)

Comment thread src/pyscipopt/expr.pxi
return (self * Constant(base).log()).exp()

def __neg__(self):
return Expr({v:-c for v,c in self.terms.items()})
Copy link
Copy Markdown
Contributor Author

@Zeroto521 Zeroto521 May 23, 2026

Choose a reason for hiding this comment

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

This benefits from #1185:

  • master PR calls __neg__ for Expr via Python list comprehension: 4.7777s on the following example
  • this PR calls __neg__ for Expr via C-API: 3.8283s on the same example
from timeit import timeit

from pyscipopt import Model


m = Model()

n = 1000
x = m.addMatrixVar(n)
e = x.sum()

number = 100_000
cost = timeit(lambda: -e, number=number)
print(f"Cost of negating an expression over {number} runs: {cost:.4f} seconds")

Zeroto521 added 2 commits May 23, 2026 12:32
Update the typing stub for ExprLike.__neg__ in src/pyscipopt/scip.pyi to include the positional-only marker (/). This clarifies that __neg__ accepts no keyword arguments and returns Union[Expr, GenExpr]; no runtime behavior changes.
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.

3 participants