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 15 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 |
| def __radd__(self, other: object, /) -> object: ... | ||
| def __sub__(self, other: object, /) -> object: ... | ||
| def __rsub__(self, other: object, /) -> object: ... | ||
| def __rmul__(self, other: object, /) -> object: ... | ||
| def __rtruediv__(self, other: object, /) -> object: ... | ||
| def __neg__(self) -> object: ... |
| def __ne__(self, other: object, /) -> bool: ... | ||
| def __neg__(self) -> Incomplete: ... | ||
| def __pow__(self, other: Incomplete, modulo: Incomplete = ..., /) -> Incomplete: ... | ||
| def __radd__(self, other: Incomplete, /) -> Incomplete: ... | ||
| def __rmul__(self, other: Incomplete, /) -> Incomplete: ... | ||
| def __rpow__(self, other: Incomplete, /) -> Incomplete: ... | ||
| def __rsub__(self, other: Incomplete, /) -> Incomplete: ... | ||
| def __rtruediv__(self, other: Incomplete, /) -> Incomplete: ... | ||
| def __sub__(self, other: Incomplete, /) -> Incomplete: ... | ||
| def __truediv__(self, other: Incomplete, /) -> Incomplete: ... | ||
| def __rtruediv__(self, other: object, /) -> object: ... | ||
|
|
|
Here is what my Claude flagged: 1. Likely perf regression on 2. Stub typings inconsistent. In 3. Duplicate |
Move magic methods (
__radd__,__sub__,__rsub__,__rmul__,__richcmp__,__neg__, and__rtruediv__) toExprLikebase class. To simplify the code.