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

Allow TypedDict unpacking in Callable types #16083

Merged
merged 1 commit into from
Sep 11, 2023

Conversation

ilevkivskyi
Copy link
Member

Fixes #16082

Currently we only allow Unpack of a TypedDict when it appears in a function definition. This PR also allows this in Callable types, 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.

@github-actions
Copy link
Contributor

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)

@JelleZijlstra
Copy link
Member

So to be clear this allows Callable[[Unpack[TD]], T], where TD is a TypedDict. It's pretty obvious what that means, but I don't think any PEP prescribes it (PEP 692 says nothing about Callable[]), so I'm uncomfortable adding this to mypy without further discussion and buy-in from other type checkers.

@ilevkivskyi
Copy link
Member Author

@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.

@ilevkivskyi ilevkivskyi merged commit 9e520c3 into python:master Sep 11, 2023
17 checks passed
@ilevkivskyi ilevkivskyi deleted the fix-unpack-callable branch September 11, 2023 19:02
@tmke8
Copy link
Contributor

tmke8 commented Sep 12, 2023

This idea could even be extended to ParamSpec:

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?

@ilevkivskyi
Copy link
Member Author

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unable to use Unpack inside a Callable
4 participants