A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
Fix reactive highlighting of property access names (#8725)
Trevor Manzf45031095c46
diff --git a/frontend/src/core/codemirror/reactive-references/__tests__/analyzer.test.ts b/frontend/src/core/codemirror/reactive-references/__tests__/analyzer.test.tsindex bce4597af2..a3974b6e1c 100644--- a/frontend/src/core/codemirror/reactive-references/__tests__/analyzer.test.ts+++ b/frontend/src/core/codemirror/reactive-references/__tests__/analyzer.test.ts@@ -1115,6 +1115,60 @@ class Foo:`);});+ test("should not highlight attribute access matching a for-loop variable", () => {+ // When `tool` is used as a for-loop variable, `mcp.tool` (attribute access)+ // should NOT have `tool` highlighted — it's a property, not a variable reference.+ expect(+ runHighlight(+ ["mcp", "client"],+ `+def roll_dice():+ pass++async with client:+ for tool in await client.list_tools():+ print(tool)+`,+ ),+ ).toMatchInlineSnapshot(`+ "+ @mcp.tool+ ^^^+ def roll_dice():+ pass++ async with client:+ ^^^^^^+ for tool in await client.list_tools():+ ^^^^^^+ print(tool)+ "+ `);+ });++ test("should not highlight property name matching a global variable", () => {+ // `tool` is a global variable, but `mcp.tool` should not highlight `tool`+ // because it's a property access, not a variable reference.+ expect(+ runHighlight(+ ["mcp", "tool"],+ `+def roll_dice():+ pass+`,+ ),+ ).toMatchInlineSnapshot(`+ "+ @mcp.tool+ ^^^+ def roll_dice():+ pass+ "+ `);+ });+test("class property self-reference", () => {expect(runHighlight(diff --git a/frontend/src/core/codemirror/reactive-references/analyzer.ts b/frontend/src/core/codemirror/reactive-references/analyzer.tsindex 8995e3dfeb..14679806be 100644--- a/frontend/src/core/codemirror/reactive-references/analyzer.ts+++ b/frontend/src/core/codemirror/reactive-references/analyzer.ts@@ -462,6 +462,7 @@ export function findReactiveVariables(options: {): boolean {return (allVariableNames.has(varName) &&+ !isPropertyAccess(cursor) &&!isKeywordArgumentName(cursor) &&!isAssignmentTarget(cursor));@@ -529,6 +530,19 @@ function isAssignmentTarget(cursor: TreeCursor): boolean {return false;}+/** Checks whether a `VariableName` is a property access (e.g. `tool` in `mcp.tool`). */+function isPropertyAccess(cursor: TreeCursor): boolean {+ // Check if the previous sibling is a "." node, which means this+ // VariableName is a property access rather than a standalone variable.+ // This handles both MemberExpression (e.g. `obj.attr`) and Decorator+ // (e.g. `@mcp.tool`) where the parser emits flat sibling nodes.+ const temp = cursor.node.cursor();+ if (temp.prevSibling() && temp.name === ".") {+ return true;+ }+ return false;+}+/*** Checks if the syntax tree contains any syntax errors.* If there are errors, we shouldn't show reactive variable highlighting.