Skip to content

Commit

Permalink
feat(python): warn about inefficient apply json.loads if json is loca…
Browse files Browse the repository at this point in the history
…l import (#10310)
  • Loading branch information
MarcoGorelli authored Aug 5, 2023
1 parent 2fc1491 commit 82c1685
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
6 changes: 4 additions & 2 deletions py-polars/polars/utils/udfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,9 @@ class RewrittenInstructions:
from the identification of expression translation opportunities.
"""

_ignored_ops = frozenset(["COPY_FREE_VARS", "PRECALL", "RESUME", "RETURN_VALUE"])
_ignored_ops = frozenset(
["COPY_FREE_VARS", "PRECALL", "PUSH_NULL", "RESUME", "RETURN_VALUE"]
)

def __init__(self, instructions: Iterator[Instruction]):
self._original_instructions = list(instructions)
Expand Down Expand Up @@ -636,7 +638,7 @@ def _rewrite_functions(
if matching_instructions := self._matches(
idx,
opnames=[
{"LOAD_GLOBAL"},
{"LOAD_GLOBAL", "LOAD_DEREF"},
OpNames.LOAD_ATTR,
{"LOAD_FAST", "LOAD_CONST"},
OpNames.CALL,
Expand Down
14 changes: 14 additions & 0 deletions py-polars/tests/test_udfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,17 @@ def test_bytecode_parser_expression_noop(func: Callable[[Any], Any]) -> None:

parser = udfs.BytecodeParser(func, apply_target="expr")
assert not parser.can_attempt_rewrite() or not parser.to_expression("x")


def test_local_imports() -> None:
try:
import udfs
except ModuleNotFoundError as exc:
assert "No module named 'udfs'" in str(exc) # noqa: PT017
return
import json

bytecode_parser = udfs.BytecodeParser(lambda x: json.loads(x), apply_target="expr")
result = bytecode_parser.to_expression("x")
expected = 'pl.col("x").str.json_extract()'
assert result == expected

0 comments on commit 82c1685

Please sign in to comment.