Skip to content
Closed
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
45 changes: 34 additions & 11 deletions packages/python/src/components/VariableDeclaration.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
createSymbolSlot,
memo,
} from "@alloy-js/core";
import { usePythonNamePolicy } from "../name-policy.js";
import { createPythonSymbol } from "../symbol-creation.js";
import { Atom } from "./Atom.jsx";
import { BaseDeclarationProps } from "./Declaration.jsx";
Expand Down Expand Up @@ -74,17 +75,20 @@ export function VariableDeclaration(props: VariableDeclarationProps) {
const TypeSymbolSlot = createSymbolSlot();
const ValueTypeSymbolSlot = createSymbolSlot();

const sym = createPythonSymbol(
props.name,
{
instance: props.instanceVariable,
refkeys: props.refkey,
type: props.type ? TypeSymbolSlot.firstSymbol : undefined,
},
"variable",
);
// For callStatementVar, don't create a symbol (keyword arguments aren't variables)
const sym = props.callStatementVar
? undefined
: createPythonSymbol(
props.name,
{
instance: props.instanceVariable,
refkeys: props.refkey,
type: props.type ? TypeSymbolSlot.firstSymbol : undefined,
},
"variable",
);

if (!props.type) {
if (!props.type && sym) {
ValueTypeSymbolSlot.moveMembersTo(sym);
}

Expand Down Expand Up @@ -133,13 +137,32 @@ export function VariableDeclaration(props: VariableDeclarationProps) {
];
};
const [renderRightSideOperator, rightSide] = getRightSide();

// For callStatementVar, render without symbol registration
// We need to manually apply the name policy to the name
// since that is normally handled by the symbol creation.
if (props.callStatementVar) {
const namePolicy = usePythonNamePolicy();
const name =
typeof props.name === "string" && props.name
? namePolicy.getName(props.name, "variable")
: "";
return (
<>
{name}
{renderRightSideOperator && assignmentOperator}
{rightSide}
</>
);
}

return (
<>
<Show when={Boolean(props.doc)}>
<SimpleCommentBlock children={props.doc} />
<hbr />
</Show>
<CoreDeclaration symbol={sym}>
<CoreDeclaration symbol={sym!}>
{<Name />}
{type}
{renderRightSideOperator && assignmentOperator}
Expand Down
59 changes: 59 additions & 0 deletions packages/python/test/functioncallexpressions.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,63 @@ describe("FunctionCallExpression", () => {
`;
expect(result).toRenderTo(expected);
});

it("keyword argument name does not conflict with same-named variable", () => {
const resKey = refkey();
const kwargsKey = refkey();
const result = toSourceText([
<py.StatementList>
<py.VariableDeclaration name="kwargs" refkey={kwargsKey} initializer={<py.Atom jsValue={{}} />} />
<py.VariableDeclaration
name="res"
refkey={resKey}
initializer={
<py.FunctionCallExpression
target="resolve_with_adapter"
args={[
"obj",
"info",
<py.VariableDeclaration
name="operation"
initializer={<>Operation.QUERY</>}
callStatementVar
/>,
<py.VariableDeclaration
name="data"
initializer={kwargsKey}
callStatementVar
/>,
<py.VariableDeclaration
name="is_v5"
initializer={true}
callStatementVar
/>,
]}
/>
}
/>
<py.VariableDeclaration
name="data"
initializer={
<py.MemberExpression>
<py.MemberExpression.Part refkey={resKey} />
<py.MemberExpression.Part key={"data"} />
</py.MemberExpression>
}
/>
</py.StatementList>,
]);
const expected = d`
kwargs = {}
res = resolve_with_adapter(
obj,
info,
operation=Operation.QUERY,
data=kwargs,
is_v5=True
)
data = res["data"]
`;
expect(result).toRenderTo(expected);
});
});
Loading