Skip to content

Commit

Permalink
Support *Ts and **P in type aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
JukkaL committed May 15, 2024
1 parent d8291f3 commit 6c9f73c
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 10 deletions.
49 changes: 39 additions & 10 deletions mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -5233,18 +5233,47 @@ def visit_type_alias_stmt(self, s: TypeAliasStmt) -> None:
for p in s.type_args:
upper_bound = p.upper_bound or self.object_type()
fullname = self.qualified_name(p.name)
type_params.append(
(
p.name,
TypeVarExpr(
default = AnyType(TypeOfAny.from_omitted_generics)
if p.kind == PARAM_SPEC_KIND:
type_params.append(
(
p.name,
fullname,
[],
upper_bound,
default=AnyType(TypeOfAny.from_omitted_generics),
),
ParamSpecExpr(
name=p.name,
fullname=fullname,
upper_bound=upper_bound,
default=default,
),
)
)
elif p.kind == TYPE_VAR_TUPLE_KIND:
tuple_fallback = self.named_type("builtins.tuple", [self.object_type()])
type_params.append(
(
p.name,
TypeVarTupleExpr(
name=p.name,
fullname=fullname,
# Upper bound for *Ts is *tuple[object, ...], it can never be object.
upper_bound=tuple_fallback.copy_modified(),
tuple_fallback=tuple_fallback,
default=default,
),
)
)
else:
type_params.append(
(
p.name,
TypeVarExpr(
p.name,
fullname,
[],
upper_bound,
default=default,
),
)
)
)
all_type_params_names.append(p.name)

self.push_type_args(s.type_args, s)
Expand Down
20 changes: 20 additions & 0 deletions test-data/unit/check-python312.test
Original file line number Diff line number Diff line change
Expand Up @@ -795,6 +795,16 @@ reveal_type(a) # N: Revealed type is "__main__.C[[builtins.int, builtins.str],
reveal_type(a.m) # N: Revealed type is "def (builtins.int, builtins.str)"
[builtins fixtures/tuple.pyi]

[case testPEP695ParamSpecTypeAlias]
# flags: --enable-incomplete-feature=NewGenericSyntax
from typing import Callable

type C[**P] = Callable[P, int]

f: C[[str, int | None]]
reveal_type(f) # N: Revealed type is "def (builtins.str, Union[builtins.int, None]) -> builtins.int"
[builtins fixtures/tuple.pyi]

[case testPEP695TypeVarTuple]
# flags: --enable-incomplete-feature=NewGenericSyntax

Expand All @@ -816,6 +826,16 @@ reveal_type(c) # N: Revealed type is "__main__.C[builtins.str]"
b = c # E: Incompatible types in assignment (expression has type "C[str]", variable has type "C[int, str, None]")
[builtins fixtures/tuple.pyi]

[case testPEP695TypeVarTupleAlias]
# flags: --enable-incomplete-feature=NewGenericSyntax
from typing import Callable

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

a: C[str, None]
reveal_type(a) # N: Revealed type is "Tuple[builtins.str, None, builtins.int]"
[builtins fixtures/tuple.pyi]

[case testPEP695IncrementalFunction]
# flags: --enable-incomplete-feature=NewGenericSyntax
import a
Expand Down

0 comments on commit 6c9f73c

Please sign in to comment.