steering-council icon indicating copy to clipboard operation
steering-council copied to clipboard

PEP 692 -- Using TypedDict for more precise **kwargs typing

Open franekmagiera opened this issue 3 years ago • 7 comments

Please consider PEP 692 -- Using TypedDict for more precise **kwargs typing https://peps.python.org/pep-0692/

  • [x] The PEP has been discussed in threads listed in its Post-History header
  • [x] The PEP was annonunced on the python-dev list (link in Post-History)
  • [x] The PEP includes all relevant Suggested Sections
  • [x] The PEP includes endorsements from the projects/groups/people it helps https://github.com/python/mypy/issues/4441
  • [x] The PEP has a CODEOWNERS entry

SIG-specific:

  • [x] typing-sig PEPs: link to Guido/Jelle's summary of the typing-sig discussion: below
  • [ ] Packaging PEPs: don't file the issue here, ask the delegate (Paul Moore) on Packaging Discourse

franekmagiera avatar Sep 13 '22 15:09 franekmagiera

Jelle and I are debating who should write the summary of the typing-sig discussion.

gvanrossum avatar Sep 14 '22 04:09 gvanrossum

Jelle and I support this PEP. It has several moving parts: a grammar change, a new dunder, a new special in typing.py, and of course implementations in type checkers. However, almost the only thing that users will see is a small piece of new grammar:

class Movie(TypedDict):
    title: str
    year: int
def foo(arg1: type1, **kwds: **Movie) -> type2:
    ...

Or the pre-3.12 equivalent using typing.Unpack:

from typing_extensions import Unpack
def foo(arg1: type1, **kwds: Unpack[Movie]) -> type2:
   ...

(The dunder exists solely to be able to implement Unpack properly, no types besides TypedDict need to worry about it.)

The main objection brought in on Discourse, also heard on typing-sig, is that the use case is a bit weak: why would you write the above foo() definition instead of this?

def foo(arg1: type1, *, title: str, year: int) -> type2:
    ...

The answer is that if you're writing new code, you probably would write it that way, but if you're adding type annotations to a large code base that happens to use this pattern (for whatever reason) it would require an unreasonable amount of refactoring to do everything "right", and this syntax is a pretty good way to get such functions to be fully annotated (including type checking of individual keyword types, rejection of unknown keywords, and requiring certain keywords).

The typing-sig discussion helped the author firm up the design and the text of the PEP in various ways, and we're happy with the result. Note that the feature is already implemented in mypy and a prototype implementation also exists in pyright. The other type checkers will be able to quickly follow this lead, should the PEP be accepted.

gvanrossum avatar Sep 15 '22 04:09 gvanrossum

almost the only thing that users will see is a small piece of new grammar...The main objection brought in on Discourse, also heard on typing-sig, is that the use case is a bit weak...The answer is that if you're writing new code, you probably would write it that way, but if you're adding type annotations to a large code base that happens to use this pattern (for whatever reason) it would require an unreasonable amount of refactoring to do everything "right"

This is my sentiment as well. Very glad to have the new Unpack/** functionality, but admittedly I had hoped PEP 692 would include a variadic TypedDict (akin to TypedVarTuple) that could be used to type hint **kwargs as used in older existing (3rd-party) code bases.

It feels odd to me that we can type hint *args with a TypeVarTuple, but that similar variadic support for **kwargs is not available. The Intended Usage does feel weak to me too.

Something like TypeVarTuple for **kwargs would be more beneficial for me (personally), but I am happy this PEP is being considered 😃.

adam-grant-hendry avatar Sep 15 '22 20:09 adam-grant-hendry

The SC tracker is not the place to have this discussion. Note that my post here was to satisfy th SC’s requirement from the checklist above.

gvanrossum avatar Sep 15 '22 21:09 gvanrossum

The SC tracker is not the place to have this discussion. Note that my post here was to satisfy th SC’s requirement from the checklist above.

Apologies.

adam-grant-hendry avatar Sep 15 '22 21:09 adam-grant-hendry

Thanks! I added this to the SC agenda.

@gvanrossum, I see the word summary in the template is misleading -- what I was imagining to get from you is more of a quick confirmation/pronouncement that the PEP captures the typing-sig discussions/consensus, so the SC doesn't need to go through the archives, and we can consider it approved by “typing-sig as a whole”. The discussions should ideally be summarized in the PEP itself, rather than the SC ticket. I'll get that clarified. Anyway, thanks for the info! Hopefully writing the comment wasn't the hard part of your research.

@adam-grant-hendry, the place to discuss the PEP is linked in its Discussions-To header.

encukou avatar Sep 16 '22 09:09 encukou

Left a comment on behalf of the SC at https://discuss.python.org/t/pep-692-using-typeddict-for-more-precise-kwargs-typing/17314/52 .

brettcannon avatar Sep 21 '22 00:09 brettcannon

@gvanrossum now that the PEP has been updated to not have the syntax change, are you up for being the PEP delegate? We wanted to make sure you are still happy with the PEP even w/o the syntax.

brettcannon avatar Jan 09 '23 19:01 brettcannon

Guido just said elsewhere that he'll be mostly absent for a little while, FYI. However, https://discuss.python.org/t/pep-692-using-typeddict-for-more-precise-kwargs-typing/17314/70?u=jelle suggests he's OK with accepting the PEP without the syntax change. He did not review the PEP change (python/peps#2941).

JelleZijlstra avatar Jan 09 '23 19:01 JelleZijlstra

What Jelle said. I'm okay with accepting this PEP (though I still would have preferred it with the syntax change).

gvanrossum avatar Jan 10 '23 00:01 gvanrossum

The SC is happy to say the sans-syntax version of the PEP is accepted!

brettcannon avatar Jan 16 '23 19:01 brettcannon

@franekmagiera or @JelleZijlstra, please could you update the PEP to mark it as accepted?

https://peps.python.org/pep-0692/

hugovk avatar Feb 22 '23 06:02 hugovk

Thanks, opened python/peps#3022

JelleZijlstra avatar Feb 22 '23 07:02 JelleZijlstra