Skip to content

Commit 1d5bd36

Browse files
Pass refkey to DataclassDeclaration
1 parent 74f5ebc commit 1d5bd36

File tree

3 files changed

+70
-3
lines changed

3 files changed

+70
-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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ export function DataclassDeclaration(props: DataclassDeclarationProps) {
176176
)
177177
</Show>
178178
<hbr />
179-
<ClassDeclaration name={props.name} bases={props.bases} doc={props.doc}>
179+
<ClassDeclaration name={props.name} bases={props.bases} doc={props.doc} refkey={props.refkey}>
180180
<StatementList>{props.children}</StatementList>
181181
{validateDataclassMemberConflicts(kwargs as DataclassDecoratorKwargs)}
182182
</ClassDeclaration>

packages/python/test/dataclassdeclarations.test.tsx

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
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 { assertFileContents, toSourceText, toSourceTextMultiple } from "./utils.jsx";
77

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

0 commit comments

Comments
 (0)