Skip to content

Commit a311ca6

Browse files
Pass refkey to DataclassDeclaration (#334)
`DataclassDeclaration` doesn't forward the `refkey` prop to the underlying `ClassDeclaration`, causing symbol resolution to fail when other code tries to reference the dataclass, so we are fixing that.
1 parent 74f5ebc commit a311ca6

File tree

3 files changed

+76
-3
lines changed

3 files changed

+76
-3
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
changeKind: fix
3+
packages:
4+
- "@alloy-js/python"
5+
---
6+
7+
Pass refkey to DataclassDeclaration

packages/python/src/components/DataclassDeclaration.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,12 @@ export function DataclassDeclaration(props: DataclassDeclarationProps) {
176176
)
177177
</Show>
178178
<hbr />
179-
<ClassDeclaration name={props.name} bases={props.bases} doc={props.doc}>
179+
<ClassDeclaration
180+
name={props.name}
181+
bases={props.bases}
182+
doc={props.doc}
183+
refkey={props.refkey}
184+
>
180185
<StatementList>{props.children}</StatementList>
181186
{validateDataclassMemberConflicts(kwargs as DataclassDecoratorKwargs)}
182187
</ClassDeclaration>

packages/python/test/dataclassdeclarations.test.tsx

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
import { Prose, namekey } from "@alloy-js/core";
1+
import { Prose, namekey, refkey } from "@alloy-js/core";
22
import { d } from "@alloy-js/core/testing";
33
import { describe, expect, it } from "vitest";
44
import { dataclassesModule } from "../src/builtins/python.js";
55
import * as py from "../src/index.js";
6-
import { toSourceText } from "./utils.jsx";
6+
import {
7+
assertFileContents,
8+
toSourceText,
9+
toSourceTextMultiple,
10+
} from "./utils.jsx";
711

812
describe("DataclassDeclaration", () => {
913
it("Creates a dataclass with a class doc", () => {
@@ -582,4 +586,61 @@ describe("DataclassDeclaration", () => {
582586
`,
583587
);
584588
});
589+
590+
it("Forwards refkey prop for symbol resolution in type references", () => {
591+
const userRefkey = refkey();
592+
const res = toSourceTextMultiple(
593+
[
594+
<py.SourceFile path="models.py">
595+
<py.DataclassDeclaration name="User" refkey={userRefkey}>
596+
<py.VariableDeclaration
597+
instanceVariable
598+
omitNone
599+
name="id"
600+
type="int"
601+
/>
602+
<py.VariableDeclaration
603+
instanceVariable
604+
omitNone
605+
name="name"
606+
type="str"
607+
/>
608+
</py.DataclassDeclaration>
609+
</py.SourceFile>,
610+
<py.SourceFile path="services.py">
611+
<py.FunctionDeclaration name="get_user" returnType={userRefkey}>
612+
<py.VariableDeclaration
613+
name="user"
614+
type={userRefkey}
615+
initializer={
616+
<py.ClassInstantiation target="User" args={["1", '"Alice"']} />
617+
}
618+
/>
619+
<hbr />
620+
{"return user"}
621+
</py.FunctionDeclaration>
622+
</py.SourceFile>,
623+
],
624+
{ externals: [dataclassesModule] },
625+
);
626+
assertFileContents(res, {
627+
"models.py": `
628+
from dataclasses import dataclass
629+
630+
@dataclass
631+
class User:
632+
id: int
633+
name: str
634+
635+
`,
636+
"services.py": `
637+
from models import User
638+
639+
def get_user() -> User:
640+
user: User = User(1, "Alice")
641+
return user
642+
643+
`,
644+
});
645+
});
585646
});

0 commit comments

Comments
 (0)