Move same magic methods from Expr and GenExpr to ExprLike#1204
Move same magic methods from Expr and GenExpr to ExprLike#1204Zeroto521 wants to merge 23 commits into
Expr and GenExpr to ExprLike#1204Conversation
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.
There was a problem hiding this comment.
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__) toExprLike. - Remove the duplicated implementations of those magic methods from
ExprandGenExpr. - 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.
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.
|
Can you please fix the conflicts @Zeroto521 ? I think it's missing the |
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.
Done |
|
Here is what my Claude flagged: 1. Likely perf regression on 2. Stub typings inconsistent. In 3. Duplicate |
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.
| def __neg__(self): | ||
| return -1.0 * self |
There was a problem hiding this comment.
This should be a bit faster now.
- master PR calls
__neg__onGenExpr:GenExpr.__neg__()→(-1.0).__mul__(GenExpr)→GenExpr.__rmul__(-1.0) - this PR calls
__neg__onGenExprvia C-API:GenExpr.__neg__()→GenExpr.__rmul__(-1.0)
| return (self * Constant(base).log()).exp() | ||
|
|
||
| def __neg__(self): | ||
| return Expr({v:-c for v,c in self.terms.items()}) |
There was a problem hiding this comment.
This benefits from #1185:
- master PR calls
__neg__forExprvia Python list comprehension: 4.7777s on the following example - this PR calls
__neg__forExprvia 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")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.
Move magic methods (
__radd__,__sub__,__rsub__,__rmul__,__richcmp__,__neg__, and__rtruediv__) toExprLikebase class. To simplify the code.