Skip to content
Open
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
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
exclude: ^python/tests/__snapshots__/
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.14.3
rev: v0.16.0
hooks:
- id: ruff-check
args: [--fix]
- id: ruff-format
- repo: https://github.com/astral-sh/uv-pre-commit
rev: 0.9.7
rev: 0.11.32
hooks:
- id: uv-lock
2 changes: 1 addition & 1 deletion python/egglog/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from . import config, ipython_magic # noqa: F401
from .bindings import EggSmolError, StageInfo, TimeOnly, WithPlan # noqa: F401
from .builtins import * # noqa: UP029
from .builtins import *
from .conversion import *
from .deconstruct import *
from .egraph import *
Expand Down
5 changes: 2 additions & 3 deletions python/egglog/declarations.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
"ConstantRef",
"ConstructorDecl",
"Declarations",
"Declarations",
"DeclarationsLike",
"DefaultRewriteDecl",
"DelayedDeclarations",
Expand Down Expand Up @@ -128,7 +127,7 @@ class HasDeclarations(Protocol):
def __egg_decls__(self) -> Declarations: ...


DeclarationsLike: TypeAlias = Union[HasDeclarations, None, "Declarations"]
DeclarationsLike: TypeAlias = Union[HasDeclarations, "Declarations", None]


def upcast_declarations(declarations_like: Iterable[DeclarationsLike]) -> list[Declarations]:
Expand Down Expand Up @@ -659,7 +658,7 @@ def signature(self) -> FunctionSignature:
)

@property
def egg_name(self) -> None | str:
def egg_name(self) -> str | None:
return None


Expand Down
6 changes: 2 additions & 4 deletions python/egglog/egraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,11 @@
"BaseExpr",
"BuiltinExpr",
"Command",
"Command",
"CostModel",
"EGraph",
"Expr",
"ExprCallable",
"Fact",
"Fact",
"GraphvizKwargs",
"GreedyDagCost",
"RewriteOrRule",
Expand Down Expand Up @@ -1538,7 +1536,7 @@ def __call__(self, *args, **kwargs) -> Never:
def ruleset(
rule_or_generator: RewriteOrRule | RewriteOrRuleGenerator | None = None,
*rules: RewriteOrRule,
name: None | str = None,
name: str | None = None,
) -> Ruleset:
"""
Creates a ruleset with the following rules.
Expand Down Expand Up @@ -2086,7 +2084,7 @@ def run(ruleset: Ruleset | None = None, *until: FactLike, scheduler: BackOff | N
)


def back_off(match_limit: None | int = None, ban_length: None | int = None) -> BackOff:
def back_off(match_limit: int | None = None, ban_length: int | None = None) -> BackOff:
"""
Create a backoff scheduler configuration.

Expand Down
4 changes: 2 additions & 2 deletions python/egglog/exp/any_expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -753,8 +753,8 @@ def any_eval(self: A) -> Any:
return res


_CURRENT_EGRAPH: None | EGraph = None
_LAST_ASSERT: None | A = None
_CURRENT_EGRAPH: EGraph | None = None
_LAST_ASSERT: A | None = None


@contextlib.contextmanager
Expand Down
12 changes: 7 additions & 5 deletions python/egglog/exp/array_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1459,7 +1459,7 @@ def slice(cls, slice: Slice) -> MultiAxisIndexKeyItem: ...
converter(Int, MultiAxisIndexKeyItem, lambda i: MultiAxisIndexKeyItem.int(i))
converter(Slice, MultiAxisIndexKeyItem, lambda s: MultiAxisIndexKeyItem.slice(s))

MultiAxisIndexKeyItemLike: TypeAlias = MultiAxisIndexKeyItem | EllipsisType | None | IntLike | SliceLike
MultiAxisIndexKeyItemLike: TypeAlias = MultiAxisIndexKeyItem | EllipsisType | IntLike | SliceLike | None


class MultiAxisIndexKey(Expr, ruleset=array_api_ruleset):
Expand Down Expand Up @@ -2123,7 +2123,7 @@ def int(cls, value: Int) -> OptionalIntOrTuple: ...
def tuple(cls, value: TupleIntLike) -> OptionalIntOrTuple: ...


OptionalIntOrTupleLike: TypeAlias = OptionalIntOrTuple | None | IntLike | TupleIntLike
OptionalIntOrTupleLike: TypeAlias = OptionalIntOrTuple | IntLike | TupleIntLike | None

converter(type(None), OptionalIntOrTuple, lambda _: OptionalIntOrTuple.none)
converter(Int, OptionalIntOrTuple, lambda v: OptionalIntOrTuple.int(v))
Expand Down Expand Up @@ -2379,7 +2379,8 @@ def vecdot(x1: NDArrayLike, x2: NDArrayLike) -> NDArray:
x1.shape.drop_last(),
x1.dtype,
lambda idx: (
TupleInt.range(x1.shape.last())
TupleInt
.range(x1.shape.last())
.map_value(lambda i: x1.index(idx.append(i)) * x2.index((i,)))
.foldl_value(Value.__add__, Value.from_float(0))
),
Expand Down Expand Up @@ -2740,7 +2741,8 @@ def unravel_index(flat_index: IntLike, shape: TupleIntLike) -> TupleInt:
shape = cast("TupleInt", shape)

return (
shape.reverse()
shape
.reverse()
.foldl_tuple_int(
# Store remainder as last item in accumulator
lambda acc, dim: acc.drop_last().append((r := acc.last()) % dim).append(r // dim),
Expand All @@ -2754,7 +2756,7 @@ def unravel_index(flat_index: IntLike, shape: TupleIntLike) -> TupleInt:
array_api_combined_ruleset = array_api_ruleset
array_api_schedule = (array_api_combined_ruleset + run()).saturate()

_CURRENT_EGRAPH: None | EGraph = None
_CURRENT_EGRAPH: EGraph | None = None


@contextlib.contextmanager
Expand Down
2 changes: 1 addition & 1 deletion python/egglog/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,7 @@ def to_py_signature(sig: FunctionSignature, decls: Declarations, optional_args:
return Signature(parameters)


ON_CREATE_EXPR: None | Callable[[Callable[[], TypedExprDecl]], None] = None
ON_CREATE_EXPR: Callable[[Callable[[], TypedExprDecl]], None] | None = None


@dataclass
Expand Down
21 changes: 13 additions & 8 deletions python/tests/test_array_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,11 +263,14 @@ def linalg_norm(X: NDArray, axis: TupleIntLike) -> NDArray:
return NDArray.fn(
outshape,
X.dtype,
lambda k: LoopNestAPI.from_tuple(reduce_axis)
.unwrap()
.indices()
.foldl_value(lambda carry, i: carry + ((x := X.index(i + k)).conj() * x).real(), init=0.0)
.sqrt(),
lambda k: (
LoopNestAPI
.from_tuple(reduce_axis)
.unwrap()
.indices()
.foldl_value(lambda carry, i: carry + ((x := X.index(i + k)).conj() * x).real(), init=0.0)
.sqrt()
),
)


Expand All @@ -277,9 +280,11 @@ def linalg_norm_v2(X: NDArrayLike, axis: TupleIntLike) -> NDArray:
return NDArray.fn(
X.shape.deselect(axis),
X.dtype,
lambda k: ndindex(X.shape.select(axis))
.foldl_value(lambda carry, i: carry + ((x := X.index(i + k)).conj() * x).real(), init=0.0)
.sqrt(),
lambda k: (
ndindex(X.shape.select(axis))
.foldl_value(lambda carry, i: carry + ((x := X.index(i + k)).conj() * x).real(), init=0.0)
.sqrt()
),
)


Expand Down
Loading