Skip to content

Commit 9bb83d4

Browse files
Merge pull request #230 from egraphs-good/fix-loopnest
Add better error message when using @function in class
2 parents 646f15a + 14a7b80 commit 9bb83d4

File tree

3 files changed

+35
-17
lines changed

3 files changed

+35
-17
lines changed

docs/changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ _This project uses semantic versioning_
66

77
- Fix pretty printing of lambda functions
88
- Add support for subsuming rewrite generated by default function and method definitions
9+
- Add better error message when using @function in class (thanks @shinawy)
910

1011
## 8.0.1 (2024-10-24)
1112

python/egglog/egraph.py

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -577,23 +577,25 @@ def _generate_class_decls( # noqa: C901,PLR0912
577577
decl = FunctionDecl(special_function_name, builtin=True, egg_name=egg_fn)
578578
decls.set_function_decl(ref, decl)
579579
continue
580-
581-
_, add_rewrite = _fn_decl(
582-
decls,
583-
egg_fn,
584-
ref,
585-
fn,
586-
locals,
587-
default,
588-
cost,
589-
merge,
590-
on_merge,
591-
mutates,
592-
builtin,
593-
ruleset=ruleset,
594-
unextractable=unextractable,
595-
subsume=subsume,
596-
)
580+
try:
581+
_, add_rewrite = _fn_decl(
582+
decls,
583+
egg_fn,
584+
ref,
585+
fn,
586+
locals,
587+
default,
588+
cost,
589+
merge,
590+
on_merge,
591+
mutates,
592+
builtin,
593+
ruleset=ruleset,
594+
unextractable=unextractable,
595+
subsume=subsume,
596+
)
597+
except ValueError as e:
598+
raise ValueError(f"Error processing {cls_name}.{method_name}: {e}") from e
597599

598600
if not builtin and not isinstance(ref, InitRef) and not mutates:
599601
add_default_funcs.append(add_rewrite)
@@ -721,6 +723,9 @@ def _fn_decl(
721723
"""
722724
Sets the function decl for the function object and returns the ref as well as a thunk that sets the default callable.
723725
"""
726+
if isinstance(fn, RuntimeFunction):
727+
msg = "Inside of classes, wrap methods with the `method` decorator, not `function`"
728+
raise ValueError(msg) # noqa: TRY004
724729
if not isinstance(fn, FunctionType):
725730
raise NotImplementedError(f"Can only generate function decls for functions not {fn} {type(fn)}")
726731

python/tests/test_high_level.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -762,3 +762,15 @@ def test_inserting_map(self):
762762

763763
def test_creating_map(self):
764764
EGraph().simplify(Map[String, i64].empty(), 1)
765+
766+
767+
def test_helpful_error_function_class():
768+
class E(Expr):
769+
@function(cost=10)
770+
def __init__(self) -> None: ...
771+
772+
with pytest.raises(
773+
ValueError,
774+
match="Error processing E.__init__: Inside of classes, wrap methods with the `method` decorator, not `function`",
775+
):
776+
E()

0 commit comments

Comments
 (0)