Skip to content

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.

Don't highlight keyword argument names as reactive references (#5500)

Trevor Manze2bf9d3e6b34
diff --git a/frontend/src/core/codemirror/reactive-references/__tests__/analyzer.test.ts b/frontend/src/core/codemirror/reactive-references/__tests__/analyzer.test.ts
index f159785d05..66d3575f55 100644
--- a/frontend/src/core/codemirror/reactive-references/__tests__/analyzer.test.ts
+++ b/frontend/src/core/codemirror/reactive-references/__tests__/analyzer.test.ts
@@ -982,6 +982,96 @@ def outer_func():
"
`);
});
+
+ test("should not highlight class keyword argument", () => {
+ expect(
+ runHighlight(
+ ["a", "b", "c"],
+ `
+class Bar:
+ def __init__(self, a, b):
+ self.a = a
+ self.b = b
+
+Bar(a, b=b)
+`,
+ ),
+ ).toMatchInlineSnapshot(`
+ "
+ class Bar:
+ def __init__(self, a, b):
+ self.a = a
+ self.b = b
+
+ Bar(a, b=b)
+ ^ ^
+ "
+ `);
+ });
+
+ test("should not highlight function keyword argument", () => {
+ expect(
+ runHighlight(
+ ["a", "b"],
+ `
+def foo(a, b):
+ print(a, b)
+
+foo(a, b=b)
+`,
+ ),
+ ).toMatchInlineSnapshot(`
+ "
+ def foo(a, b):
+ print(a, b)
+
+ foo(a, b=b)
+ ^ ^
+ "
+ `);
+ });
+
+ test("should not highlight function keyword argument (with whitespace)", () => {
+ expect(
+ runHighlight(
+ ["b"],
+ `
+def foo(a, b):
+ print(a, b)
+
+foo(a,
+ b= b)
+
+foo(a,
+ b = b)
+
+foo(a,
+ b =
+
+b)
+`,
+ ),
+ ).toMatchInlineSnapshot(`
+ "
+ def foo(a, b):
+ print(a, b)
+
+ foo(a,
+ b= b)
+ ^
+
+ foo(a,
+ b = b)
+ ^
+
+ foo(a,
+ b =
+
+ b)
+ ^
+ "
+ `);
+ });
});
/**
diff --git a/frontend/src/core/codemirror/reactive-references/analyzer.ts b/frontend/src/core/codemirror/reactive-references/analyzer.ts
index 81168b9923..b44ba39005 100644
--- a/frontend/src/core/codemirror/reactive-references/analyzer.ts
+++ b/frontend/src/core/codemirror/reactive-references/analyzer.ts
@@ -395,7 +395,7 @@ export function findReactiveVariables(options: {
if (nodeName === "VariableName") {
const varName = options.state.doc.sliceString(cursor.from, cursor.to);
- if (allVariableNames.has(varName)) {
+ if (allVariableNames.has(varName) && !isKeywordArgumentName(cursor)) {
let isDeclaredLocally = false;
for (const scope of currentScopeStack) {
if (allDeclarations.get(scope)?.has(varName)) {
@@ -430,6 +430,26 @@ export function findReactiveVariables(options: {
return ranges;
}
+/** Checks whether a `VariableName` is a keyword argument name. */
+function isKeywordArgumentName(cursor: TreeCursor): boolean {
+ const temp = cursor.node.cursor();
+ temp.moveTo(cursor.from);
+ if (temp.parent() && temp.name === "CallExpression" && temp.firstChild()) {
+ do {
+ // @ts-expect-error: comparing disjoint string literals is intentional due to do/while traversal
+ if (temp.name === "ArgList" && temp.firstChild()) {
+ do {
+ if (temp.from === cursor.from && temp.to === cursor.to) {
+ return temp.nextSibling() && temp.name === "AssignOp";
+ }
+ } while (temp.nextSibling());
+ break;
+ }
+ } while (temp.nextSibling());
+ }
+ return false;
+}
+
/**
* Checks if the syntax tree contains any syntax errors.
* If there are errors, we shouldn't show reactive variable highlighting.