Quick proof-of-concept code:from typing import TypedDictclass ErrorOnly(TypedDict):error: strdef get_error() -> ErrorOnly:return {"error": "something bad happened", "details": "some stuff"}def process_error(err: ErrorOnly) -> str:return err["error"]process_error(get_error())Result was an error on get_error():A.py:7: error: Extra key 'details' for TypedDict "ErrorOnly"
Found 1 error in 1 file (checked 1 source file)On Wed, Mar 4, 2020 at 4:14 PM Guido van Rossum <guido@python.org> wrote:When you try the TypedDict, what error do you get? (I think totality should be true BTW.)--On Wed, Mar 4, 2020 at 16:12 Brett Cannon <brett@python.org> wrote:Looking at TypedDict it seems that it does not like the idea of superfluous keys existing in a dict. In my case I have a REST call returning JSON for an error and all I'm guaranteed is to get an "error" key. But there may be extra keys that provide more details about the specific error. Unfortunately I don't know the exhaustive list of keys so I can't use a TypedDict with `totality == False` to try to be complete.
I can't think of any way to somehow say "require/expect this one key, but other keys are OK". Am I missing anything that would let me do this?
_______________________________________________
Typing-sig mailing list -- typing-sig@python.org
To unsubscribe send an email to typing-sig-leave@python.org
https://mail.python.org/mailman3/lists/typing-sig.python.org/
--Guido (mobile)