Skip to content

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

Open
Zeroto521 wants to merge 10 commits intoscipopt:masterfrom
Zeroto521:ExprLike
Open

Move same magic methods from Expr and GenExpr to ExprLike#1204
Zeroto521 wants to merge 10 commits intoscipopt: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.

Zeroto521 and others added 5 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.
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.

2 participants