From f11616594d8cf14efa726576d8d812e16f2eff23 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 18 Dec 2025 00:57:58 +0000 Subject: [PATCH 1/2] Add PyObject.to_int() method returning i64 Add the py-to-int primitive that extracts a Python integer as i64, complementing the existing from_int method for bidirectional conversion. --- python/egglog/builtins.py | 3 +++ src/py_object_sort.rs | 9 +++++++++ 2 files changed, 12 insertions(+) diff --git a/python/egglog/builtins.py b/python/egglog/builtins.py index a9c75f69..c8b9ac09 100644 --- a/python/egglog/builtins.py +++ b/python/egglog/builtins.py @@ -1022,6 +1022,9 @@ def to_string(self) -> String: ... @method(egg_fn="py-to-bool") def to_bool(self) -> Bool: ... + @method(egg_fn="py-to-int") + def to_int(self) -> i64: ... + @method(egg_fn="py-dict-update") def dict_update(self, *keys_and_values: object) -> PyObject: ... diff --git a/src/py_object_sort.rs b/src/py_object_sort.rs index 51de73f9..ad4859ed 100644 --- a/src/py_object_sort.rs +++ b/src/py_object_sort.rs @@ -170,6 +170,15 @@ impl BaseSort for PyObjectSort { } } ); + // (py-to-int ) + add_primitive!( + eg, + "py-to-int" = |x: PyPickledValue| -?> i64 { + { + attach("py-to-int", move |py| load(py, &x)?.extract()) + } + } + ); // (py-from-string ) add_primitive!( eg, From 35ea38267a1152fbbd3fa2dc6172ec49e38998f6 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 18 Dec 2025 16:54:37 +0000 Subject: [PATCH 2/2] Add test for PyObject.to_int() in test_high_level --- python/tests/test_high_level.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/python/tests/test_high_level.py b/python/tests/test_high_level.py index 173e817a..8477101c 100644 --- a/python/tests/test_high_level.py +++ b/python/tests/test_high_level.py @@ -229,6 +229,9 @@ def test_from_string(self): def test_to_string(self): EGraph().check(PyObject("foo").to_string() == String("foo")) + def test_to_int(self): + EGraph().check(PyObject(42).to_int() == i64(42)) + def test_dict_update(self): original_d = {"foo": "bar"} res = EGraph().extract(PyObject(original_d).dict_update("foo", "baz")).value