From 415d49f25b6315cf1b7a04046a942246a033498d Mon Sep 17 00:00:00 2001 From: Jukka Lehtosalo Date: Tue, 11 Jun 2024 17:13:46 +0100 Subject: [PATCH] [mypyc] Support new syntax for generic functions and classes (PEP 695) (#17357) Generate an implicit `Generic` base class for new-style generic classes. For this to work, also create C statics that can be used to access type variable objects (e.g. `T` or `Ts`) at runtime. These are needed when evaluating base classes. Import `TypeVar` and friends from the `_typing` C extension instead of `typing`, since the latter is pretty slow to import, and we don't want to add a hidden new runtime dependency in case the full `typing` module isn't needed. Generic functions don't need any changes, since they don't support indexing with a type, and type variable types aren't valid in runtime contexts. Type erasure seems sufficient, especially considering that mypyc doesn't support classes nested within functions. (I'm not 100% sure about this though, and we might need to put function type variables into statics eventually.) Update builtins test fixtures used in mypyc tests to not defined type variables such as `T`, since these leak into tests and can produce unexpected or unrealistic results. Ignore upper bounds and value restrictions. These are only used for type checking. This should only affect introspection of type variables, which isn't properly supported in compiled code anyway. New type alias syntax is not supported in this PR. --- mypy/nodes.py | 9 +- mypy/semanal.py | 9 +- mypyc/codegen/emitfunc.py | 11 +- mypyc/codegen/emitmodule.py | 11 ++ mypyc/common.py | 1 + mypyc/ir/module_ir.py | 6 + mypyc/ir/ops.py | 3 + mypyc/irbuild/builder.py | 17 +++ mypyc/irbuild/classdef.py | 62 +++++++- mypyc/irbuild/expression.py | 5 + mypyc/irbuild/main.py | 1 + mypyc/primitives/generic_ops.py | 2 +- mypyc/test-data/fixtures/ir.py | 132 ++++++++--------- mypyc/test-data/fixtures/typing-full.pyi | 3 + mypyc/test-data/irbuild-set.test | 5 +- mypyc/test-data/run-loops.test | 11 +- mypyc/test-data/run-python312.test | 172 +++++++++++++++++++++++ mypyc/test/test_run.py | 3 + 18 files changed, 382 insertions(+), 81 deletions(-) create mode 100644 mypyc/test-data/run-python312.test diff --git a/mypy/nodes.py b/mypy/nodes.py index 90561779051d..850b1db87556 100644 --- a/mypy/nodes.py +++ b/mypy/nodes.py @@ -2535,8 +2535,9 @@ def __init__( default: mypy.types.Type, variance: int = INVARIANT, is_new_style: bool = False, + line: int = -1, ) -> None: - super().__init__() + super().__init__(line=line) self._name = name self._fullname = fullname self.upper_bound = upper_bound @@ -2582,8 +2583,9 @@ def __init__( default: mypy.types.Type, variance: int = INVARIANT, is_new_style: bool = False, + line: int = -1, ) -> None: - super().__init__(name, fullname, upper_bound, default, variance, is_new_style) + super().__init__(name, fullname, upper_bound, default, variance, is_new_style, line=line) self.values = values def accept(self, visitor: ExpressionVisitor[T]) -> T: @@ -2661,8 +2663,9 @@ def __init__( default: mypy.types.Type, variance: int = INVARIANT, is_new_style: bool = False, + line: int = -1, ) -> None: - super().__init__(name, fullname, upper_bound, default, variance, is_new_style) + super().__init__(name, fullname, upper_bound, default, variance, is_new_style, line=line) self.tuple_fallback = tuple_fallback def accept(self, visitor: ExpressionVisitor[T]) -> T: diff --git a/mypy/semanal.py b/mypy/semanal.py index 903af80fe404..8da5c68d562d 100644 --- a/mypy/semanal.py +++ b/mypy/semanal.py @@ -1709,7 +1709,7 @@ def push_type_args( self.scope_stack.append(SCOPE_ANNOTATION) tvs: list[tuple[str, TypeVarLikeExpr]] = [] for p in type_args: - tv = self.analyze_type_param(p) + tv = self.analyze_type_param(p, context) if tv is None: return None tvs.append((p.name, tv)) @@ -1732,7 +1732,9 @@ def is_defined_type_param(self, name: str) -> bool: return True return False - def analyze_type_param(self, type_param: TypeParam) -> TypeVarLikeExpr | None: + def analyze_type_param( + self, type_param: TypeParam, context: Context + ) -> TypeVarLikeExpr | None: fullname = self.qualified_name(type_param.name) if type_param.upper_bound: upper_bound = self.anal_type(type_param.upper_bound) @@ -1757,6 +1759,7 @@ def analyze_type_param(self, type_param: TypeParam) -> TypeVarLikeExpr | None: default=default, variance=VARIANCE_NOT_READY, is_new_style=True, + line=context.line, ) elif type_param.kind == PARAM_SPEC_KIND: return ParamSpecExpr( @@ -1765,6 +1768,7 @@ def analyze_type_param(self, type_param: TypeParam) -> TypeVarLikeExpr | None: upper_bound=upper_bound, default=default, is_new_style=True, + line=context.line, ) else: assert type_param.kind == TYPE_VAR_TUPLE_KIND @@ -1777,6 +1781,7 @@ def analyze_type_param(self, type_param: TypeParam) -> TypeVarLikeExpr | None: tuple_fallback=tuple_fallback, default=default, is_new_style=True, + line=context.line, ) def pop_type_args(self, type_args: list[TypeParam] | None) -> None: diff --git a/mypyc/codegen/emitfunc.py b/mypyc/codegen/emitfunc.py index 12f57b9cee6f..d945a28d8481 100644 --- a/mypyc/codegen/emitfunc.py +++ b/mypyc/codegen/emitfunc.py @@ -6,7 +6,14 @@ from mypyc.analysis.blockfreq import frequently_executed_blocks from mypyc.codegen.emit import DEBUG_ERRORS, Emitter, TracebackAndGotoHandler, c_array_initializer -from mypyc.common import MODULE_PREFIX, NATIVE_PREFIX, REG_PREFIX, STATIC_PREFIX, TYPE_PREFIX +from mypyc.common import ( + MODULE_PREFIX, + NATIVE_PREFIX, + REG_PREFIX, + STATIC_PREFIX, + TYPE_PREFIX, + TYPE_VAR_PREFIX, +) from mypyc.ir.class_ir import ClassIR from mypyc.ir.func_ir import FUNC_CLASSMETHOD, FUNC_STATICMETHOD, FuncDecl, FuncIR, all_values from mypyc.ir.ops import ( @@ -14,6 +21,7 @@ NAMESPACE_MODULE, NAMESPACE_STATIC, NAMESPACE_TYPE, + NAMESPACE_TYPE_VAR, Assign, AssignMulti, BasicBlock, @@ -477,6 +485,7 @@ def visit_set_attr(self, op: SetAttr) -> None: NAMESPACE_STATIC: STATIC_PREFIX, NAMESPACE_TYPE: TYPE_PREFIX, NAMESPACE_MODULE: MODULE_PREFIX, + NAMESPACE_TYPE_VAR: TYPE_VAR_PREFIX, } def visit_load_static(self, op: LoadStatic) -> None: diff --git a/mypyc/codegen/emitmodule.py b/mypyc/codegen/emitmodule.py index 6c8f5ac91335..1d8708912de5 100644 --- a/mypyc/codegen/emitmodule.py +++ b/mypyc/codegen/emitmodule.py @@ -41,6 +41,7 @@ PREFIX, RUNTIME_C_FILES, TOP_LEVEL_NAME, + TYPE_VAR_PREFIX, shared_lib_name, short_id_from_name, use_vectorcall, @@ -590,6 +591,7 @@ def generate_c_for_modules(self) -> list[tuple[str, str]]: self.declare_finals(module_name, module.final_names, declarations) for cl in module.classes: generate_class_type_decl(cl, emitter, ext_declarations, declarations) + self.declare_type_vars(module_name, module.type_var_names, declarations) for fn in module.functions: generate_function_declaration(fn, declarations) @@ -1063,6 +1065,15 @@ def declare_static_pyobject(self, identifier: str, emitter: Emitter) -> None: symbol = emitter.static_name(identifier, None) self.declare_global("PyObject *", symbol) + def declare_type_vars(self, module: str, type_var_names: list[str], emitter: Emitter) -> None: + for name in type_var_names: + static_name = emitter.static_name(name, module, prefix=TYPE_VAR_PREFIX) + emitter.context.declarations[static_name] = HeaderDeclaration( + f"PyObject *{static_name};", + [f"PyObject *{static_name} = NULL;"], + needs_export=False, + ) + def sort_classes(classes: list[tuple[str, ClassIR]]) -> list[tuple[str, ClassIR]]: mod_name = {ir: name for name, ir in classes} diff --git a/mypyc/common.py b/mypyc/common.py index 3d07f6c3d0d3..d7610fe15c41 100644 --- a/mypyc/common.py +++ b/mypyc/common.py @@ -13,6 +13,7 @@ STATIC_PREFIX: Final = "CPyStatic_" # Static variables (for literals etc.) TYPE_PREFIX: Final = "CPyType_" # Type object struct MODULE_PREFIX: Final = "CPyModule_" # Cached modules +TYPE_VAR_PREFIX: Final = "CPyTypeVar_" # Type variables when using new-style Python 3.12 syntax ATTR_PREFIX: Final = "_" # Attributes ENV_ATTR_NAME: Final = "__mypyc_env__" diff --git a/mypyc/ir/module_ir.py b/mypyc/ir/module_ir.py index dcf6f8768547..e3b240629eda 100644 --- a/mypyc/ir/module_ir.py +++ b/mypyc/ir/module_ir.py @@ -21,12 +21,17 @@ def __init__( functions: list[FuncIR], classes: list[ClassIR], final_names: list[tuple[str, RType]], + type_var_names: list[str], ) -> None: self.fullname = fullname self.imports = imports.copy() self.functions = functions self.classes = classes self.final_names = final_names + # Names of C statics used for Python 3.12 type variable objects. + # These are only visible in the module that defined them, so no need + # to serialize. + self.type_var_names = type_var_names def serialize(self) -> JsonDict: return { @@ -45,6 +50,7 @@ def deserialize(cls, data: JsonDict, ctx: DeserMaps) -> ModuleIR: [ctx.functions[FuncDecl.get_id_from_json(f)] for f in data["functions"]], [ClassIR.deserialize(c, ctx) for c in data["classes"]], [(k, deserialize_type(t, ctx)) for k, t in data["final_names"]], + [], ) diff --git a/mypyc/ir/ops.py b/mypyc/ir/ops.py index 377266e797d9..896ba3ac091c 100644 --- a/mypyc/ir/ops.py +++ b/mypyc/ir/ops.py @@ -789,6 +789,9 @@ def accept(self, visitor: OpVisitor[T]) -> T: # Namespace for modules NAMESPACE_MODULE: Final = "module" +# Namespace for Python 3.12 type variable objects (implicitly created TypeVar instances, etc.) +NAMESPACE_TYPE_VAR: Final = "typevar" + class LoadStatic(RegisterOp): """Load a static name (name :: static). diff --git a/mypyc/irbuild/builder.py b/mypyc/irbuild/builder.py index cca771e82c83..1b4f551d4a2a 100644 --- a/mypyc/irbuild/builder.py +++ b/mypyc/irbuild/builder.py @@ -69,6 +69,7 @@ from mypyc.ir.func_ir import INVALID_FUNC_DEF, FuncDecl, FuncIR, FuncSignature, RuntimeArg from mypyc.ir.ops import ( NAMESPACE_MODULE, + NAMESPACE_TYPE_VAR, Assign, BasicBlock, Branch, @@ -179,6 +180,7 @@ def __init__( self.function_names: set[tuple[str | None, str]] = set() self.classes: list[ClassIR] = [] self.final_names: list[tuple[str, RType]] = [] + self.type_var_names: list[str] = [] self.callable_class_names: set[str] = set() self.options = options @@ -541,6 +543,21 @@ def load_final_static( error_msg=f'value for final name "{error_name}" was not set', ) + def init_type_var(self, value: Value, name: str, line: int) -> None: + unique_name = name + "___" + str(line) + self.type_var_names.append(unique_name) + self.add(InitStatic(value, unique_name, self.module_name, namespace=NAMESPACE_TYPE_VAR)) + + def load_type_var(self, name: str, line: int) -> Value: + return self.add( + LoadStatic( + object_rprimitive, + name + "___" + str(line), + self.module_name, + namespace=NAMESPACE_TYPE_VAR, + ) + ) + def load_literal_value(self, val: int | str | bytes | float | complex | bool) -> Value: """Load value of a final name, class-level attribute, or constant folded expression.""" if isinstance(val, bool): diff --git a/mypyc/irbuild/classdef.py b/mypyc/irbuild/classdef.py index 3f6ec0f33822..303ee8849244 100644 --- a/mypyc/irbuild/classdef.py +++ b/mypyc/irbuild/classdef.py @@ -7,6 +7,9 @@ from typing import Callable, Final from mypy.nodes import ( + PARAM_SPEC_KIND, + TYPE_VAR_KIND, + TYPE_VAR_TUPLE_KIND, AssignmentStmt, CallExpr, ClassDef, @@ -22,6 +25,7 @@ StrExpr, TempNode, TypeInfo, + TypeParam, is_class_var, ) from mypy.types import ENUM_REMOVED_PROPS, Instance, RawExpressionType, get_proper_type @@ -63,9 +67,16 @@ ) from mypyc.irbuild.util import dataclass_type, get_func_def, is_constant, is_dataclass_decorator from mypyc.primitives.dict_ops import dict_new_op, dict_set_item_op -from mypyc.primitives.generic_ops import py_hasattr_op, py_setattr_op +from mypyc.primitives.generic_ops import ( + iter_op, + next_op, + py_get_item_op, + py_hasattr_op, + py_setattr_op, +) from mypyc.primitives.misc_ops import ( dataclass_sleight_of_hand, + import_op, not_implemented_op, py_calc_meta_op, pytype_from_template_op, @@ -405,8 +416,14 @@ def get_type_annotation(self, stmt: AssignmentStmt) -> TypeInfo | None: def allocate_class(builder: IRBuilder, cdef: ClassDef) -> Value: # OK AND NOW THE FUN PART base_exprs = cdef.base_type_exprs + cdef.removed_base_type_exprs - if base_exprs: - bases = [builder.accept(x) for x in base_exprs] + new_style_type_args = cdef.type_args + if new_style_type_args: + bases = [make_generic_base_class(builder, cdef.fullname, new_style_type_args, cdef.line)] + else: + bases = [] + + if base_exprs or new_style_type_args: + bases.extend([builder.accept(x) for x in base_exprs]) tp_bases = builder.new_tuple(bases, cdef.line) else: tp_bases = builder.add(LoadErrorValue(object_rprimitive, is_borrowed=True)) @@ -453,6 +470,45 @@ def allocate_class(builder: IRBuilder, cdef: ClassDef) -> Value: return tp +def make_generic_base_class( + builder: IRBuilder, fullname: str, type_args: list[TypeParam], line: int +) -> Value: + """Construct Generic[...] base class object for a new-style generic class (Python 3.12).""" + mod = builder.call_c(import_op, [builder.load_str("_typing")], line) + tvs = [] + type_var_imported: Value | None = None + for type_param in type_args: + unpack = False + if type_param.kind == TYPE_VAR_KIND: + if type_var_imported: + # Reuse previously imported value as a minor optimization + tvt = type_var_imported + else: + tvt = builder.py_get_attr(mod, "TypeVar", line) + type_var_imported = tvt + elif type_param.kind == TYPE_VAR_TUPLE_KIND: + tvt = builder.py_get_attr(mod, "TypeVarTuple", line) + unpack = True + else: + assert type_param.kind == PARAM_SPEC_KIND + tvt = builder.py_get_attr(mod, "ParamSpec", line) + tv = builder.py_call(tvt, [builder.load_str(type_param.name)], line) + builder.init_type_var(tv, type_param.name, line) + if unpack: + # Evaluate *Ts for a TypeVarTuple + it = builder.call_c(iter_op, [tv], line) + tv = builder.call_c(next_op, [it], line) + tvs.append(tv) + gent = builder.py_get_attr(mod, "Generic", line) + if len(tvs) == 1: + arg = tvs[0] + else: + arg = builder.new_tuple(tvs, line) + + base = builder.call_c(py_get_item_op, [gent, arg], line) + return base + + # Mypy uses these internally as base classes of TypedDict classes. These are # lies and don't have any runtime equivalent. MAGIC_TYPED_DICT_CLASSES: Final[tuple[str, ...]] = ( diff --git a/mypyc/irbuild/expression.py b/mypyc/irbuild/expression.py index a16faf6cd7d7..8d7c089e20cd 100644 --- a/mypyc/irbuild/expression.py +++ b/mypyc/irbuild/expression.py @@ -44,6 +44,7 @@ TupleExpr, TypeApplication, TypeInfo, + TypeVarLikeExpr, UnaryExpr, Var, ) @@ -106,6 +107,10 @@ def transform_name_expr(builder: IRBuilder, expr: NameExpr) -> Value: + if isinstance(expr.node, TypeVarLikeExpr) and expr.node.is_new_style: + # Reference to Python 3.12 implicit TypeVar/TupleVarTuple/... object. + # These are stored in C statics and not visible in Python namespaces. + return builder.load_type_var(expr.node.name, expr.node.line) if expr.node is None: builder.add( RaiseStandardError( diff --git a/mypyc/irbuild/main.py b/mypyc/irbuild/main.py index 85b905393af1..15928d939cbf 100644 --- a/mypyc/irbuild/main.py +++ b/mypyc/irbuild/main.py @@ -99,6 +99,7 @@ def build_ir( builder.functions, builder.classes, builder.final_names, + builder.type_var_names, ) result[module.fullname] = module_ir class_irs.extend(builder.classes) diff --git a/mypyc/primitives/generic_ops.py b/mypyc/primitives/generic_ops.py index 3caec0a9875e..fe42767db11e 100644 --- a/mypyc/primitives/generic_ops.py +++ b/mypyc/primitives/generic_ops.py @@ -178,7 +178,7 @@ ) # obj1[obj2] -method_op( +py_get_item_op = method_op( name="__getitem__", arg_types=[object_rprimitive, object_rprimitive], return_type=object_rprimitive, diff --git a/mypyc/test-data/fixtures/ir.py b/mypyc/test-data/fixtures/ir.py index bf06613ad2a8..6f0d8da90d57 100644 --- a/mypyc/test-data/fixtures/ir.py +++ b/mypyc/test-data/fixtures/ir.py @@ -7,12 +7,12 @@ overload, Mapping, Union, Callable, Sequence, FrozenSet, Protocol ) -T = TypeVar('T') +_T = TypeVar('_T') T_co = TypeVar('T_co', covariant=True) T_contra = TypeVar('T_contra', contravariant=True) -S = TypeVar('S') -K = TypeVar('K') # for keys in mapping -V = TypeVar('V') # for values in mapping +_S = TypeVar('_S') +_K = TypeVar('_K') # for keys in mapping +_V = TypeVar('_V') # for values in mapping class __SupportsAbs(Protocol[T_co]): def __abs__(self) -> T_co: pass @@ -199,76 +199,76 @@ def __contains__(self, item: object) -> int: ... class function: pass -class list(Generic[T], Sequence[T], Iterable[T]): - def __init__(self, i: Optional[Iterable[T]] = None) -> None: pass +class list(Generic[_T], Sequence[_T], Iterable[_T]): + def __init__(self, i: Optional[Iterable[_T]] = None) -> None: pass @overload - def __getitem__(self, i: int) -> T: ... + def __getitem__(self, i: int) -> _T: ... @overload - def __getitem__(self, s: slice) -> List[T]: ... - def __setitem__(self, i: int, o: T) -> None: pass + def __getitem__(self, s: slice) -> List[_T]: ... + def __setitem__(self, i: int, o: _T) -> None: pass def __delitem__(self, i: int) -> None: pass - def __mul__(self, i: int) -> List[T]: pass - def __rmul__(self, i: int) -> List[T]: pass - def __iter__(self) -> Iterator[T]: pass + def __mul__(self, i: int) -> List[_T]: pass + def __rmul__(self, i: int) -> List[_T]: pass + def __iter__(self) -> Iterator[_T]: pass def __len__(self) -> int: pass def __contains__(self, item: object) -> int: ... - def __add__(self, x: List[T]) -> List[T]: ... - def append(self, x: T) -> None: pass - def pop(self, i: int = -1) -> T: pass - def count(self, T) -> int: pass - def extend(self, l: Iterable[T]) -> None: pass - def insert(self, i: int, x: T) -> None: pass + def __add__(self, x: List[_T]) -> List[_T]: ... + def append(self, x: _T) -> None: pass + def pop(self, i: int = -1) -> _T: pass + def count(self, _T) -> int: pass + def extend(self, l: Iterable[_T]) -> None: pass + def insert(self, i: int, x: _T) -> None: pass def sort(self) -> None: pass def reverse(self) -> None: pass - def remove(self, o: T) -> None: pass - def index(self, o: T) -> int: pass + def remove(self, o: _T) -> None: pass + def index(self, o: _T) -> int: pass -class dict(Mapping[K, V]): +class dict(Mapping[_K, _V]): @overload - def __init__(self, **kwargs: K) -> None: ... + def __init__(self, **kwargs: _K) -> None: ... @overload - def __init__(self, map: Mapping[K, V], **kwargs: V) -> None: ... + def __init__(self, map: Mapping[_K, _V], **kwargs: _V) -> None: ... @overload - def __init__(self, iterable: Iterable[Tuple[K, V]], **kwargs: V) -> None: ... - def __getitem__(self, key: K) -> V: pass - def __setitem__(self, k: K, v: V) -> None: pass - def __delitem__(self, k: K) -> None: pass + def __init__(self, iterable: Iterable[Tuple[_K, _V]], **kwargs: _V) -> None: ... + def __getitem__(self, key: _K) -> _V: pass + def __setitem__(self, k: _K, v: _V) -> None: pass + def __delitem__(self, k: _K) -> None: pass def __contains__(self, item: object) -> int: pass - def __iter__(self) -> Iterator[K]: pass + def __iter__(self) -> Iterator[_K]: pass def __len__(self) -> int: pass @overload - def update(self, __m: Mapping[K, V], **kwargs: V) -> None: pass + def update(self, __m: Mapping[_K, _V], **kwargs: _V) -> None: pass @overload - def update(self, __m: Iterable[Tuple[K, V]], **kwargs: V) -> None: ... + def update(self, __m: Iterable[Tuple[_K, _V]], **kwargs: _V) -> None: ... @overload - def update(self, **kwargs: V) -> None: ... - def pop(self, x: int) -> K: pass - def keys(self) -> Iterable[K]: pass - def values(self) -> Iterable[V]: pass - def items(self) -> Iterable[Tuple[K, V]]: pass + def update(self, **kwargs: _V) -> None: ... + def pop(self, x: int) -> _K: pass + def keys(self) -> Iterable[_K]: pass + def values(self) -> Iterable[_V]: pass + def items(self) -> Iterable[Tuple[_K, _V]]: pass def clear(self) -> None: pass - def copy(self) -> Dict[K, V]: pass - def setdefault(self, key: K, val: V = ...) -> V: pass + def copy(self) -> Dict[_K, _V]: pass + def setdefault(self, key: _K, val: _V = ...) -> _V: pass -class set(Generic[T]): - def __init__(self, i: Optional[Iterable[T]] = None) -> None: pass - def __iter__(self) -> Iterator[T]: pass +class set(Generic[_T]): + def __init__(self, i: Optional[Iterable[_T]] = None) -> None: pass + def __iter__(self) -> Iterator[_T]: pass def __len__(self) -> int: pass - def add(self, x: T) -> None: pass - def remove(self, x: T) -> None: pass - def discard(self, x: T) -> None: pass + def add(self, x: _T) -> None: pass + def remove(self, x: _T) -> None: pass + 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: Union[Set[S], FrozenSet[S]]) -> Set[Union[T, S]]: ... - def __xor__(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 pop(self) -> _T: pass + def update(self, x: Iterable[_S]) -> None: pass + def __or__(self, s: Union[Set[_S], FrozenSet[_S]]) -> Set[Union[_T, _S]]: ... + def __xor__(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]]: ... - def __xor__(self, s: Union[Set[S], FrozenSet[S]]) -> FrozenSet[Union[T, S]]: ... + def __or__(self, s: Union[Set[_S], FrozenSet[_S]]) -> FrozenSet[Union[_T, _S]]: ... + def __xor__(self, s: Union[Set[_S], FrozenSet[_S]]) -> FrozenSet[Union[_T, _S]]: ... class slice: pass @@ -323,31 +323,31 @@ class OverflowError(ArithmeticError): pass class GeneratorExit(BaseException): pass -def any(i: Iterable[T]) -> bool: pass -def all(i: Iterable[T]) -> bool: pass -def sum(i: Iterable[T]) -> int: pass -def reversed(object: Sequence[T]) -> Iterator[T]: ... +def any(i: Iterable[_T]) -> bool: pass +def all(i: Iterable[_T]) -> bool: pass +def sum(i: Iterable[_T]) -> int: pass +def reversed(object: Sequence[_T]) -> Iterator[_T]: ... def id(o: object) -> int: pass # This type is obviously wrong but the test stubs don't have Sized anymore def len(o: object) -> int: pass def print(*object) -> None: pass def isinstance(x: object, t: object) -> bool: pass -def iter(i: Iterable[T]) -> Iterator[T]: pass +def iter(i: Iterable[_T]) -> Iterator[_T]: pass @overload -def next(i: Iterator[T]) -> T: pass +def next(i: Iterator[_T]) -> _T: pass @overload -def next(i: Iterator[T], default: T) -> T: pass +def next(i: Iterator[_T], default: _T) -> _T: pass def hash(o: object) -> int: ... def globals() -> Dict[str, Any]: ... def getattr(obj: object, name: str, default: Any = None) -> Any: ... def setattr(obj: object, name: str, value: Any) -> None: ... -def enumerate(x: Iterable[T]) -> Iterator[Tuple[int, T]]: ... +def enumerate(x: Iterable[_T]) -> Iterator[Tuple[int, _T]]: ... @overload -def zip(x: Iterable[T], y: Iterable[S]) -> Iterator[Tuple[T, S]]: ... +def zip(x: Iterable[_T], y: Iterable[_S]) -> Iterator[Tuple[_T, _S]]: ... @overload -def zip(x: Iterable[T], y: Iterable[S], z: Iterable[V]) -> Iterator[Tuple[T, S, V]]: ... +def zip(x: Iterable[_T], y: Iterable[_S], z: Iterable[_V]) -> Iterator[Tuple[_T, _S, _V]]: ... def eval(e: str) -> Any: ... -def abs(x: __SupportsAbs[T]) -> T: ... +def abs(x: __SupportsAbs[_T]) -> _T: ... @overload def divmod(x: __SupportsDivMod[T_contra, T_co], y: T_contra) -> T_co: ... @overload @@ -359,8 +359,8 @@ def pow(base: __SupportsPow3NoneOnly[T_contra, T_co], exp: T_contra, mod: None = @overload def pow(base: __SupportsPow3[T_contra, _M, T_co], exp: T_contra, mod: _M) -> T_co: ... def exit() -> None: ... -def min(x: T, y: T) -> T: ... -def max(x: T, y: T) -> T: ... +def min(x: _T, y: _T) -> _T: ... +def max(x: _T, y: _T) -> _T: ... def repr(o: object) -> str: ... def ascii(o: object) -> str: ... def ord(o: object) -> int: ... diff --git a/mypyc/test-data/fixtures/typing-full.pyi b/mypyc/test-data/fixtures/typing-full.pyi index 52bca09a1dec..3ddc1f1bba08 100644 --- a/mypyc/test-data/fixtures/typing-full.pyi +++ b/mypyc/test-data/fixtures/typing-full.pyi @@ -167,3 +167,6 @@ class _TypedDict(Mapping[str, object]): def pop(self, k: NoReturn, default: T = ...) -> object: ... def update(self: T, __m: T) -> None: ... def __delitem__(self, k: NoReturn) -> None: ... + +class TypeAliasType: + pass diff --git a/mypyc/test-data/irbuild-set.test b/mypyc/test-data/irbuild-set.test index 1ac638754a8b..110801b78a66 100644 --- a/mypyc/test-data/irbuild-set.test +++ b/mypyc/test-data/irbuild-set.test @@ -39,7 +39,10 @@ L0: return r0 [case testNewSetFromIterable] -from typing import Set, List +from typing import Set, List, TypeVar + +T = TypeVar("T") + def f(l: List[T]) -> Set[T]: return set(l) [out] diff --git a/mypyc/test-data/run-loops.test b/mypyc/test-data/run-loops.test index 994b30b42347..6f7d79059a6d 100644 --- a/mypyc/test-data/run-loops.test +++ b/mypyc/test-data/run-loops.test @@ -276,7 +276,10 @@ for k in range(12): [out] [case testForIterable] -from typing import Iterable, Dict, Any, Tuple +from typing import Iterable, Dict, Any, Tuple, TypeVar + +T = TypeVar("T") + def iterate_over_any(a: Any) -> None: for element in a: print(element) @@ -350,13 +353,13 @@ iterate_over_tuple((1, 2, 3)) Traceback (most recent call last): File "driver.py", line 16, in iterate_over_any(5) - File "native.py", line 3, in iterate_over_any + File "native.py", line 6, in iterate_over_any for element in a: TypeError: 'int' object is not iterable Traceback (most recent call last): File "driver.py", line 20, in iterate_over_iterable(broken_generator(5)) - File "native.py", line 7, in iterate_over_iterable + File "native.py", line 10, in iterate_over_iterable for element in iterable: File "driver.py", line 8, in broken_generator raise Exception('Exception Manually Raised') @@ -364,7 +367,7 @@ Exception: Exception Manually Raised Traceback (most recent call last): File "driver.py", line 24, in iterate_and_delete(d) - File "native.py", line 11, in iterate_and_delete + File "native.py", line 14, in iterate_and_delete for key in d: RuntimeError: dictionary changed size during iteration 15 diff --git a/mypyc/test-data/run-python312.test b/mypyc/test-data/run-python312.test new file mode 100644 index 000000000000..fbafeaf3e65f --- /dev/null +++ b/mypyc/test-data/run-python312.test @@ -0,0 +1,172 @@ +[case testPEP695Basics] +# flags: --enable-incomplete-feature=NewGenericSyntax +from typing import Any, TypeAliasType, cast + +from testutil import assertRaises + +def id[T](x: T) -> T: + return x + +def test_call_generic_function() -> None: + assert id(2) == 2 + assert id('x') == 'x' + +class C[T]: + x: T + + def __init__(self, x: T) -> None: + self.x = x + +class D[T, S]: + x: T + y: S + + def __init__(self, x: T, y: S) -> None: + self.x = x + self.y = y + + def set(self, x: object, y: object) -> None: + self.x = cast(T, x) + self.y = cast(S, y) + +def test_generic_class() -> None: + c = C(5) + assert c.x == 5 + c2 = C[str]('x') + assert c2.x == 'x' + d = D[str, int]('a', 5) + assert d.x == 'a' + assert d.y == 5 + d.set('b', 6) + assert d.x == 'b' + assert d.y == 6 + +def test_generic_class_via_any() -> None: + c_any: Any = C + c = c_any(2) + assert c.x == 2 + c2 = c_any[str]('y') + assert c2.x == 'y' + assert str(c_any[str]) == 'native.C[str]' + + d_any: Any = D + d = d_any(1, 'x') + assert d.x == 1 + assert d.y == 'x' + d2 = d_any[int, str](2, 'y') + assert d2.x == 2 + assert d2.y == 'y' + + with assertRaises(TypeError): + c_any[int, str] + with assertRaises(TypeError): + d_any[int] + +class E[*Ts]: pass + +def test_type_var_tuple() -> None: + e: E[int, str] = E() + e_any: Any = E + assert isinstance(e_any(), E) + assert isinstance(e_any[int](), E) + assert isinstance(e_any[int, str](), E) + +class F[**P]: pass + +def test_param_spec() -> None: + f: F[[int, str]] = F() + f_any: Any = F + assert isinstance(f_any(), F) + assert isinstance(f_any[[int, str]](), F) + +class SubC[S](C[S]): + def __init__(self, x: S) -> None: + super().__init__(x) + +def test_generic_subclass() -> None: + s = SubC(1) + assert s.x == 1 + s2 = SubC[str]('y') + assert s2.x == 'y' + sub_any: Any = SubC + assert sub_any(1).x == 1 + assert sub_any[str]('x').x == 'x' + assert isinstance(s, SubC) + assert isinstance(s, C) + +class SubD[ + T, # Put everything on separate lines + S]( + D[T, + S]): pass + +def test_generic_subclass_two_params() -> None: + s = SubD(3, 'y') + assert s.x == 3 + assert s.y == 'y' + s2 = SubD[str, int]('z', 4) + assert s2.x == 'z' + assert s2.y == 4 + sub_any: Any = SubD + assert sub_any(3, 'y').y == 'y' + assert sub_any[int, str](3, 'y').y == 'y' + assert isinstance(s, SubD) + assert isinstance(s, D) + +class SubE[*Ts](E[*Ts]): pass + +def test_type_var_tuple_subclass() -> None: + sub_any: Any = SubE + assert isinstance(sub_any(), SubE) + assert isinstance(sub_any(), E) + assert isinstance(sub_any[int](), SubE) + assert isinstance(sub_any[int, str](), SubE) + + +class SubF[**P](F[P]): pass + +def test_param_spec_subclass() -> None: + sub_any: Any = SubF + assert isinstance(sub_any(), SubF) + assert isinstance(sub_any(), F) + assert isinstance(sub_any[[int]](), SubF) + assert isinstance(sub_any[[int, str]](), SubF) + +# We test that upper bounds and restricted values can be used, but not that +# they are introspectable + +def bound[T: C](x: T) -> T: + return x + +def test_function_with_upper_bound() -> None: + c = C(1) + assert bound(c) is c + +def restriction[T: (int, str)](x: T) -> T: + return x + +def test_function_with_value_restriction() -> None: + assert restriction(1) == 1 + assert restriction('x') == 'x' + +class Bound[T: C]: + def __init__(self, x: T) -> None: + self.x = x + +def test_class_with_upper_bound() -> None: + c = C(1) + b = Bound(c) + assert b.x is c + b2 = Bound[C](c) + assert b2.x is c + +class Restriction[T: (int, str)]: + def __init__(self, x: T) -> None: + self.x = x + +def test_class_with_value_restriction() -> None: + r = Restriction(1) + assert r.x == 1 + r2 = Restriction[str]('a') + assert r2.x == 'a' +[typing fixtures/typing-full.pyi] diff --git a/mypyc/test/test_run.py b/mypyc/test/test_run.py index 467ef8b87a92..37de192a9291 100644 --- a/mypyc/test/test_run.py +++ b/mypyc/test/test_run.py @@ -71,6 +71,8 @@ if sys.version_info >= (3, 10): files.append("run-match.test") +if sys.version_info >= (3, 12): + files.append("run-python312.test") setup_format = """\ from setuptools import setup @@ -194,6 +196,7 @@ def run_case_step(self, testcase: DataDrivenTestCase, incremental_step: int) -> options.preserve_asts = True options.allow_empty_bodies = True options.incremental = self.separate + options.enable_incomplete_feature.append("NewGenericSyntax") # Avoid checking modules/packages named 'unchecked', to provide a way # to test interacting with code we don't have types for.