Skip to content

Commit 21fe5df

Browse files
authored
Don't add _iface suffixes in so many places (#76)
And drop type aliases when the RHS has the same name as the type alias.
1 parent b6851ce commit 21fe5df

File tree

2 files changed

+60
-84
lines changed

2 files changed

+60
-84
lines changed

type-generation/src/astToIR.ts

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -377,9 +377,6 @@ class SyntheticTypeConverter {
377377
const typeParams = this.converter.getTypeParamsFromDecls(res);
378378

379379
let name = this.nameContext.join("__");
380-
if (!name.endsWith("_iface")) {
381-
name += "_iface";
382-
}
383380
let members = nodes.flatMap((x) => x.getMembers());
384381
const { omitSet, partial, pickSet } = modifiers;
385382
if (omitSet) {
@@ -452,12 +449,7 @@ class SyntheticTypeConverter {
452449
return res;
453450
})
454451
.filter((x): x is ReferenceTypeIR => !!x && x.kind === "reference");
455-
for (const node of types) {
456-
if (!node.name.endsWith("_iface")) {
457-
node.name += "_iface";
458-
}
459-
}
460-
const name = this.nameContext.join("__") + "_iface";
452+
const name = this.nameContext.join("__");
461453
this.converter.extraTopLevels.push(
462454
this.converter.interfaceToIR(name, types, [], [], [], []),
463455
);
@@ -1383,21 +1375,13 @@ export class Converter {
13831375
.getTypeParameters()
13841376
.map((p) => p.getName());
13851377
const typeNode = classified.decl.getTypeNode()!;
1386-
// If the typeNode is a type literal, emit an interface instead of a
1387-
// type alias. This reduces redundancy. Also, interfaces can be used in
1388-
// bases.
1389-
if (Node.isTypeLiteral(typeNode)) {
1390-
return this.interfaceToIR(
1391-
name,
1392-
[],
1393-
typeNode.getMembers(),
1394-
[],
1395-
[],
1396-
aliasTypeParams,
1397-
);
1398-
}
13991378
const type = this.typeToIR(typeNode);
14001379
this.nameContext = undefined;
1380+
// If we just emitted a class definition with the same name, we can drop
1381+
// the type alias.
1382+
if (type.kind === "reference" && type.name === name) {
1383+
return undefined;
1384+
}
14011385
return { kind: "typeAlias", name, type, typeParams: aliasTypeParams };
14021386
case "varDecl":
14031387
console.warn("Skipping varDecl", ident.getText());

type-generation/tests/a.test.ts

Lines changed: 54 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1366,17 +1366,15 @@ describe("emit", () => {
13661366
assert.strictEqual(
13671367
removeTypeIgnores(res.slice(1).join("\n\n")),
13681368
dedent(`
1369-
type Q = Q_iface
1370-
13711369
def q() -> Q: ...
13721370
1373-
class Q__Intersection0_iface(Protocol):
1371+
class Q__Intersection0(Protocol):
13741372
a: str = ...
13751373
1376-
class Q__Intersection1_iface(Protocol):
1374+
class Q__Intersection1(Protocol):
13771375
def f(self, /, *, x: str | int | float) -> None: ...
13781376
1379-
class Q_iface(Q__Intersection1_iface, Q__Intersection0_iface, Protocol):
1377+
class Q(Q__Intersection1, Q__Intersection0, Protocol):
13801378
pass
13811379
`).trim(),
13821380
);
@@ -1412,14 +1410,14 @@ describe("emit", () => {
14121410
assert.strictEqual(
14131411
removeTypeIgnores(res.slice(1).join("\n\n")),
14141412
dedent(`
1415-
type A = A__Union0_iface | A__Union1_iface
1413+
type A = A__Union0 | A__Union1
14161414
14171415
def f() -> A: ...
14181416
1419-
class A__Union0_iface(Protocol):
1417+
class A__Union0(Protocol):
14201418
a: int | float = ...
14211419
1422-
class A__Union1_iface(Protocol):
1420+
class A__Union1(Protocol):
14231421
b: str = ...
14241422
`).trim(),
14251423
);
@@ -1434,12 +1432,12 @@ describe("emit", () => {
14341432
dedent(`
14351433
def f() -> A: ...
14361434
1435+
class A__b(Protocol):
1436+
c: int | float = ...
1437+
14371438
class A(Protocol):
14381439
a: int | float = ...
1439-
b: A__b_iface = ...
1440-
1441-
class A__b_iface(Protocol):
1442-
c: int | float = ...
1440+
b: A__b = ...
14431441
14441442
`).trim(),
14451443
);
@@ -1452,17 +1450,15 @@ describe("emit", () => {
14521450
assert.strictEqual(
14531451
removeTypeIgnores(res.slice(1).join("\n\n")),
14541452
dedent(`
1455-
type A = A_iface
1456-
14571453
def f() -> A: ...
14581454
1459-
class A__Intersection0_iface(Protocol):
1455+
class A__Intersection0(Protocol):
14601456
a: int | float = ...
14611457
1462-
class A__Intersection1_iface(Protocol):
1458+
class A__Intersection1(Protocol):
14631459
b: str = ...
14641460
1465-
class A_iface(A__Intersection1_iface, A__Intersection0_iface, Protocol):
1461+
class A(A__Intersection1, A__Intersection0, Protocol):
14661462
pass
14671463
`).trim(),
14681464
);
@@ -1480,18 +1476,16 @@ describe("emit", () => {
14801476
assert.strictEqual(
14811477
removeTypeIgnores(res.slice(1).join("\n\n")),
14821478
dedent(`
1483-
type D = D_iface
1484-
14851479
def f() -> D: ...
14861480
1487-
class D__Intersection0_iface(Protocol):
1481+
class D__Intersection0(Protocol):
14881482
a: str = ...
14891483
1490-
class D__Intersection1_iface(Protocol):
1484+
class D__Intersection1(Protocol):
14911485
@property
14921486
def id(self, /) -> int | float: ...
14931487
1494-
class D_iface(D__Intersection1_iface, D__Intersection0_iface, Protocol):
1488+
class D(D__Intersection1, D__Intersection0, Protocol):
14951489
pass
14961490
`).trim(),
14971491
);
@@ -1510,17 +1504,15 @@ describe("emit", () => {
15101504
assert.strictEqual(
15111505
removeTypeIgnores(res.slice(1).join("\n\n")),
15121506
dedent(`
1513-
type D = D_iface
1514-
15151507
def f() -> D: ...
15161508
15171509
class I_iface[T](Protocol):
15181510
x: T = ...
15191511
1520-
class D__Intersection1_iface(Protocol):
1512+
class D__Intersection1(Protocol):
15211513
id: int | float = ...
15221514
1523-
class D_iface(D__Intersection1_iface, I_iface[str], Protocol):
1515+
class D(D__Intersection1, I_iface[str], Protocol):
15241516
pass
15251517
`).trim(),
15261518
);
@@ -1536,11 +1528,11 @@ describe("emit", () => {
15361528
assert.strictEqual(
15371529
removeTypeIgnores(res.slice(1).join("\n\n")),
15381530
dedent(`
1539-
type D = D__Omit_iface
1531+
type D = D__Omit
15401532
15411533
def f() -> D: ...
15421534
1543-
class D__Omit_iface(Protocol):
1535+
class D__Omit(Protocol):
15441536
a: str = ...
15451537
`).trim(),
15461538
);
@@ -1553,11 +1545,11 @@ describe("emit", () => {
15531545
assert.strictEqual(
15541546
removeTypeIgnores(res.slice(1).join("\n\n")),
15551547
dedent(`
1556-
type D = D__Omit_iface
1548+
type D = D__Omit
15571549
15581550
def f() -> D: ...
15591551
1560-
class D__Omit_iface(Protocol):
1552+
class D__Omit(Protocol):
15611553
a: str = ...
15621554
`).trim(),
15631555
);
@@ -1571,17 +1563,17 @@ describe("emit", () => {
15711563
assert.strictEqual(
15721564
removeTypeIgnores(res.slice(1).join("\n\n")),
15731565
dedent(`
1574-
type D = D__Omit_iface
1566+
type D = D__Omit
15751567
15761568
def f() -> D: ...
15771569
1578-
class D__Omit__Intersection0_iface(Protocol):
1570+
class D__Omit__Intersection0(Protocol):
15791571
a: str = ...
15801572
1581-
class D__Omit__Intersection1_iface(Protocol):
1573+
class D__Omit__Intersection1(Protocol):
15821574
c: str = ...
15831575
1584-
class D__Omit_iface(D__Omit__Intersection1_iface, D__Omit__Intersection0_iface, Protocol):
1576+
class D__Omit(D__Omit__Intersection1, D__Omit__Intersection0, Protocol):
15851577
pass
15861578
`).trim(),
15871579
);
@@ -1616,11 +1608,11 @@ describe("emit", () => {
16161608
assert.strictEqual(
16171609
removeTypeIgnores(res.slice(1).join("\n\n")),
16181610
dedent(`
1619-
type B = B__Omit_iface
1611+
type B = B__Omit
16201612
16211613
def f() -> B: ...
16221614
1623-
class B__Omit_iface(Protocol):
1615+
class B__Omit(Protocol):
16241616
t: str | None = ...
16251617
`).trim(),
16261618
);
@@ -1657,11 +1649,11 @@ describe("emit", () => {
16571649
assert.strictEqual(
16581650
removeTypeIgnores(res.slice(1).join("\n\n")),
16591651
dedent(`
1660-
type D = D__Pick_iface
1652+
type D = D__Pick
16611653
16621654
def f() -> D: ...
16631655
1664-
class D__Pick_iface(Protocol):
1656+
class D__Pick(Protocol):
16651657
b: int | float = ...
16661658
`).trim(),
16671659
);
@@ -1674,11 +1666,11 @@ describe("emit", () => {
16741666
assert.strictEqual(
16751667
removeTypeIgnores(res.slice(1).join("\n\n")),
16761668
dedent(`
1677-
type D = D__Pick_iface
1669+
type D = D__Pick
16781670
16791671
def f() -> D: ...
16801672
1681-
class D__Pick_iface(Protocol):
1673+
class D__Pick(Protocol):
16821674
b: str = ...
16831675
c: int | float = ...
16841676
`).trim(),
@@ -1692,17 +1684,17 @@ describe("emit", () => {
16921684
assert.strictEqual(
16931685
removeTypeIgnores(res.slice(1).join("\n\n")),
16941686
dedent(`
1695-
type D = D__Pick_iface
1687+
type D = D__Pick
16961688
16971689
def f() -> D: ...
16981690
1699-
class D__Pick__Intersection0_iface(Protocol):
1691+
class D__Pick__Intersection0(Protocol):
17001692
b: str = ...
17011693
1702-
class D__Pick__Intersection1_iface(Protocol):
1694+
class D__Pick__Intersection1(Protocol):
17031695
pass
17041696
1705-
class D__Pick_iface(D__Pick__Intersection1_iface, D__Pick__Intersection0_iface, Protocol):
1697+
class D__Pick(D__Pick__Intersection1, D__Pick__Intersection0, Protocol):
17061698
pass
17071699
`).trim(),
17081700
);
@@ -1737,11 +1729,11 @@ describe("emit", () => {
17371729
assert.strictEqual(
17381730
removeTypeIgnores(res.slice(1).join("\n\n")),
17391731
dedent(`
1740-
type B = B__Pick_iface
1732+
type B = B__Pick
17411733
17421734
def f() -> B: ...
17431735
1744-
class B__Pick_iface(Protocol):
1736+
class B__Pick(Protocol):
17451737
s: bool | None = ...
17461738
`).trim(),
17471739
);
@@ -1760,11 +1752,11 @@ describe("emit", () => {
17601752
assert.strictEqual(
17611753
removeTypeIgnores(res.slice(1).join("\n\n")),
17621754
dedent(`
1763-
type B = B__Partial_iface
1755+
type B = B__Partial
17641756
17651757
def f() -> B: ...
17661758
1767-
class B__Partial_iface(Protocol):
1759+
class B__Partial(Protocol):
17681760
s: bool | None = ...
17691761
t: str | None = ...
17701762
`).trim(),
@@ -1782,11 +1774,11 @@ describe("emit", () => {
17821774
assert.strictEqual(
17831775
removeTypeIgnores(res.slice(1).join("\n\n")),
17841776
dedent(`
1785-
type B = B__Partial_iface
1777+
type B = B__Partial
17861778
17871779
def f() -> B: ...
17881780
1789-
class B__Partial_iface(Protocol):
1781+
class B__Partial(Protocol):
17901782
s: bool | None = ...
17911783
t: str | None = ...
17921784
`).trim(),
@@ -1874,9 +1866,9 @@ describe("emit", () => {
18741866
assert.strictEqual(
18751867
removeTypeIgnores(res.slice(1).join("\n\n")),
18761868
dedent(`
1877-
def f(a: f__Sig0__a_iface, b: bool, /) -> None: ...
1869+
def f(a: f__Sig0__a, b: bool, /) -> None: ...
18781870
1879-
class f__Sig0__a_iface(Protocol):
1871+
class f__Sig0__a(Protocol):
18801872
x: str = ...
18811873
y: bool = ...
18821874
`).trim(),
@@ -1892,12 +1884,12 @@ describe("emit", () => {
18921884
removeTypeIgnores(res.slice(1).join("\n\n")),
18931885
dedent(`
18941886
class T_iface(Protocol):
1895-
a: T__a_iface = ...
1887+
a: T__a = ...
18961888
18971889
class T(T_iface, _JsObject):
18981890
pass
18991891
1900-
class T__a_iface(Protocol):
1892+
class T__a(Protocol):
19011893
x: str = ...
19021894
y: bool = ...
19031895
`).trim(),
@@ -1913,12 +1905,12 @@ describe("emit", () => {
19131905
removeTypeIgnores(res.slice(1).join("\n\n")),
19141906
dedent(`
19151907
class T_iface(Protocol):
1916-
def f(self, a: T__f__Sig0__a_iface, b: bool, /) -> None: ...
1908+
def f(self, a: T__f__Sig0__a, b: bool, /) -> None: ...
19171909
19181910
class T(T_iface, _JsObject):
19191911
pass
19201912
1921-
class T__f__Sig0__a_iface(Protocol):
1913+
class T__f__Sig0__a(Protocol):
19221914
x: str = ...
19231915
y: bool = ...
19241916
`).trim(),
@@ -1937,9 +1929,9 @@ describe("emit", () => {
19371929
def f() -> O_iface[str]: ...
19381930
19391931
class O_iface(Protocol):
1940-
x: O_iface__x_iface | None = ...
1932+
x: O_iface__x | None = ...
19411933
1942-
class O_iface__x_iface(Protocol):
1934+
class O_iface__x(Protocol):
19431935
a: str = ...
19441936
`).trim(),
19451937
);
@@ -1958,15 +1950,15 @@ describe("emit", () => {
19581950
def f(options: O_iface[str], /) -> None: ...
19591951
19601952
@overload
1961-
def f(*, x: f__Sig0_iface | None = None) -> None: ...
1953+
def f(*, x: f__Sig0 | None = None) -> None: ...
19621954
19631955
class O_iface[T](Protocol):
1964-
x: O_iface__x_iface | None = ...
1956+
x: O_iface__x | None = ...
19651957
1966-
class f__Sig0_iface(Protocol):
1958+
class f__Sig0(Protocol):
19671959
a: str = ...
19681960
1969-
class O_iface__x_iface(Protocol):
1961+
class O_iface__x(Protocol):
19701962
a: T = ...
19711963
`).trim(),
19721964
);

0 commit comments

Comments
 (0)