Skip to content

Commit 2175b3b

Browse files
authored
DependencyTracker: only handle STORE_FAST for the GETTING_IMPORT status (#6058)
STORE_FAST handling is not generally correct outside of the specific case of dealing with imports. So avoid resetting the stack or other issues when we're not importing.
1 parent 488a388 commit 2175b3b

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

reflex/vars/dep_tracking.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class ScanStatus(enum.Enum):
4646
GETTING_STATE = enum.auto()
4747
GETTING_STATE_POST_AWAIT = enum.auto()
4848
GETTING_VAR = enum.auto()
49+
GETTING_IMPORT = enum.auto()
4950

5051

5152
class UntrackedLocalVarError(VarValueError):
@@ -446,6 +447,7 @@ def _populate_dependencies(self) -> None:
446447
)
447448
)
448449
elif instruction.opname == "IMPORT_NAME" and instruction.argval is not None:
450+
self.scan_status = ScanStatus.GETTING_IMPORT
449451
self._last_import_name = instruction.argval
450452
importlib.import_module(instruction.argval)
451453
top_module_name = instruction.argval.split(".")[0]
@@ -472,7 +474,11 @@ def _populate_dependencies(self) -> None:
472474
)
473475
# If we see a STORE_FAST, we can assign the top of stack to an aliased name.
474476
self.top_of_stack = instruction.argval
475-
elif instruction.opname == "STORE_FAST" and self.top_of_stack is not None:
477+
elif (
478+
self.scan_status == ScanStatus.GETTING_IMPORT
479+
and instruction.opname == "STORE_FAST"
480+
and self.top_of_stack is not None
481+
):
476482
self.tracked_locals[instruction.argval] = self.tracked_locals.pop(
477483
self.top_of_stack
478484
)

tests/units/vars/test_dep_tracking.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,22 @@ async def get_state_imported_global(self: DependencyTestState):
271271
assert tracker.dependencies == expected_deps
272272

273273

274+
def test_nested_function():
275+
"""Test tracking dependencies in nested functions."""
276+
277+
def func_with_nested(self: DependencyTestState):
278+
async def inner(): # noqa: RUF029
279+
if self.board:
280+
pass
281+
282+
return self.count
283+
284+
tracker = DependencyTracker(func_with_nested, DependencyTestState)
285+
286+
expected_deps = {DependencyTestState.get_full_name(): {"board", "count"}}
287+
assert tracker.dependencies == expected_deps
288+
289+
274290
@pytest.mark.skipif(
275291
sys.version_info < (3, 11), reason="Requires Python 3.11+ for positions"
276292
)

0 commit comments

Comments
 (0)