Skip to content

Commit 7811f08

Browse files
committed
[mypyc] Add LoadAddress op for PySet_Type & PyFrozenSet_Type
This also fixes mypyc/mypyc#917 RE above, the root issue is that mypyc didn't know builtins.set was a built-in name, so it guessed it comes from the module globals. This didn't blow up anything up somehow... until the dataclasses commit[^1] which made the `__annotations__` logic for dataclasses try to better preserve the type annotations (previously they would be erased to builtins.type). This new logic would use `load_type` to load `builtins.set` (so it can be put in `__annotations__`) whick went poorly as only types registered with `load_address_op` are considered built-ins. [^1]: 1bcfc04
1 parent 914297e commit 7811f08

File tree

4 files changed

+32
-4
lines changed

4 files changed

+32
-4
lines changed

mypyc/primitives/set_ops.py

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,27 @@
11
"""Primitive set (and frozenset) ops."""
22

3-
from mypyc.primitives.registry import function_op, method_op, binary_op, ERR_NEG_INT
3+
from mypyc.primitives.registry import (
4+
load_address_op, function_op, method_op, binary_op, ERR_NEG_INT
5+
)
46
from mypyc.ir.ops import ERR_MAGIC, ERR_FALSE
57
from mypyc.ir.rtypes import (
68
object_rprimitive, bool_rprimitive, set_rprimitive, c_int_rprimitive, pointer_rprimitive,
79
bit_rprimitive
810
)
911

1012

13+
# Get the 'builtins.set' type object.
14+
load_address_op(
15+
name='builtins.set',
16+
type=object_rprimitive,
17+
src='PySet_Type')
18+
19+
# Get the 'builtins.frozenset' tyoe object.
20+
load_address_op(
21+
name='builtins.frozenset',
22+
type=object_rprimitive,
23+
src='PyFrozenSet_Type')
24+
1125
# Construct an empty set.
1226
new_set_op = function_op(
1327
name='builtins.set',

mypyc/test-data/fixtures/ir.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
from typing import (
55
TypeVar, Generic, List, Iterator, Iterable, Dict, Optional, Tuple, Any, Set,
6-
overload, Mapping, Union, Callable, Sequence,
6+
overload, Mapping, Union, Callable, Sequence, FrozenSet
77
)
88

99
T = TypeVar('T')
@@ -213,6 +213,12 @@ def pop(self) -> T: pass
213213
def update(self, x: Iterable[S]) -> None: pass
214214
def __or__(self, s: Set[S]) -> Set[Union[T, S]]: ...
215215

216+
class frozenset(Generic[T]):
217+
def __init__(self, i: Optional[Iterable[T]] = None) -> None: pass
218+
def __iter__(self) -> Iterator[T]: pass
219+
def __len__(self) -> int: pass
220+
def __or__(self, s: FrozenSet[S]) -> FrozenSet[Union[T, S]]: ...
221+
216222
class slice: pass
217223

218224
class range(Iterable[int]):

mypyc/test-data/fixtures/typing-full.pyi

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ TypeVar = 0
1919
Generic = 0
2020
Protocol = 0
2121
Tuple = 0
22+
Set = 0
23+
FrozenSet = 0
2224
Callable = 0
2325
_promote = 0
2426
NamedTuple = 0

mypyc/test-data/run-python37.test

+8-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
[case testRunDataclass]
44
import dataclasses
55
from dataclasses import dataclass, field
6-
from typing import Set, List, Callable, Any
6+
from typing import Set, FrozenSet, List, Callable, Any
77

88
@dataclass
99
class Person1:
@@ -68,8 +68,13 @@ class Person4:
6868
def name(self) -> str:
6969
return self._name
7070

71+
@dataclass
72+
class Person5:
73+
friends: Set[str] = field(default_factory=set)
74+
parents: FrozenSet[str] = frozenset()
75+
7176
[file other.py]
72-
from native import Person1, Person1b, Person2, Person3, Person4, testBool
77+
from native import Person1, Person1b, Person2, Person3, Person4, Person5, testBool
7378
i1 = Person1(age = 5, name = 'robot')
7479
assert i1.age == 5
7580
assert i1.name == 'robot'
@@ -117,6 +122,7 @@ assert i8 > i9
117122

118123
assert Person1.__annotations__ == {'age': int, 'name': str}
119124
assert Person2.__annotations__ == {'age': int, 'name': str}
125+
assert Person5.__annotations__ == {'friends': set, 'parents': frozenset}
120126

121127
[file driver.py]
122128
import sys

0 commit comments

Comments
 (0)