Skip to content

Allow TypeVarTuple arguments to subclasses of generic TypedDict #19234

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions mypy/semanal_shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ def anal_type(
allow_tuple_literal: bool = False,
allow_unbound_tvars: bool = False,
allow_typed_dict_special_forms: bool = False,
allow_unpack: bool = False,
allow_placeholder: bool = False,
report_invalid_types: bool = True,
prohibit_self_type: str | None = None,
Expand Down
5 changes: 4 additions & 1 deletion mypy/semanal_typeddict.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,13 +253,16 @@ def analyze_base_args(self, base: IndexExpr, ctx: Context) -> list[Type] | None:

for arg_expr in args:
try:
type = expr_to_unanalyzed_type(arg_expr, self.options, self.api.is_stub_file)
type = expr_to_unanalyzed_type(
arg_expr, self.options, self.api.is_stub_file, allow_unpack=True
)
except TypeTranslationError:
self.fail("Invalid TypedDict type argument", ctx)
return None
analyzed = self.api.anal_type(
type,
allow_typed_dict_special_forms=True,
allow_unpack=True,
allow_placeholder=not self.api.is_func_scope(),
)
if analyzed is None:
Expand Down
49 changes: 49 additions & 0 deletions test-data/unit/check-python311.test
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,55 @@ x4: Alias4[int] # E: Bad number of arguments for type alias, expected 0, given
reveal_type(x4) # N: Revealed type is "def (*Any) -> builtins.int"
[builtins fixtures/tuple.pyi]

[case testVariadicTypedDictDeriveStar]
from typing import Tuple, Callable, Generic, TypedDict, TypeVar
from typing_extensions import TypeVarTuple

T = TypeVar("T")
Ts = TypeVarTuple("Ts")
class A(TypedDict, Generic[*Ts]):
fn: Callable[[*Ts], None]

class B(A[*Ts]):
val: bool

class C(A[*Ts], Generic[*Ts, T]):
val: T

y: B[int, str]
reveal_type(y) # N: Revealed type is "TypedDict('__main__.B', {'fn': def (builtins.int, builtins.str), 'val': builtins.bool})"
reveal_type(y["fn"]) # N: Revealed type is "def (builtins.int, builtins.str)"
reveal_type(y["val"]) # N: Revealed type is "builtins.bool"

y2: C[int, str, bool]
reveal_type(y2) # N: Revealed type is "TypedDict('__main__.C', {'fn': def (builtins.int, builtins.str), 'val': builtins.bool})"
reveal_type(y2["fn"]) # N: Revealed type is "def (builtins.int, builtins.str)"
reveal_type(y2["val"]) # N: Revealed type is "builtins.bool"

z: B[*Tuple[int, ...]]
reveal_type(z) # N: Revealed type is "TypedDict('__main__.B', {'fn': def (*builtins.int), 'val': builtins.bool})"
reveal_type(z["fn"]) # N: Revealed type is "def (*builtins.int)"
reveal_type(y["val"]) # N: Revealed type is "builtins.bool"

z2: C[*Tuple[int, ...]]
reveal_type(z2) # N: Revealed type is "TypedDict('__main__.C', {'fn': def (*builtins.int), 'val': builtins.int})"
reveal_type(z2["fn"]) # N: Revealed type is "def (*builtins.int)"
reveal_type(z2["val"]) # N: Revealed type is "builtins.int"

z3: C[*Tuple[int, ...], bool]
reveal_type(z3) # N: Revealed type is "TypedDict('__main__.C', {'fn': def (*builtins.int), 'val': builtins.bool})"
reveal_type(z3["fn"]) # N: Revealed type is "def (*builtins.int)"
reveal_type(z3["val"]) # N: Revealed type is "builtins.bool"

t: B[int, *Tuple[int, str]]
reveal_type(t) # N: Revealed type is "TypedDict('__main__.B', {'fn': def (builtins.int, builtins.int, builtins.str), 'val': builtins.bool})"

t2: C[int, *Tuple[int, str]]
reveal_type(t2) # N: Revealed type is "TypedDict('__main__.C', {'fn': def (builtins.int, builtins.int), 'val': builtins.str})"

[builtins fixtures/dict.pyi]
[typing fixtures/typing-typeddict.pyi]

[case testReturnInExceptStarBlock1]
# flags: --python-version 3.11
def foo() -> None:
Expand Down
60 changes: 60 additions & 0 deletions test-data/unit/check-typevar-tuple.test
Original file line number Diff line number Diff line change
Expand Up @@ -1159,6 +1159,66 @@ td2 = A({"fn": bad, "val": 42}) # E: Incompatible types (expression has type "C
[builtins fixtures/dict.pyi]
[typing fixtures/typing-typeddict.pyi]

[case testVariadicTypedDictDeriveUnpack]
from typing import Tuple, Callable, Generic, TypedDict, TypeVar
from typing_extensions import TypeVarTuple, Unpack

T = TypeVar("T")
Ts = TypeVarTuple("Ts")
class A(TypedDict, Generic[Unpack[Ts]]):
fn: Callable[[Unpack[Ts]], None]

class B(A[Unpack[Ts]]):
val: bool

class C(A[Unpack[Ts]], Generic[Unpack[Ts], T]):
val: T


y: B[int, str]
reveal_type(y) # N: Revealed type is "TypedDict('__main__.B', {'fn': def (builtins.int, builtins.str), 'val': builtins.bool})"
reveal_type(y["fn"]) # N: Revealed type is "def (builtins.int, builtins.str)"
reveal_type(y["val"]) # N: Revealed type is "builtins.bool"

y2: C[int, str, bool]
reveal_type(y2) # N: Revealed type is "TypedDict('__main__.C', {'fn': def (builtins.int, builtins.str), 'val': builtins.bool})"
reveal_type(y2["fn"]) # N: Revealed type is "def (builtins.int, builtins.str)"
reveal_type(y2["val"]) # N: Revealed type is "builtins.bool"

z: B[Unpack[Tuple[int, ...]]]
reveal_type(z) # N: Revealed type is "TypedDict('__main__.B', {'fn': def (*builtins.int), 'val': builtins.bool})"
reveal_type(z["fn"]) # N: Revealed type is "def (*builtins.int)"
reveal_type(y["val"]) # N: Revealed type is "builtins.bool"

z2: C[Unpack[Tuple[int, ...]]]
reveal_type(z2) # N: Revealed type is "TypedDict('__main__.C', {'fn': def (*builtins.int), 'val': builtins.int})"
reveal_type(z2["fn"]) # N: Revealed type is "def (*builtins.int)"
reveal_type(z2["val"]) # N: Revealed type is "builtins.int"

z3: C[Unpack[Tuple[int, ...]], bool]
reveal_type(z3) # N: Revealed type is "TypedDict('__main__.C', {'fn': def (*builtins.int), 'val': builtins.bool})"
reveal_type(z3["fn"]) # N: Revealed type is "def (*builtins.int)"
reveal_type(z3["val"]) # N: Revealed type is "builtins.bool"

t: B[int, Unpack[Tuple[int, str]]]
reveal_type(t) # N: Revealed type is "TypedDict('__main__.B', {'fn': def (builtins.int, builtins.int, builtins.str), 'val': builtins.bool})"

t2: C[int, Unpack[Tuple[int, str]]]
reveal_type(t2) # N: Revealed type is "TypedDict('__main__.C', {'fn': def (builtins.int, builtins.int), 'val': builtins.str})"

def test(x: int, y: str) -> None: ...
td = B({"fn": test, "val": False})
reveal_type(td) # N: Revealed type is "TypedDict('__main__.B', {'fn': def (builtins.int, builtins.str), 'val': builtins.bool})"
td2 = C({"fn": test, "val": False})
reveal_type(td2) # N: Revealed type is "TypedDict('__main__.C', {'fn': def (builtins.int, builtins.str), 'val': builtins.bool})"

def bad() -> int: ...
td3 = B({"fn": bad, "val": False}) # E: Incompatible types (expression has type "Callable[[], int]", TypedDict item "fn" has type "Callable[[], None]")
td4 = C({"fn": bad, "val": False}) # E: Incompatible types (expression has type "Callable[[], int]", TypedDict item "fn" has type "Callable[[], None]")

[builtins fixtures/dict.pyi]
[typing fixtures/typing-typeddict.pyi]

[case testFixedUnpackWithRegularInstance]
from typing import Tuple, Generic, TypeVar
from typing_extensions import Unpack
Expand Down