Skip to content

DEP: remove getOp and Operator - #1259

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

Zeroto521 wants to merge 23 commits into
scipopt:masterfrom
Zeroto521:RemoveOP

Conversation

@Zeroto521

Copy link
Copy Markdown
Contributor

Since we have all UnaryExpr subclasses (abs, log, exp, sin, cos), we can drop the GenExpr._op attribute and use type instead.

Zeroto521 added 12 commits June 8, 2026 21:50
…r unary expressions

Remove Python math module and use libc.math for C-level functions (fabs, exp, log, sqrt, sin, cos). Refactor unary expression evaluation by introducing specific subclasses (AbsExpr, ExpExpr, LogExpr, SqrtExpr, SinExpr, CosExpr) with dedicated evaluate methods using C functions, replacing the generic UnaryExpr implementation.
Update UnaryExpr type stubs to include concrete expression subclasses for
more precise type annotations. This change refines the return type of
__abs__ and introduces new expression classes.

- Change __abs__ return type from GenExpr to AbsExpr
- Add AbsExpr, ExpExpr, LogExpr, SqrtExpr, SinExpr, and CosExpr classes
- All new classes inherit from UnaryExpr
Copilot AI lite review requested due to automatic review settings September 19, 2026 02:31

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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.

@
Return a string from VarExpr.__repr__

VarExpr.__repr__ returned the wrapped Variable object instead of its name.
Since Variable defines __repr__ but no __str__, str() on it fell back to
__repr__, handed the non-string object back, and str.__str__ raised
TypeError: __str__ returned non-string (type pyscipopt.scip.Variable).

SumExpr and ProdExpr render their children with map(str, ...), so any
VarExpr reached by buildGenExprObj or GenExpr.__add__/__mul__ blew up --
e.g. str(abs(x)), str(sqrt(x) * -1), and repr(2**x) vs repr(exp(x * log(2))).
PowExpr and the unary __repr__ methods relied on the same implicit str(),
so the leaf node is now responsible for returning a real string.

Also annotate the __repr__ return type as str.
@
@codecov

codecov Bot commented Sep 19, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.21429% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 57.71%. Comparing base (9547994) to head (c8ed7ef).
⚠️ Report is 15 commits behind head on master.

Files with missing lines Patch % Lines
src/pyscipopt/scip.pxi 93.33% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1259      +/-   ##
==========================================
- Coverage   57.91%   57.71%   -0.20%     
==========================================
  Files          26       27       +1     
  Lines        5807     5969     +162     
==========================================
+ Hits         3363     3445      +82     
- Misses       2444     2524      +80     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Zeroto521

Copy link
Copy Markdown
Contributor Author

Codecov CI error on upstream: codecov/codecov-action#1975

with:
  fail_ci_if_error: false
env:
  version: 10.0.0
  pythonLocation: /opt/hostedtoolcache/Python/3.11.16/x64
  PKG_CONFIG_PATH: /opt/hostedtoolcache/Python/3.11.16/x64/lib/pkgconfig
  Python_ROOT_DIR: /opt/hostedtoolcache/Python/3.11.16/x64
  Python2_ROOT_DIR: /opt/hostedtoolcache/Python/3.11.16/x64
  Python3_ROOT_DIR: /opt/hostedtoolcache/Python/3.11.16/x64
  LD_LIBRARY_PATH: /opt/hostedtoolcache/Python/3.11.16/x64/lib
==> linux OS detected
Error: write EPROTO 406D8E39717F0000:error:0A000410:SSL routines:ssl3_read_bytes:ssl/tls alert handshake failure:../deps/openssl/openssl/ssl/record/rec_layer_s3.c:918:SSL alert number 40

    at WriteWrap.onWriteComplete [as oncomplete] (node:internal/stream_base_commons:87:19)

Comment thread src/pyscipopt/expr.pxi Outdated
Co-authored-by: João Dionísio <57299939+Joao-Dionisio@users.noreply.github.com>
Comment thread src/pyscipopt/expr.pxi
'''Note: none of these expressions should be polynomial'''
return INFINITY

def getOp(self):

@Joao-Dionisio Joao-Dionisio Oct 8, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think there should still be a comfortable way for users to access this information. It's currently used in https://github.com/feloopy/feloopy, for example.

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 is a breaking change for downstream.

Before

https://github.com/feloopy/feloopy/blob/c1f8a8a153d4fc3bafec51a08686e48b3b253b6c/feloopy/generators/solution/scip_solution_generator.py#L216-L233

After

from pyscipopt.scip import LogExpr, ExpExpr, SinExpr, CosExpr, SqrtExpr

if isinstance(expr, LogExpr): return log(child)
elif isinstance(expr, ExpExpr): return exp(child)

If we merge Expr (without Op) and GenExpr (with Op), we need to drop the Op attribute.
For a breaking change, we can add a deprecation warning in version 6.2.2 and remove it in 6.3.x.

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.

Some bugs I found.

  1. SumExpr.coefs was removed
    if isinstance(expr, SumExpr):
        result = Expr()
        result = result + expr.constant
        for coef, child in zip(expr.coefs, expr.children):
            result = result + coef * _remap_scip_expr(child, var_map)
        return result
  1. PySCIPOpt doesn't have a tan function.
        elif op == 'tan':
            from pyscipopt.scip import tan as scip_fn

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@Zeroto521 A deprecation warning would be nice, yes, thank you.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@k-tafakkori Hey! We're planning on merging some things that should impact your work on feloopy. Nothing too major, just giving you a heads up.

This change assigns explicit operator tags to expression objects such as sum, product, abs, exp, log, and trig functions, and exposes them via GenExpr.getOp(). It updates ExprLike constructors and constant/unary expression initialization so operator metadata stays consistent across copied and wrapped expressions. The corresponding type stubs in scip.pyi are also updated for the new Operator API.

This branch has not been deployed

No deployments
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