Allow TypedDict unpacking in Callable types#16083
Conversation
|
Diff from mypy_primer, showing the effect of this PR on open source code: graphql-core (https://github.com/graphql-python/graphql-core): typechecking got 1.06x slower (313.1s -> 331.1s)
(Performance measurements are based on a single noisy sample)
|
|
So to be clear this allows |
|
@JelleZijlstra I don't like the model where people outside of mypy have to say anything about whether a given feature should be in mypy or not. We can coordinate on details of semantics of certain constructs, to achieve better compatibility (especially for use in typeshed), but this doesn't really apply here, since semantics is quite obvious. |
|
This idea could even be extended to P = ParamSpec("P")
class C(Generic[P]):
def __init__(self, f: Callable[P, None]): ...
class Args(TypedDict):
x: int
y: str
def f(*, x: int, y: str) -> None: ...
c: C[[Unpack[Args]]] = C(f) # OK
d: C[[int, str]] = = C(f) # error because `f` expects keyword arguments? |
|
@tmke8 Yes, this is totally possible. It however may be a bit trickier to implement (and I am busy with other things now). You can open a separate issue for this. |
Fixes #16082
Currently we only allow
Unpackof a TypedDict when it appears in a function definition. This PR also allows this inCallabletypes, similarly to how we do this for variadic types.Note this still doesn't allow having both variadic unpack and a TypedDict unpack in the same
Callable. Supporting this is tricky, so let's not so this until people will actually ask for this. FWIW we can always suggest callback protocols for such tricky cases.