Update to PEP 692's specification

Hey, based on developer feedback I wanted to propose changes to PEP 692. Here is a link to the PR with proposed changes.

In a nutshell, from type checkers point of view, the function signature typed with Unpack should be equivalent to its explicitly expanded form. Therefore:

class Movie(TypedDict):
    name: str
    year: int

def foo(**kwargs: Unpack[Movie]): ...

should be equivalent to:

def foo(*, name: str, year: int): ...

This creates a more robust specification for the feature. Alsot it is a lot simpler to remember and reason about and should not jeopardize type safety. Ivan’s comment goes into a bit more depth on the issue.

Also, when it comes to the Passing kwargs inside a function to another function from section - we cannot expect type checkers to treat the same function call differently depending on the context, therefore this feature should be deleted from the specification. This, however, should not degrade the value of the rest of the proposal.

Let me know what you think!

Any chance someone had some time to take a look at the proposal?

PEP 692 was accepted and is marked Final, so it probably doesn’t make sense to amend the PEP at this stage. PEPs are meant to be historical documents, not evolving documentation.

If you think that a clarification is needed, I recommend doing so in the typing spec.

Here’s the process we’re using for such updates.

1 Like

I don’t think the specification for Unpack should change for a regular TypedDict in this way, because it introduces safety holes.

But for a closed TypedDict, as made possible with the changes proposed in PEP 728, we can make this bidirectional equivalency, because we can now be sure that there will be no additional keys. Incidentally PEP 728 should allow Unpack with TypedDict to be expressive enough to model every possible layout of keyword only arguments.