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

The SC is happy to say that we accept the PEP (w/ the syntax changes removed)!

And to reiterate, the SC is not wholly against the syntax change, but we do feel it deserves a separate PEP for a more thorough discussion.

6 Likes

The PEP mentions

Also, for the benefit of IDEs and documentation pages, functions that are part of the public API should prefer explicit keyword parameters whenever possible.

To me this indeed seems like a good reason to still prefer duplication over using this new feature. But I wonder if maybe IDEs/LSPs will be able to do the unpacking for the user, i.e. show the TypedDict fields as normal keyword parameters (completely hiding the use of **kwargs), and auto-complete the parameters.

@erictraut (hope you don’t mind my question), do you think this will be possible to do in the Pylance LSP, or is there something preventing it?

The current implementation in Pylance does not show the TypedDict fields as normal keyword parameters, nor does it auto-complete the parameters if PEP 692 is used. These are both theoretically possible to do and (IMO) worth considering. User feedback plays heavily into decisions made by the Pylance team, so if this is something you’d like to see, I encourage you to submit an enhancement request in the pylance-release issue tracker.

I’m not aware of any library using this feature yet, so no need from me, but it’s good to know that it’s possible, thanks.

One thing that will definitely be lost is seeing the value of the parameter defaults in the hover/documentation, so NotRequired fields will need to be shown as param=... instead of param=100, I don’t think there’s a way around that.

1 Like