Skip to content

Commit 4939b11

Browse files
authored
[mypyc] Fix crash with NewType and other non-class types in incremental builds (#19837)
Fixes mypyc/mypyc#1138. Also fix similar issue with named tuples and TypedDicts.
1 parent 83d186a commit 4939b11

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

mypyc/irbuild/prepare.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,13 @@ def load_type_map(mapper: Mapper, modules: list[MypyFile], deser_ctx: DeserMaps)
159159
"""Populate a Mapper with deserialized IR from a list of modules."""
160160
for module in modules:
161161
for node in module.names.values():
162-
if isinstance(node.node, TypeInfo) and is_from_module(node.node, module):
162+
if (
163+
isinstance(node.node, TypeInfo)
164+
and is_from_module(node.node, module)
165+
and not node.node.is_newtype
166+
and not node.node.is_named_tuple
167+
and node.node.typeddict_type is None
168+
):
163169
ir = deser_ctx.classes[node.node.fullname]
164170
mapper.type_to_ir[node.node] = ir
165171
mapper.symbol_fullnames.add(node.node.fullname)

mypyc/test-data/run-multimodule.test

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -902,3 +902,51 @@ import native
902902
[out2]
903903
0
904904
None
905+
906+
[case testIncrementalCompilationWithNonClassTypeDef]
907+
import other_a
908+
[file other_a.py]
909+
from other_b import MyInt
910+
[file other_a.py.2]
911+
from other_b import MyInt, NT, TD
912+
i = MyInt(42)
913+
914+
def f(x: MyInt) -> int:
915+
return x + 1
916+
917+
def g(x: int) -> MyInt:
918+
return MyInt(x + 2)
919+
920+
print(i)
921+
print(f(i))
922+
print(g(13))
923+
924+
def make_nt(x: int) -> NT:
925+
return NT(x=MyInt(x))
926+
927+
print(make_nt(4))
928+
929+
def make_td(x: int) -> TD:
930+
return {"x": MyInt(x)}
931+
932+
print(make_td(5))
933+
934+
[file other_b.py]
935+
from typing import NewType, NamedTuple, TypedDict
936+
from enum import Enum
937+
938+
MyInt = NewType("MyInt", int)
939+
NT = NamedTuple("NT", [("x", MyInt)])
940+
TD = TypedDict("TD", {"x": MyInt})
941+
942+
[file driver.py]
943+
import native
944+
945+
[typing fixtures/typing-full.pyi]
946+
[out]
947+
[out2]
948+
42
949+
43
950+
15
951+
NT(x=4)
952+
{'x': 5}

0 commit comments

Comments
 (0)