More forgiving TypedDict?
Hey, everyone. When writing a more static WSGI definition based on PEP3333, I discovered such a problem. TypedDict cannot describe the HTTP_* keys in the WSGI specification. I know that one of the original intentions of TypedDict is to prevent typing the wrong keys. But can we add an option (for example: allow_undefined=True) to allow this actual problem? Now the definition I wrote is here https://gist.github.com/abersheeran/f3723029387fe7f6584c6b157b90a50a In order to allow undefined key values to be used, such as `HTTP_HOST`, perhaps it can be used like the following (there is currently no specification, this is just the solution I envisioned). ```python class Environ(CGIRequiredDefined, CGIOptionalDefined, WSGIDefined, allow_undefined=True): """ WSGI Environ """ ``` Or is there a solution that I don’t know? --- Aber
As you point out, TypedDict was proposed to explicitly reject an invalid key. Q1: In your view, would allowing undefined keys simply allow any undefined key to be considered valid, regardless of value? Q2: Since environ keys and values must be str, and as you don't want to validate the keys, what is the value of using TypedDict[...] over, say, dict[str, str]? Paul On Wed, 2021-04-07 at 14:00 +0800, Aber wrote: Hey, everyone. When writing a more static WSGI definition based on PEP3333, I discovered such a problem. TypedDict cannot describe the HTTP_* keys in the WSGI specification. I know that one of the original intentions of TypedDict is to prevent typing the wrong keys. But can we add an option (for example: allow_undefined=True) to allow this actual problem? Now the definition I wrote is here https://gist.github.com/abersheeran/f3723029387fe7f6584c6b157b90a5 0a In order to allow undefined key values to be used, such as `HTTP_HOST`, perhaps it can be used like the following (there is currently no specification, this is just the solution I envisioned). ```python class Environ(CGIRequiredDefined, CGIOptionalDefined, WSGIDefined, allow_undefined=True): """ WSGI Environ """ ``` Or is there a solution that I don’t know? --- Aber _______________________________________________ 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/ Member address: pbryan@anode.ca
participants (2)
-
Aber
-
Paul Bryan