Skip to content
Merged
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
52 changes: 31 additions & 21 deletions type-generation/src/astToIR.ts
Original file line number Diff line number Diff line change
Expand Up @@ -754,9 +754,12 @@ export class Converter {
for (const param of decl.getParameters()) {
const spread = !!param.getDotDotDotToken();
const optional = !!param.hasQuestionToken();
const name = param.getName();
this.pushNameContext(name);
const type = this.typeToIR(param.getTypeNode()!, optional);
this.popNameContext();
const pyParam: ParamIR = {
name: param.getName(),
name,
type,
isOptional: optional,
};
Expand Down Expand Up @@ -896,7 +899,14 @@ export class Converter {
signatures: Signature[],
isStatic?: boolean,
): CallableIR {
const sigs = signatures.flatMap((sig) => this.sigToIRDestructure(sig));
this.pushNameContext(name);
const sigs = signatures.flatMap((sig, idx) => {
this.pushNameContext(`Sig${idx}`);
const result = this.sigToIRDestructure(sig);
this.popNameContext();
return result;
});
this.popNameContext();

const result: CallableIR = {
kind: "callable",
Expand Down Expand Up @@ -1091,25 +1101,25 @@ export class Converter {
const typeArgs = typeParams.map((param) => simpleType(param));
const concreteBases = [{ name: ifaceName, typeArgs }];
const constructors = classDecl.getConstructors();

return [
this.interfaceToIR(
ifaceName,
bases,
[...methods, ...properties],
[],
[],
typeParams,
),
this.interfaceToIR(
name,
concreteBases,
[],
[...constructors, ...staticMethods, ...staticProperties],
[],
typeParams,
),
];
this.nameContext = [name];
const ifaceIR = this.interfaceToIR(
ifaceName,
bases,
[...methods, ...properties],
[],
[],
typeParams,
);
const concreteIR = this.interfaceToIR(
name,
concreteBases,
[],
[...constructors, ...staticMethods, ...staticProperties],
[],
typeParams,
);
this.nameContext = undefined;
return [ifaceIR, concreteIR];
}

getBasesOfDecls(
Expand Down
44 changes: 44 additions & 0 deletions type-generation/tests/a.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1797,6 +1797,50 @@ describe("emit", () => {
`).trim(),
);
});
describe("type literals", () => {
it("type literal in class property", () => {
const res = emitFile(`
declare class T {
a: {x: string, y: boolean};
}
`);
assert.strictEqual(
removeTypeIgnores(res.slice(1).join("\n\n")),
dedent(`
class T_iface(Protocol):
a: T__a_iface = ...

class T(T_iface, _JsObject):
pass

class T__a_iface(Protocol):
x: str = ...
y: bool = ...
`).trim(),
);
});
it("type literal in class method signature", () => {
const res = emitFile(`
declare class T {
f(a: {x: string, y: boolean}, b: boolean): void;
}
`);
assert.strictEqual(
removeTypeIgnores(res.slice(1).join("\n\n")),
dedent(`
class T_iface(Protocol):
def f(self, a: T__f__Sig0__a_iface, b: bool, /) -> None: ...

class T(T_iface, _JsObject):
pass

class T__f__Sig0__a_iface(Protocol):
x: str = ...
y: bool = ...
`).trim(),
);
});
});
describe("adjustments", () => {
it("setTimeout", () => {
const res = emitIRNoTypeIgnores(convertBuiltinFunction("setTimeout"));
Expand Down