Skip to content

Commit

Permalink
[mypyc] Loading type from imported modules. (#18158)
Browse files Browse the repository at this point in the history
Fixes [#1075](mypyc/mypyc#1075)

Previously, this compiled but failed to run:
```python
import collections
from typing_extensions import TypedDict
class C(TypedDict):
    x: collections.deque
```
Although type of `x` was being inferred correctly, it was not looking
for `collections.deque` in imported modules.
  • Loading branch information
advait-dixit authored Nov 16, 2024
1 parent 35048bf commit 8ef2197
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
5 changes: 5 additions & 0 deletions mypyc/irbuild/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,11 @@ def load_type(builder: IRBuilder, typ: TypeInfo, line: int) -> Value:
elif typ.fullname in builtin_names:
builtin_addr_type, src = builtin_names[typ.fullname]
class_obj = builder.add(LoadAddress(builtin_addr_type, src, line))
elif typ.module_name in builder.imports:
loaded_module = builder.load_module(typ.module_name)
class_obj = builder.builder.get_attr(
loaded_module, typ.name, object_rprimitive, line, borrow=False
)
else:
class_obj = builder.load_global_str(typ.name, line)

Expand Down
13 changes: 13 additions & 0 deletions mypyc/test-data/run-classes.test
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,19 @@ assert hasattr(c, 'x')
1000000000000000000000000000001
1000000000000000000000000000002

[case testTypedDictWithFields]
import collections
from typing_extensions import TypedDict
class C(TypedDict):
x: collections.deque
[file driver.py]
from native import C
from collections import deque

print(C.__annotations__["x"] is deque)
[out]
True

[case testClassWithDeletableAttributes]
from typing import Any, cast
from testutil import assertRaises
Expand Down

0 comments on commit 8ef2197

Please sign in to comment.