Skip to content

Commit 4d5aca0

Browse files
Add __round__
1 parent 4d5382a commit 4d5aca0

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed

python/egglog/exp/array_api.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,8 @@ def __hash__(self) -> int:
164164
simplified = egraph.extract(self)
165165
return hash(cast("RuntimeExpr", simplified).__egg_typed_expr__)
166166

167+
def __round__(self, ndigits: OptionalIntLike = None) -> Int: ...
168+
167169
# TODO: Fix this?
168170
# Make != always return a Bool, so that numpy.unique works on a tuple of ints
169171
# In _unique1d
@@ -290,6 +292,8 @@ def _int(i: i64, j: i64, r: Boolean, o: Int, b: Int):
290292
yield rewrite(Int.if_(TRUE, o, b), subsume=True).to(o)
291293
yield rewrite(Int.if_(FALSE, o, b), subsume=True).to(b)
292294

295+
yield rewrite(o.__round__(OptionalInt.none)).to(o)
296+
293297
# Never cannot be equal to anything real
294298
yield rule(eq(Int.NEVER).to(Int(i))).then(panic("Int.NEVER cannot be equal to any real int"))
295299

@@ -681,6 +685,8 @@ class OptionalInt(Expr, ruleset=array_api_ruleset):
681685
def some(cls, value: Int) -> OptionalInt: ...
682686

683687

688+
OptionalIntLike: TypeAlias = OptionalInt | IntLike | None
689+
684690
converter(type(None), OptionalInt, lambda _: OptionalInt.none)
685691
converter(Int, OptionalInt, OptionalInt.some)
686692

python/egglog/runtime.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
"__le__",
9090
"__gt__",
9191
"__ge__",
92+
"__round__",
9293
}
9394

9495
# Set this globally so we can get access to PyObject when we have a type annotation of just object.

0 commit comments

Comments
 (0)