Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[mypyc] Support Python 3.12 type alias syntax (PEP 695) #17384

Merged
merged 13 commits into from
Jun 17, 2024
Prev Previous commit
Next Next commit
Refactor
  • Loading branch information
JukkaL committed Jun 14, 2024
commit 54689307b471e5d676a1b339eefaec6f205a96b3
8 changes: 4 additions & 4 deletions mypyc/irbuild/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
ARG_POS,
GDEF,
LDEF,
PARAM_SPEC_KIND,
TYPE_VAR_KIND,
TYPE_VAR_TUPLE_KIND,
ArgKind,
CallExpr,
Decorator,
Expand All @@ -44,12 +47,9 @@
TupleExpr,
TypeAlias,
TypeInfo,
TypeParam,
UnaryExpr,
Var,
TypeParam,
TYPE_VAR_KIND,
TYPE_VAR_TUPLE_KIND,
PARAM_SPEC_KIND,
)
from mypy.types import (
AnyType,
Expand Down
37 changes: 10 additions & 27 deletions mypyc/irbuild/classdef.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
from typing import Callable, Final

from mypy.nodes import (
PARAM_SPEC_KIND,
TYPE_VAR_KIND,
TYPE_VAR_TUPLE_KIND,
AssignmentStmt,
CallExpr,
Expand Down Expand Up @@ -57,7 +55,7 @@
is_optional_type,
object_rprimitive,
)
from mypyc.irbuild.builder import IRBuilder
from mypyc.irbuild.builder import IRBuilder, create_type_params
from mypyc.irbuild.function import (
gen_property_getter_ir,
gen_property_setter_ir,
Expand Down Expand Up @@ -475,35 +473,20 @@ def make_generic_base_class(
) -> 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:
tvs = create_type_params(builder, mod, type_args, line)
args = []
for tv, type_param in zip(tvs, type_args):
if type_param.kind == TYPE_VAR_TUPLE_KIND:
# Evaluate *Ts for a TypeVarTuple
it = builder.call_c(iter_op, [tv], line)
tv = builder.call_c(next_op, [it], line)
tvs.append(tv)
args.append(tv)

gent = builder.py_get_attr(mod, "Generic", line)
if len(tvs) == 1:
arg = tvs[0]
if len(args) == 1:
arg = args[0]
else:
arg = builder.new_tuple(tvs, line)
arg = builder.new_tuple(args, line)

base = builder.call_c(py_get_item_op, [gent, arg], line)
return base
Expand Down
4 changes: 2 additions & 2 deletions mypyc/irbuild/statement.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
from typing import Callable, Sequence

from mypy.nodes import (
ARG_POS,
ARG_NAMED,
ARG_POS,
AssertStmt,
AssignmentStmt,
AwaitExpr,
Expand Down Expand Up @@ -77,7 +77,7 @@
object_rprimitive,
)
from mypyc.irbuild.ast_helpers import is_borrow_friendly_expr, process_conditional
from mypyc.irbuild.builder import IRBuilder, int_borrow_friendly_op, create_type_params
from mypyc.irbuild.builder import IRBuilder, create_type_params, int_borrow_friendly_op
from mypyc.irbuild.for_helpers import for_loop_helper
from mypyc.irbuild.generator import add_raise_exception_blocks_to_generator_class
from mypyc.irbuild.nonlocalcontrol import (
Expand Down