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
Set infer_variance=True at runtime
  • Loading branch information
JukkaL committed Jun 14, 2024
commit d009aa51cd8c11fa6513a0a1571946717da0fed3
17 changes: 14 additions & 3 deletions mypyc/irbuild/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -1415,8 +1415,9 @@ def get_call_target_fullname(ref: RefExpr) -> str:
return ref.fullname


def create_type_params(builder: IRBuilder, typing_mod: Value,
type_args: list[TypeParam], line: int) -> list[Value]:
def create_type_params(
builder: IRBuilder, typing_mod: Value, type_args: list[TypeParam], line: int
) -> list[Value]:
tvs = []
type_var_imported: Value | None = None
for type_param in type_args:
Expand All @@ -1432,7 +1433,17 @@ def create_type_params(builder: IRBuilder, typing_mod: Value,
else:
assert type_param.kind == PARAM_SPEC_KIND
tvt = builder.py_get_attr(typing_mod, "ParamSpec", line)
tv = builder.py_call(tvt, [builder.load_str(type_param.name)], line)
if type_param.kind != TYPE_VAR_TUPLE_KIND:
# To match runtime semantics, pass infer_variance=True
tv = builder.py_call(
tvt,
[builder.load_str(type_param.name), builder.true()],
line,
arg_kinds=[ARG_POS, ARG_NAMED],
arg_names=[None, "infer_variance"],
)
else:
tv = builder.py_call(tvt, [builder.load_str(type_param.name)], line)
builder.init_type_var(tv, type_param.name, line)
tvs.append(tv)
return tvs
8 changes: 4 additions & 4 deletions mypyc/test-data/run-python312.test
Original file line number Diff line number Diff line change
Expand Up @@ -203,13 +203,13 @@ type A[T] = list[T]
def test_generic_alias() -> None:
assert type(A[str]) is GenericAlias
assert str(A[str]) == "A[str]"
assert str(getattr(A, "__value__")) == "list[~T]"
assert str(getattr(A, "__value__")) == "list[T]"

type B[T, S] = dict[S, T]

def test_generic_alias_with_two_args() -> None:
assert str(B[str, int]) == "B[str, int]"
assert str(getattr(B, "__value__")) == "dict[~S, ~T]" # TODO: T, S
assert str(getattr(B, "__value__")) == "dict[S, T]"

type C[*Ts] = tuple[*Ts]

Expand All @@ -220,6 +220,6 @@ def test_type_var_tuple_type_alias() -> None:
type D[**P] = Callable[P, int]

def test_param_spec_type_alias() -> None:
assert str(D[int, str]) == "D[int, str]"
assert str(getattr(D, "__value__")) == "typing.Callable[~P, int]"
assert str(D[[int, str]]) == "D[[int, str]]"
assert str(getattr(D, "__value__")) == "typing.Callable[P, int]"
[typing fixtures/typing-full.pyi]