Skip to content

Conversation

@jonatanklosko
Copy link
Member

Currently when ~PY references an undefined variable, it results in Elixir compile error, because the expanded AST references the corresponding Elixir variable. This PR changes it, such that we don't generate such AST nodes and let the Python code run and fail. This way we fail later, but provide a more informative error (actually related to the code), including line information.

lib/pythonx.ex Outdated
defmacro sigil_PY({:<<>>, _meta, [code]}, []) when is_binary(code) do
%{referenced: referenced, defined: defined} = Pythonx.AST.scan_globals(code)

versioned_vars = __CALLER__.versioned_vars
Copy link
Member Author

Choose a reason for hiding this comment

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

@josevalim let me know if you have any concerns with this approach :)

Copy link
Contributor

Choose a reason for hiding this comment

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

Let’s use this function instead: https://hexdocs.pm/elixir/Macro.Env.html#has_var?/2

Copy link
Member Author

Choose a reason for hiding this comment

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

Ohh perfect, I was almost certain we had this function, it's since v1.7 so I don't know how I missed it :D Thanks!

@jonatanklosko jonatanklosko merged commit b07752f into main Feb 27, 2025
9 checks passed
@jonatanklosko jonatanklosko deleted the jk-undefined-vars branch February 27, 2025 04:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants