Skip to content

Commit

Permalink
Make sure the ~PY sigil does not trigger an unused variable warning (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonatanklosko authored Feb 23, 2025
1 parent 3ed9209 commit a89aa68
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/pythonx.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
19 changes: 19 additions & 0 deletions test/pythonx_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a89aa68

Please sign in to comment.