From 640d821108b6c4ed223b4b8ca1153facb558f870 Mon Sep 17 00:00:00 2001 From: Micha Reiser Date: Thu, 27 Mar 2025 04:04:57 +0100 Subject: [PATCH] [red-knot] `reveal-type` should return the revaled type (#17007) ## Summary Return the revealed-type from the monkey-patched `revale_type` implementation to preserve the identity behavior. This PR also isolates different script runs by assigning a different `globals` dict for each script-run. See https://github.com/pyodide/pyodide/issues/703 --- .github/workflows/publish-knot-playground.yml | 8 +++--- playground/knot/src/Editor/SecondaryPanel.tsx | 26 +++++++++++++++++-- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/.github/workflows/publish-knot-playground.yml b/.github/workflows/publish-knot-playground.yml index 6dbacdf82e..51b118caf9 100644 --- a/.github/workflows/publish-knot-playground.yml +++ b/.github/workflows/publish-knot-playground.yml @@ -8,10 +8,10 @@ on: branches: [main] paths: - "crates/red_knot*/**" - - "crates/ruff_db" - - "crates/ruff_python_ast" - - "crates/ruff_python_parser" - - "playground" + - "crates/ruff_db/**" + - "crates/ruff_python_ast/**" + - "crates/ruff_python_parser/**" + - "playground/**" - ".github/workflows/publish-knot-playground.yml" concurrency: diff --git a/playground/knot/src/Editor/SecondaryPanel.tsx b/playground/knot/src/Editor/SecondaryPanel.tsx index 0c0a09a506..18b798feda 100644 --- a/playground/knot/src/Editor/SecondaryPanel.tsx +++ b/playground/knot/src/Editor/SecondaryPanel.tsx @@ -144,20 +144,42 @@ function RunWithPyiodide({ const main = files.selected == null ? "" : files.contents[files.selected]; + let fileName = "main.py"; for (const file of files.index) { pyodide.FS.writeFile(file.name, files.contents[file.id]); + + if (file.id === files.selected) { + fileName = file.name; + } } + const dict = pyodide.globals.get("dict"); + const globals = dict(); + try { // Patch up reveal types pyodide.runPython(` import builtins - builtins.reveal_type = print`); - pyodide.runPython(main); + def reveal_type(obj): + import typing + print(f"Runtime value is: \`{obj}\`") + return typing.reveal_type(obj) + + builtins.reveal_type = reveal_type`); + + pyodide.runPython(main, { + globals, + locals: globals, + filename: fileName, + }); + setOutput(stdout); } catch (e) { setOutput(`Failed to run Python script: ${e}`); + } finally { + globals.destroy(); + dict.destroy(); } }; return (