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

Make sure the ~PY sigil does not trigger an unused variable warning #6

Merged
merged 1 commit into from
Feb 23, 2025
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
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After taking a look at Elixir's source, I also think we can get the same behaviour by marking it as generated:

quote generated: true do

unquote({String.to_atom(name), [], nil}) = Map.get(globals, unquote(name), nil)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or even:

{String.to_atom(name), [generated: true], nil}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Glorious, pushed to main, thank you!

# 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
Loading