|
20 | 20 | BinOperator = Callable[[Number, Number], Number] |
21 | 21 | UnOperator = Callable[[Number], Number] |
22 | 22 |
|
| 23 | + |
23 | 24 | def safe_eval(s: str) -> Number: |
24 | 25 | def checkmath(x: str, *args: Number) -> Number: |
25 | 26 | if x not in [x for x in dir(math) if "__" not in x]: |
26 | 27 | msg = f"Unknown func {x}()" |
27 | 28 | raise SyntaxError(msg) |
28 | | - fun = cast(MathFunc, getattr(math, x)) |
| 29 | + fun = cast("MathFunc", getattr(math, x)) |
29 | 30 | try: |
30 | 31 | return fun(*args) |
31 | 32 | except TypeError as e: |
@@ -57,12 +58,12 @@ def _eval(node: ast.AST) -> Number: |
57 | 58 | return _eval(node.body) |
58 | 59 | if isinstance(node, ast.Constant): |
59 | 60 | logger.info("Const") |
60 | | - return cast(Number, node.value) |
| 61 | + return cast("Number", node.value) |
61 | 62 | if isinstance(node, ast.Name): |
62 | 63 | # Handle math constants like pi, e, etc. |
63 | 64 | logger.info("MathConst") |
64 | 65 | if hasattr(math, node.id): |
65 | | - return cast(Number, getattr(math, node.id)) |
| 66 | + return cast("Number", getattr(math, node.id)) |
66 | 67 | msg = f"Unknown constant: {node.id}" |
67 | 68 | raise SyntaxError(msg) |
68 | 69 | if isinstance(node, ast.BinOp): |
|
0 commit comments