Skip to content

Commit 48bc65c

Browse files
Merge pull request #43 from oscarbenjamin/pr_sym
Add Sym as a superclass for Expr
2 parents 1fd7873 + 541544b commit 48bc65c

File tree

7 files changed

+925
-117
lines changed

7 files changed

+925
-117
lines changed

pyproject.toml

+2
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ fail_under = 100
8080
exclude_lines = [
8181
"pragma: no cover",
8282
"if _TYPE_CHECKING:",
83+
"@overload",
8384
]
8485

8586
[tool.mypy]
@@ -96,6 +97,7 @@ module = [
9697
'llvmlite.binding',
9798
'py',
9899
'symengine',
100+
'numpy',
99101
]
100102
ignore_missing_imports = true
101103

src/protosym/core/evaluate.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@
2626

2727

2828
if _TYPE_CHECKING:
29-
from typing import Optional, Iterable, Any
29+
from typing import Optional, Sequence, Any
3030

3131
Op1 = Callable[[_T], _T]
3232
Op2 = Callable[[_T, _T], _T]
33-
OpN = Callable[[Iterable[_T]], _T]
33+
OpN = Callable[[Sequence[_T]], _T]
3434

3535

36-
def _generic_operation_error(head: TreeExpr, argvals: Iterable[_T]) -> _T:
36+
def _generic_operation_error(head: TreeExpr, argvals: Sequence[_T]) -> _T:
3737
"""Error fallback rule for handling unknown heads."""
3838
msg = "No rule for head: " + repr(head)
3939
raise NoEvaluationRuleError(msg)
@@ -91,7 +91,7 @@ class Evaluator(Generic[_T]):
9191

9292
atoms: dict[AtomType[_AnyValue], Callable[[_AnyValue], _T]]
9393
operations: dict[TreeExpr, tuple[Callable[..., _T], bool]]
94-
generic_operation_func: Callable[[TreeExpr, Iterable[_T]], _T]
94+
generic_operation_func: Callable[[TreeExpr, Sequence[_T]], _T]
9595
generic_atom_func: Callable[[TreeAtom[_S]], _T]
9696

9797
def __init__(self) -> None:
@@ -123,7 +123,7 @@ def add_opn(self, head: TreeExpr, func: OpN[_T]) -> None:
123123
"""Add an evaluation rule for a particular head."""
124124
self.operations[head] = (func, False)
125125

126-
def add_op_generic(self, func: Callable[[TreeExpr, Iterable[_T]], _T]) -> None:
126+
def add_op_generic(self, func: Callable[[TreeExpr, Sequence[_T]], _T]) -> None:
127127
"""Add a generic fallback rule for heads."""
128128
self.generic_operation_func = func
129129

@@ -135,7 +135,7 @@ def eval_atom(self, atom: TreeAtom[_S]) -> _T:
135135
return self.generic_atom_func(atom)
136136
return atom_func(atom_value.value)
137137

138-
def eval_operation(self, head: TreeExpr, argvals: Iterable[_T]) -> _T:
138+
def eval_operation(self, head: TreeExpr, argvals: Sequence[_T]) -> _T:
139139
"""Evaluate one function with some values."""
140140
func_star = self.operations.get(head)
141141

src/protosym/core/exceptions.py

+6
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,9 @@ class NoEvaluationRuleError(ProtoSymError):
1111
"""Raised when an :class:`Evaluator` has no rule for an expression."""
1212

1313
pass
14+
15+
16+
class BadRuleError(ProtoSymError):
17+
"""Raised when an :class:`Evaluator` is given an invalid rule."""
18+
19+
pass

0 commit comments

Comments
 (0)