Skip to content
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
8 changes: 7 additions & 1 deletion reflex/vars/dep_tracking.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class ScanStatus(enum.Enum):
GETTING_STATE = enum.auto()
GETTING_STATE_POST_AWAIT = enum.auto()
GETTING_VAR = enum.auto()
GETTING_IMPORT = enum.auto()


class UntrackedLocalVarError(VarValueError):
Expand Down Expand Up @@ -446,6 +447,7 @@ def _populate_dependencies(self) -> None:
)
)
elif instruction.opname == "IMPORT_NAME" and instruction.argval is not None:
self.scan_status = ScanStatus.GETTING_IMPORT
self._last_import_name = instruction.argval
importlib.import_module(instruction.argval)
top_module_name = instruction.argval.split(".")[0]
Expand All @@ -472,7 +474,11 @@ def _populate_dependencies(self) -> None:
)
# If we see a STORE_FAST, we can assign the top of stack to an aliased name.
self.top_of_stack = instruction.argval
elif instruction.opname == "STORE_FAST" and self.top_of_stack is not None:
elif (
self.scan_status == ScanStatus.GETTING_IMPORT
and instruction.opname == "STORE_FAST"
and self.top_of_stack is not None
):
self.tracked_locals[instruction.argval] = self.tracked_locals.pop(
self.top_of_stack
)
Expand Down
16 changes: 16 additions & 0 deletions tests/units/vars/test_dep_tracking.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,22 @@ async def get_state_imported_global(self: DependencyTestState):
assert tracker.dependencies == expected_deps


def test_nested_function():
"""Test tracking dependencies in nested functions."""

def func_with_nested(self: DependencyTestState):
async def inner(): # noqa: RUF029
if self.board:
pass

return self.count

tracker = DependencyTracker(func_with_nested, DependencyTestState)

expected_deps = {DependencyTestState.get_full_name(): {"board", "count"}}
assert tracker.dependencies == expected_deps


@pytest.mark.skipif(
sys.version_info < (3, 11), reason="Requires Python 3.11+ for positions"
)
Expand Down
Loading