Skip to content

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

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

Merged
merged 13 commits into from
Jun 17, 2024
Prev Previous commit
Next Next commit
Minor tweaks
  • Loading branch information
JukkaL committed Jun 14, 2024
commit af3f0587a5868c47fd3fa7cd5d4fcf8215a016d1
9 changes: 5 additions & 4 deletions mypyc/irbuild/statement.py
Original file line number Diff line number Diff line change
Expand Up @@ -1024,23 +1024,24 @@ def transform_match_stmt(builder: IRBuilder, m: MatchStmt) -> None:

def transform_type_alias_stmt(builder: IRBuilder, s: TypeAliasStmt) -> None:
line = s.line
# Use _typing.TypeAliasType to avoid importing "typing", as this can be expensive.
# Use "_typing" to avoid importing "typing", as the latter can be expensive.
# "_typing" includes everything we need here.
mod = builder.call_c(import_op, [builder.load_str("_typing")], line)
type_params = create_type_params(builder, mod, s.type_args, s.line)
typ = builder.py_get_attr(mod, "TypeAliasType", line)

type_alias_type = builder.py_get_attr(mod, "TypeAliasType", line)
args = [builder.load_str(s.name.name), builder.none()]
arg_names: list[str | None] = [None, None]
arg_kinds = [ARG_POS, ARG_POS]
if s.type_args:
args.append(builder.new_tuple(type_params, line))
arg_names.append("type_params")
arg_kinds.append(ARG_NAMED)
alias = builder.py_call(typ, args, line, arg_names=arg_names, arg_kinds=arg_kinds)
alias = builder.py_call(type_alias_type, args, line, arg_names=arg_names, arg_kinds=arg_kinds)

# Use primitive to set function used to lazily compute type alias type value.
# The value needs to be lazily computed to match Python runtime behavior, but
# the public API doesn't support this, so we use a C primitive.
# Python public APIs don't support this, so we use a C primitive.
compute_fn = s.value.accept(builder.visitor)
builder.builder.primitive_op(set_type_alias_compute_function_op, [alias, compute_fn], line)

Expand Down