diff --git a/mypyc/primitives/set_ops.py b/mypyc/primitives/set_ops.py index 70d59d749070..5d18e45ad528 100644 --- a/mypyc/primitives/set_ops.py +++ b/mypyc/primitives/set_ops.py @@ -1,6 +1,8 @@ """Primitive set (and frozenset) ops.""" -from mypyc.primitives.registry import function_op, method_op, binary_op, ERR_NEG_INT +from mypyc.primitives.registry import ( + load_address_op, function_op, method_op, binary_op, ERR_NEG_INT +) from mypyc.ir.ops import ERR_MAGIC, ERR_FALSE from mypyc.ir.rtypes import ( object_rprimitive, bool_rprimitive, set_rprimitive, c_int_rprimitive, pointer_rprimitive, @@ -8,6 +10,18 @@ ) +# Get the 'builtins.set' type object. +load_address_op( + name='builtins.set', + type=object_rprimitive, + src='PySet_Type') + +# Get the 'builtins.frozenset' tyoe object. +load_address_op( + name='builtins.frozenset', + type=object_rprimitive, + src='PyFrozenSet_Type') + # Construct an empty set. new_set_op = function_op( name='builtins.set', diff --git a/mypyc/test-data/fixtures/ir.py b/mypyc/test-data/fixtures/ir.py index a6914ccc36e5..d8c4333cafad 100644 --- a/mypyc/test-data/fixtures/ir.py +++ b/mypyc/test-data/fixtures/ir.py @@ -3,7 +3,7 @@ from typing import ( TypeVar, Generic, List, Iterator, Iterable, Dict, Optional, Tuple, Any, Set, - overload, Mapping, Union, Callable, Sequence, + overload, Mapping, Union, Callable, Sequence, FrozenSet ) T = TypeVar('T') @@ -211,7 +211,13 @@ def discard(self, x: T) -> None: pass def clear(self) -> None: pass def pop(self) -> T: pass def update(self, x: Iterable[S]) -> None: pass - def __or__(self, s: Set[S]) -> Set[Union[T, S]]: ... + def __or__(self, s: Union[Set[S], FrozenSet[S]]) -> Set[Union[T, S]]: ... + +class frozenset(Generic[T]): + def __init__(self, i: Optional[Iterable[T]] = None) -> None: pass + def __iter__(self) -> Iterator[T]: pass + def __len__(self) -> int: pass + def __or__(self, s: Union[Set[S], FrozenSet[S]]) -> FrozenSet[Union[T, S]]: ... class slice: pass diff --git a/mypyc/test-data/run-python37.test b/mypyc/test-data/run-python37.test index 734e116c1335..5bf2c29263e1 100644 --- a/mypyc/test-data/run-python37.test +++ b/mypyc/test-data/run-python37.test @@ -3,7 +3,7 @@ [case testRunDataclass] import dataclasses from dataclasses import dataclass, field -from typing import Set, List, Callable, Any +from typing import Set, FrozenSet, List, Callable, Any @dataclass class Person1: @@ -68,8 +68,13 @@ class Person4: def name(self) -> str: return self._name +@dataclass +class Person5: + friends: Set[str] = field(default_factory=set) + parents: FrozenSet[str] = frozenset() + [file other.py] -from native import Person1, Person1b, Person2, Person3, Person4, testBool +from native import Person1, Person1b, Person2, Person3, Person4, Person5, testBool i1 = Person1(age = 5, name = 'robot') assert i1.age == 5 assert i1.name == 'robot' @@ -117,6 +122,7 @@ assert i8 > i9 assert Person1.__annotations__ == {'age': int, 'name': str} assert Person2.__annotations__ == {'age': int, 'name': str} +assert Person5.__annotations__ == {'friends': set, 'parents': frozenset} [file driver.py] import sys