Skip to content
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

Improve error messages when using non classvar type annotation #121

Merged
merged 1 commit into from
Feb 16, 2024
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
4 changes: 2 additions & 2 deletions python/egglog/egraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -494,11 +494,11 @@ class _Dummytype:
locals = self.frame.f_locals.copy()
locals[self.cls_name] = self.current_cls
for k, v in get_type_hints(_Dummytype, globalns=self.frame.f_globals, localns=locals).items():
if v.__origin__ == ClassVar:
if getattr(v, "__origin__", None) == ClassVar:
(inner_tp,) = v.__args__
_register_constant(decls, ClassVariableRef(self.cls_name, k), inner_tp, None)
else:
msg = "The only supported annotations on class attributes are class vars"
msg = f"On class {self.cls_name}, for attribute '{k}', expected a ClassVar, but got {v}"
raise NotImplementedError(msg)

# Then register each of its methods
Expand Down
8 changes: 8 additions & 0 deletions python/tests/test_high_level.py
Original file line number Diff line number Diff line change
Expand Up @@ -644,3 +644,11 @@ def g() -> Vec[String]:

assert str(egraph.extract(f())) == "Vec[i64].empty()"
assert str(egraph.extract(g())) == "Vec[String].empty()"


def test_wrong_annotation_error():
class ZX(Expr):
symbol: str

with pytest.raises(NotImplementedError):
ZX.__egg_decls__ # type: ignore[attr-defined]
Loading