Skip to content

Add Sym as a superclass for Expr #43

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Mar 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ fail_under = 100
exclude_lines = [
"pragma: no cover",
"if _TYPE_CHECKING:",
"@overload",
]

[tool.mypy]
Expand All @@ -96,6 +97,7 @@ module = [
'llvmlite.binding',
'py',
'symengine',
'numpy',
]
ignore_missing_imports = true

Expand Down
12 changes: 6 additions & 6 deletions src/protosym/core/evaluate.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@


if _TYPE_CHECKING:
from typing import Optional, Iterable, Any
from typing import Optional, Sequence, Any

Op1 = Callable[[_T], _T]
Op2 = Callable[[_T, _T], _T]
OpN = Callable[[Iterable[_T]], _T]
OpN = Callable[[Sequence[_T]], _T]


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

atoms: dict[AtomType[_AnyValue], Callable[[_AnyValue], _T]]
operations: dict[TreeExpr, tuple[Callable[..., _T], bool]]
generic_operation_func: Callable[[TreeExpr, Iterable[_T]], _T]
generic_operation_func: Callable[[TreeExpr, Sequence[_T]], _T]
generic_atom_func: Callable[[TreeAtom[_S]], _T]

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

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

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

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

Expand Down
6 changes: 6 additions & 0 deletions src/protosym/core/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,9 @@ class NoEvaluationRuleError(ProtoSymError):
"""Raised when an :class:`Evaluator` has no rule for an expression."""

pass


class BadRuleError(ProtoSymError):
"""Raised when an :class:`Evaluator` is given an invalid rule."""

pass
Loading