Skip to content

Commit 03ce35c

Browse files
committed
Use helper predicates for expr type checks
Replace the EXPR_OP_TYPES/GENEXPR_OP_TYPES isinstance tuples with inline helper predicates (_is_expr_compatible and _is_genexpr_compatible) and update all instanceof checks across Expr/GenExpr methods, buildGenExprObj, and _expr_richcmp to use them. This centralizes compatibility logic (numbers, Expr and GenExpr) and preserves existing behavior while removing the type-tuple constants.
1 parent 968cda4 commit 03ce35c

1 file changed

Lines changed: 17 additions & 14 deletions

File tree

src/pyscipopt/expr.pxi

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ if TYPE_CHECKING:
6363

6464

6565
def _expr_richcmp(self: Union[Expr, GenExpr], other, int op):
66-
if not isinstance(other, GENEXPR_OP_TYPES):
66+
if not _is_genexpr_compatible(other):
6767
return NotImplemented
6868

6969
if op == Py_LE:
@@ -164,7 +164,7 @@ CONST = Term()
164164
# helper function
165165
def buildGenExprObj(expr: Union[int, float, Expr, GenExpr]) -> GenExpr:
166166
"""helper function to generate an object of type GenExpr"""
167-
if not isinstance(expr, GENEXPR_OP_TYPES):
167+
if not _is_genexpr_compatible(expr):
168168
raise TypeError(f"Unsupported type {type(expr)}")
169169

170170
if _is_number(expr):
@@ -216,7 +216,7 @@ cdef class Expr:
216216
return abs(buildGenExprObj(self))
217217

218218
def __add__(self, other):
219-
if not isinstance(other, EXPR_OP_TYPES):
219+
if not _is_expr_compatible(other):
220220
return NotImplemented
221221

222222
left = self
@@ -233,7 +233,7 @@ cdef class Expr:
233233
return Expr(terms)
234234

235235
def __iadd__(self, other):
236-
if not isinstance(other, EXPR_OP_TYPES):
236+
if not _is_expr_compatible(other):
237237
return NotImplemented
238238

239239
if isinstance(other, Expr):
@@ -245,7 +245,7 @@ cdef class Expr:
245245
return self
246246

247247
def __mul__(self, other):
248-
if not isinstance(other, EXPR_OP_TYPES):
248+
if not _is_expr_compatible(other):
249249
return NotImplemented
250250

251251
cdef dict res = {}
@@ -276,7 +276,7 @@ cdef class Expr:
276276
return Expr(res)
277277

278278
def __truediv__(self, other):
279-
if not isinstance(other, EXPR_OP_TYPES):
279+
if not _is_expr_compatible(other):
280280
return NotImplemented
281281

282282
if _is_number(other):
@@ -285,7 +285,7 @@ cdef class Expr:
285285

286286
def __rtruediv__(self, other):
287287
''' other / self '''
288-
if not isinstance(other, EXPR_OP_TYPES):
288+
if not _is_expr_compatible(other):
289289
return NotImplemented
290290
return buildGenExprObj(other) / self
291291

@@ -475,7 +475,7 @@ cdef class GenExpr:
475475
return UnaryExpr(Operator.fabs, self)
476476

477477
def __add__(self, other):
478-
if not isinstance(other, GENEXPR_OP_TYPES):
478+
if not _is_genexpr_compatible(other):
479479
return NotImplemented
480480

481481
left = buildGenExprObj(self)
@@ -533,7 +533,7 @@ cdef class GenExpr:
533533
# return self
534534

535535
def __mul__(self, other):
536-
if not isinstance(other, GENEXPR_OP_TYPES):
536+
if not _is_genexpr_compatible(other):
537537
return NotImplemented
538538

539539
left = buildGenExprObj(self)
@@ -607,7 +607,7 @@ cdef class GenExpr:
607607

608608
#TODO: ipow, idiv, etc
609609
def __truediv__(self,other):
610-
if not isinstance(other, GENEXPR_OP_TYPES):
610+
if not _is_genexpr_compatible(other):
611611
return NotImplemented
612612

613613
divisor = buildGenExprObj(other)
@@ -618,7 +618,7 @@ cdef class GenExpr:
618618

619619
def __rtruediv__(self, other):
620620
''' other / self '''
621-
if not isinstance(other, GENEXPR_OP_TYPES):
621+
if not _is_genexpr_compatible(other):
622622
return NotImplemented
623623
return buildGenExprObj(other) / self
624624

@@ -870,8 +870,11 @@ def expr_to_array(expr, nodes):
870870
return len(nodes) - 1
871871

872872

873-
cdef tuple EXPR_OP_TYPES = (int, float, np.number, Expr)
874-
cdef tuple GENEXPR_OP_TYPES = EXPR_OP_TYPES + (GenExpr,)
875-
876873
cdef inline bint _is_number(object o):
877874
return not PyArray_Check(o) and PyNumber_Check(o)
875+
876+
cdef inline bint _is_expr_compatible(object o):
877+
return _is_number(o) or isinstance(o, Expr)
878+
879+
cdef inline bint _is_genexpr_compatible(object o):
880+
return _is_expr_compatible(o) or isinstance(o, GenExpr)

0 commit comments

Comments
 (0)