Skip to content

Commit 9a8f3eb

Browse files
Review comments
1 parent a2ec9e9 commit 9a8f3eb

3 files changed

Lines changed: 31 additions & 18 deletions

File tree

src/pyscipopt/scip.pyi

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import os
22
from collections.abc import Iterable, Mapping, Sequence
3-
from typing import Any, AnyStr, ClassVar, Literal, Self, SupportsFloat, Union, overload
3+
from typing import Any, AnyStr, ClassVar, Literal, SupportsFloat, Union, overload
44

55
import numpy as np
66
from _typeshed import Incomplete
7-
from typing_extensions import CapsuleType, disjoint_base
7+
from typing_extensions import CapsuleType, Self, disjoint_base
88

99
CONST: Term
1010
EventNames: dict
@@ -31,25 +31,25 @@ str_conversion: Incomplete
3131
value_to_array: Incomplete
3232

3333
@overload
34-
def exp(x: Expr | GenExpr) -> UnaryExpr: ...
34+
def exp(x: float | Expr | GenExpr) -> UnaryExpr: ...
3535
@overload
36-
def exp(x: MatrixExpr) -> MatrixGenExpr: ...
36+
def exp(x: list | tuple | np.ndarray | MatrixExpr) -> MatrixGenExpr: ...
3737
@overload
38-
def log(x: Expr | GenExpr) -> UnaryExpr: ...
38+
def log(x: float | Expr | GenExpr) -> UnaryExpr: ...
3939
@overload
40-
def log(x: MatrixExpr) -> MatrixGenExpr: ...
40+
def log(x: list | tuple | np.ndarray | MatrixExpr) -> MatrixGenExpr: ...
4141
@overload
42-
def sqrt(x: Expr | GenExpr) -> UnaryExpr: ...
42+
def sqrt(x: float | Expr | GenExpr) -> UnaryExpr: ...
4343
@overload
44-
def sqrt(x: MatrixExpr) -> MatrixGenExpr: ...
44+
def sqrt(x: list | tuple | np.ndarray | MatrixExpr) -> MatrixGenExpr: ...
4545
@overload
46-
def sin(x: Expr | GenExpr) -> UnaryExpr: ...
46+
def sin(x: float | Expr | GenExpr) -> UnaryExpr: ...
4747
@overload
48-
def sin(x: MatrixExpr) -> MatrixGenExpr: ...
48+
def sin(x: list | tuple | np.ndarray | MatrixExpr) -> MatrixGenExpr: ...
4949
@overload
50-
def cos(x: Expr | GenExpr) -> UnaryExpr: ...
50+
def cos(x: float | Expr | GenExpr) -> UnaryExpr: ...
5151
@overload
52-
def cos(x: MatrixExpr) -> MatrixGenExpr: ...
52+
def cos(x: list | tuple | np.ndarray | MatrixExpr) -> MatrixGenExpr: ...
5353

5454
@disjoint_base
5555
class Benders:
@@ -253,7 +253,7 @@ class Constant(GenExpr):
253253
number: Incomplete
254254
def __init__(self, *args: Incomplete, **kwargs: Incomplete) -> None: ...
255255
def __pow__( # type: ignore[override]
256-
self, other: float | Constant, modulo: Incomplete = ..., /
256+
self, other: float | Constant, mod: Incomplete = ..., /
257257
) -> Constant: ...
258258

259259
@disjoint_base
@@ -409,10 +409,17 @@ class Expr(ExprLike):
409409
@overload
410410
def __truediv__(self, other: np.ndarray | MatrixExpr, /) -> MatrixExpr: ...
411411
def __rtruediv__(self, other: float, /) -> ProdExpr: ...
412+
# __pow__'s return type depends on the value of `other`:
413+
# non-negative integers (including integer-valued floats) yield Expr,
414+
# negative integers and non-integers yield PowExpr
415+
# We annotate the int case as Expr to handle the common case (e.g. x**2)
416+
# knowing it might be wrong in some cases, and likewise the float case as
417+
# PowExpr can be wrong. We aim to make the stubs useful in common cases
418+
# even though the type system cannot fully express the real types.
412419
@overload
413-
def __pow__(self, other: int, modulo: Incomplete = ..., /) -> Expr: ...
420+
def __pow__(self, other: int, mod: Incomplete = ..., /) -> Expr: ...
414421
@overload
415-
def __pow__(self, other: float, module: Incomplete = ..., /) -> PowExpr: ...
422+
def __pow__(self, other: float, mod: Incomplete = ..., /) -> PowExpr: ...
416423
def __rpow__(self, other: float, /) -> UnaryExpr: ...
417424
def __getitem__(self, index: Incomplete, /) -> Incomplete: ...
418425
def __iter__(self) -> Incomplete: ...
@@ -458,7 +465,6 @@ class GenExpr(ExprLike):
458465
@overload
459466
def __truediv__(self, other: np.ndarray | MatrixExpr, /) -> MatrixExpr: ...
460467
def __rtruediv__(self, other: float, /) -> ProdExpr: ...
461-
def __ne__(self, other: object, /) -> bool: ...
462468
def __pow__(self, other: float | Constant, mod: Incomplete = ..., /) -> PowExpr: ...
463469
def __rpow__(self, other: float, mod: Incomplete = ..., /) -> UnaryExpr: ...
464470

stubs/baseline.sh

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ python "$REPO_ROOT"/scripts/generate_expr_type_tests.py
99
for test_file in "$REPO_ROOT"/tests/@types/*.py; do
1010
echo "Updating mypy baseline for $test_file"
1111
output_file="${test_file%.*}.mypy.out"
12-
python -m mypy "$test_file" --warn-unused-ignores | grep "error:" > /tmp/pyscipopt-mypy-output.txt
13-
cp /tmp/pyscipopt-mypy-output.txt "$output_file"
12+
# write to a temp file first to avoid truncating the output file if mypy fails
13+
tmp=$(mktemp)
14+
python -m mypy "$test_file" --warn-unused-ignores | grep "error:" > "$tmp"
15+
mv "$tmp" "$output_file"
1416
done

tests/@types/expr.mypy.out

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ tests/@types/expr.py:155: error: Expression is of type "Any", not "MatrixExpr"
1717
tests/@types/expr.py:242: error: Unused "type: ignore" comment [unused-ignore]
1818
tests/@types/expr.py:243: error: Unused "type: ignore" comment [unused-ignore]
1919
tests/@types/expr.py:244: error: Unused "type: ignore" comment [unused-ignore]
20+
tests/@types/expr.py:245: error: Unused "type: ignore" comment [unused-ignore]
21+
tests/@types/expr.py:246: error: Unused "type: ignore" comment [unused-ignore]
22+
tests/@types/expr.py:247: error: Unused "type: ignore" comment [unused-ignore]
23+
tests/@types/expr.py:248: error: Unused "type: ignore" comment [unused-ignore]
24+
tests/@types/expr.py:249: error: Unused "type: ignore" comment [unused-ignore]
2025
tests/@types/expr.py:250: error: Unused "type: ignore" comment [unused-ignore]
2126
tests/@types/expr.py:251: error: Unused "type: ignore" comment [unused-ignore]
2227
tests/@types/expr.py:270: error: Unused "type: ignore" comment [unused-ignore]

0 commit comments

Comments
 (0)