From 0406c4c56d7fbfe8248d088165c6ee5f51ff8151 Mon Sep 17 00:00:00 2001 From: Richard Si <63936253+ichard26@users.noreply.github.com> Date: Tue, 5 Jul 2022 22:55:50 -0400 Subject: [PATCH 1/2] [mypyc] Add LoadAddress op for PyFloat_Type & PyTuple_Type - Fixes https://github.com/mypyc/mypyc/issues/924 - Fixes https://github.com/mypyc/mypyc/issues/926 - Fixes https://github.com/mypyc/mypyc/issues/935 This is a follow-up of commit 7811f085d0081d48302237faa20254c0fc3eb43a. --- mypyc/primitives/float_ops.py | 10 ++++- mypyc/primitives/tuple_ops.py | 7 ++- mypyc/test-data/irbuild-dunders.test | 11 ++--- mypyc/test-data/run-python37.test | 4 +- mypyc/test-data/run-tuples.test | 67 ++++++++++++++++++++++++++++ 5 files changed, 88 insertions(+), 11 deletions(-) diff --git a/mypyc/primitives/float_ops.py b/mypyc/primitives/float_ops.py index ad028a901222..3359cf6fe122 100644 --- a/mypyc/primitives/float_ops.py +++ b/mypyc/primitives/float_ops.py @@ -2,12 +2,18 @@ from mypyc.ir.ops import ERR_MAGIC from mypyc.ir.rtypes import ( - str_rprimitive, float_rprimitive + str_rprimitive, float_rprimitive, object_rprimitive ) from mypyc.primitives.registry import ( - function_op + load_address_op, function_op ) +# Get the 'builtins.float' type object. +load_address_op( + name='builtins.float', + type=object_rprimitive, + src='PyFloat_Type') + # float(str) function_op( name='builtins.float', diff --git a/mypyc/primitives/tuple_ops.py b/mypyc/primitives/tuple_ops.py index ce88a4ee0f4d..33f8e331b56d 100644 --- a/mypyc/primitives/tuple_ops.py +++ b/mypyc/primitives/tuple_ops.py @@ -9,8 +9,13 @@ tuple_rprimitive, int_rprimitive, list_rprimitive, object_rprimitive, c_pyssize_t_rprimitive, bit_rprimitive ) -from mypyc.primitives.registry import method_op, function_op, custom_op +from mypyc.primitives.registry import load_address_op, method_op, function_op, custom_op +# Get the 'builtins.tuple' type object. +load_address_op( + name='builtins.tuple', + type=object_rprimitive, + src='PyTuple_Type') # tuple[index] (for an int index) tuple_get_item_op = method_op( diff --git a/mypyc/test-data/irbuild-dunders.test b/mypyc/test-data/irbuild-dunders.test index 808617e43889..d06a570aa7b0 100644 --- a/mypyc/test-data/irbuild-dunders.test +++ b/mypyc/test-data/irbuild-dunders.test @@ -175,16 +175,13 @@ L0: def f(c): c :: __main__.C r0, r1 :: int - r2, r3, r4 :: object - r5 :: str - r6, r7 :: object + r2, r3, r4, r5 :: object L0: r0 = c.__neg__() r1 = c.__invert__() r2 = load_address PyLong_Type r3 = PyObject_CallFunctionObjArgs(r2, c, 0) - r4 = builtins :: module - r5 = 'float' - r6 = CPyObject_GetAttr(r4, r5) - r7 = PyObject_CallFunctionObjArgs(r6, c, 0) + r4 = load_address PyFloat_Type + r5 = PyObject_CallFunctionObjArgs(r4, c, 0) return 1 + diff --git a/mypyc/test-data/run-python37.test b/mypyc/test-data/run-python37.test index 5bf2c29263e1..61d428c17a44 100644 --- a/mypyc/test-data/run-python37.test +++ b/mypyc/test-data/run-python37.test @@ -70,6 +70,7 @@ class Person4: @dataclass class Person5: + weight: float friends: Set[str] = field(default_factory=set) parents: FrozenSet[str] = frozenset() @@ -122,7 +123,8 @@ assert i8 > i9 assert Person1.__annotations__ == {'age': int, 'name': str} assert Person2.__annotations__ == {'age': int, 'name': str} -assert Person5.__annotations__ == {'friends': set, 'parents': frozenset} +assert Person5.__annotations__ == {'weight': float, 'friends': set, + 'parents': frozenset} [file driver.py] import sys diff --git a/mypyc/test-data/run-tuples.test b/mypyc/test-data/run-tuples.test index 759177342fa9..a8e56c62c6b1 100644 --- a/mypyc/test-data/run-tuples.test +++ b/mypyc/test-data/run-tuples.test @@ -95,6 +95,73 @@ class Sub(NT): pass assert f(Sub(3, 2)) == 3 +-- Ref: https://github.com/mypyc/mypyc/issues/924 +[case testNamedTupleClassSyntax] +from typing import Dict, List, NamedTuple, Optional, Tuple, Union + +class FileData(NamedTuple): + st_mtime: float + st_size: int + hash: str + +class SearchPaths(NamedTuple): + python_path: Tuple[str, ...] + mypy_path: Tuple[str, ...] + package_path: Tuple[str, ...] + typeshed_path: Tuple[str, ...] + +class ClassIR: pass + +class FuncIR: pass + +class VTableMethod(NamedTuple): + cls: 'ClassIR' + name: str + method: FuncIR + shadow_method: Optional[FuncIR] + +class DeserMaps(NamedTuple): + classes: Dict[str, 'ClassIR'] + functions: Dict[str, 'FuncIR'] + +StealsDescription = Union[bool, List[bool]] + +class RType: pass + +class CFunctionDescription(NamedTuple): + name: str + arg_types: List[RType] + return_type: RType + var_arg_type: Optional[RType] + truncated_type: Optional[RType] + c_function_name: str + error_kind: int + steals: StealsDescription + is_borrowed: bool + ordering: Optional[List[int]] + extra_int_constants: List[Tuple[int, RType]] + priority: int + +class IntComparisonOpDescription(NamedTuple): + binary_op_variant: int + c_func_description: CFunctionDescription + c_func_negated: bool + c_func_swap_operands: bool + +class LoadAddressDescription(NamedTuple): + name: str + type: RType + src: str + +[file driver.py] +from native import FileData, SearchPaths, DeserMaps, LoadAddressDescription, RType + +assert FileData.__annotations__ == {'st_mtime': float, 'st_size': int, 'hash': str} +assert SearchPaths.__annotations__ == {'python_path': tuple, 'mypy_path': tuple, + 'package_path': tuple, 'typeshed_path': tuple} +assert DeserMaps.__annotations__ == {'classes': dict, 'functions': dict} +assert LoadAddressDescription.__annotations__ == {'name': str, 'type': RType, 'src': str} + [case testTupleOps] from typing import Tuple, List, Any, Optional from typing_extensions import Final From dc903f1b9854c9c46279a609f36629de1cf21389 Mon Sep 17 00:00:00 2001 From: Richard Si <63936253+ichard26@users.noreply.github.com> Date: Wed, 6 Jul 2022 13:26:54 -0400 Subject: [PATCH 2/2] Rewrite test to be much more concise --- mypyc/test-data/run-tuples.test | 78 ++++++++++++--------------------- 1 file changed, 27 insertions(+), 51 deletions(-) diff --git a/mypyc/test-data/run-tuples.test b/mypyc/test-data/run-tuples.test index a8e56c62c6b1..26b039320844 100644 --- a/mypyc/test-data/run-tuples.test +++ b/mypyc/test-data/run-tuples.test @@ -99,68 +99,44 @@ assert f(Sub(3, 2)) == 3 [case testNamedTupleClassSyntax] from typing import Dict, List, NamedTuple, Optional, Tuple, Union -class FileData(NamedTuple): - st_mtime: float - st_size: int - hash: str - -class SearchPaths(NamedTuple): - python_path: Tuple[str, ...] - mypy_path: Tuple[str, ...] - package_path: Tuple[str, ...] - typeshed_path: Tuple[str, ...] - class ClassIR: pass class FuncIR: pass -class VTableMethod(NamedTuple): - cls: 'ClassIR' - name: str +StealsDescription = Union[bool, List[bool]] + +class Record(NamedTuple): + st_mtime: float + st_size: int + is_borrowed: bool + hash: str + python_path: Tuple[str, ...] + type: 'ClassIR' method: FuncIR shadow_method: Optional[FuncIR] - -class DeserMaps(NamedTuple): classes: Dict[str, 'ClassIR'] - functions: Dict[str, 'FuncIR'] - -StealsDescription = Union[bool, List[bool]] - -class RType: pass - -class CFunctionDescription(NamedTuple): - name: str - arg_types: List[RType] - return_type: RType - var_arg_type: Optional[RType] - truncated_type: Optional[RType] - c_function_name: str - error_kind: int steals: StealsDescription - is_borrowed: bool ordering: Optional[List[int]] - extra_int_constants: List[Tuple[int, RType]] - priority: int - -class IntComparisonOpDescription(NamedTuple): - binary_op_variant: int - c_func_description: CFunctionDescription - c_func_negated: bool - c_func_swap_operands: bool - -class LoadAddressDescription(NamedTuple): - name: str - type: RType - src: str + extra_int_constants: List[Tuple[int]] [file driver.py] -from native import FileData, SearchPaths, DeserMaps, LoadAddressDescription, RType - -assert FileData.__annotations__ == {'st_mtime': float, 'st_size': int, 'hash': str} -assert SearchPaths.__annotations__ == {'python_path': tuple, 'mypy_path': tuple, - 'package_path': tuple, 'typeshed_path': tuple} -assert DeserMaps.__annotations__ == {'classes': dict, 'functions': dict} -assert LoadAddressDescription.__annotations__ == {'name': str, 'type': RType, 'src': str} +from typing import Optional +from native import ClassIR, FuncIR, Record + +assert Record.__annotations__ == { + 'st_mtime': float, + 'st_size': int, + 'is_borrowed': bool, + 'hash': str, + 'python_path': tuple, + 'type': ClassIR, + 'method': FuncIR, + 'shadow_method': type, + 'classes': dict, + 'steals': type, + 'ordering': type, + 'extra_int_constants': list, +}, Record.__annotations__ [case testTupleOps] from typing import Tuple, List, Any, Optional