From b371dca4dbc8916ab463214ed6494b8c3775f505 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonatan=20K=C5=82osko?= Date: Sun, 23 Feb 2025 19:51:39 +0900 Subject: [PATCH] Make sure the ~PY sigil does not trigger an unused variable warning --- lib/pythonx.ex | 3 +++ test/pythonx_test.exs | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/lib/pythonx.ex b/lib/pythonx.ex index b922eec..49285ee 100644 --- a/lib/pythonx.ex +++ b/lib/pythonx.ex @@ -290,6 +290,9 @@ defmodule Pythonx do for name <- defined do quote do unquote({String.to_atom(name), [], nil}) = Map.get(globals, unquote(name), nil) + # We do an extra underscore assignment to make sure the + # generated code does not trigger an unused variable warning. + _ = unquote({String.to_atom(name), [], nil}) end end diff --git a/test/pythonx_test.exs b/test/pythonx_test.exs index 7f57e1a..dc42ef0 100644 --- a/test/pythonx_test.exs +++ b/test/pythonx_test.exs @@ -413,6 +413,25 @@ defmodule PythonxTest do assert repr(result) == "43" end + + test "does not result in unused variables" do + {_result, diagnostics} = + Code.with_diagnostics(fn -> + Code.eval_string(~s''' + defmodule TestModule#{System.unique_integer([:positive])} do + import Pythonx + + def run() do + ~PY""" + x = 1 + """ + end + end + ''') + end) + + assert diagnostics == [] + end end defp repr(object) do