From 1db1dfb733692f07a87faf6744b4d4b3276880f8 Mon Sep 17 00:00:00 2001 From: Saul Shanabrook Date: Wed, 23 Oct 2024 16:31:55 -0400 Subject: [PATCH 1/2] Add failing no import * star test --- python/tests/test_no_import_star.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 python/tests/test_no_import_star.py diff --git a/python/tests/test_no_import_star.py b/python/tests/test_no_import_star.py new file mode 100644 index 00000000..085f8aa5 --- /dev/null +++ b/python/tests/test_no_import_star.py @@ -0,0 +1,12 @@ +import egglog as el + + +def test_no_import_star(): + """ + https://github.com/egraphs-good/egglog-python/issues/210 + """ + + class Num(el.Expr): + def __init__(self, value: el.i64Like) -> None: ... + + Num(1) # gets an error "NameError: name 'i64' is not defined" From 59d538001dbf6937d7f11652d4b00231508f8062 Mon Sep 17 00:00:00 2001 From: Saul Shanabrook Date: Wed, 23 Oct 2024 17:06:20 -0400 Subject: [PATCH 2/2] Try fixing by only passing globals to get_type_hints --- python/egglog/egraph.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/python/egglog/egraph.py b/python/egglog/egraph.py index a25411fe..26f7cb39 100644 --- a/python/egglog/egraph.py +++ b/python/egglog/egraph.py @@ -704,8 +704,12 @@ def _fn_decl( # https://docs.astral.sh/ruff/rules/typing-only-standard-library-import/ if "Callable" not in hint_globals: hint_globals["Callable"] = Callable - - hints = get_type_hints(fn, hint_globals, hint_locals) + # Instead of passing both globals and locals, just pass the globals. Otherwise, for some reason forward references + # won't be resolved correctly + # We need this to be false so it returns "__forward_value__" https://github.com/python/cpython/blob/440ed18e08887b958ad50db1b823e692a747b671/Lib/typing.py#L919 + # https://github.com/egraphs-good/egglog-python/issues/210 + hint_globals.update(hint_locals) + hints = get_type_hints(fn, hint_globals) params = list(signature(fn).parameters.values())