+1 to the idea in principle. It can easily be implemented in a dynamic
type checker (my area of interest).
Some thought will need to be put into how subclassing should work
(similar to the current "total" inheritance situation).
Paul
P.S. I was tempted to suggest some kind of wildcard key or key pattern
matching, but I think that deviates from the relatively simple solution
you're suggesting.
On Wed, 2021-04-07 at 15:09 +0800, NGC4039 wrote:
> For a third-party type checker, only the type of the value
> corresponding to the defined key needs to be checked, and the value
> type of the undefined key only needs to be deduced to non-existent
> (just like https://www.python.org/dev/peps/pep-0655/) or Any type.
>
>
>
> ------------------ 原始邮件 ------------------
> 发件人: "Paul Bryan" <pbryan(a)anode.ca>;
> 发送时间: 2021年4月7日(星期三) 下午3:03
> 收件人: "typing-sig"<typing-sig(a)python.org>;
>
> 主题: [Typing-sig] Re: 回复:[Typing-sig] Re: More forgiving TypedDict?
>
> Sorry, I didn't read the second part of your answer (constraining
> types for certain keys) before I replied.
>
> Q3. Are you expecting that a static type checker will constrain such
> types?
>
> Paul
>
> On Tue, 2021-04-06 at 23:59 -0700, Paul Bryan wrote:
> > Ah, non-str values in WSGI environ. Thanks. So, rephrasing my
> > second question:
> >
> > Q2: Since environ keys must be str, and as you don't want to
> > validate the keys, what is the value of using TypedDict[...] over,
> > say, dict[str, Any]?
> >
> > Paul
> >
> > On Wed, 2021-04-07 at 14:49 +0800, NGC4039 wrote:
> > > I think the value type of the undefined key should be Any.
> > >
> > > The significance of the TypedDict that allows undefined keys is
> > > that we can constrain the types of some keys and values.
> > >
> > > ------------------ 原始邮件 ------------------
> > > 发件人: "Paul Bryan" <pbryan(a)anode.ca>;
> > > 发送时间: 2021年4月7日(星期三) 下午2:42
> > > 收件人: "NGC4039"<me(a)abersheeran.com>;"typing-sig"<typing-
> > > sig(a)python.org>;
> > >
> > > 主题: [Typing-sig] Re: More forgiving TypedDict?
> > >
> > > 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/f3723029387fe7f6584c6b157b90a
> > > 50a
> > > 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(a)python.org
> > > To unsubscribe send an email to typing-sig-leave(a)python.org
> > > https://mail.python.org/mailman3/lists/typing-sig.python.org/
> > > Member address: pbryan(a)anode.ca
> > >
> >
> > _______________________________________________
> > Typing-sig mailing list -- typing-sig(a)python.org
> > To unsubscribe send an email to typing-sig-leave(a)python.org
> > https://mail.python.org/mailman3/lists/typing-sig.python.org/
> > Member address: pbryan(a)anode.ca
>
>
Ah, non-str values in WSGI environ. Thanks. So, rephrasing my second
question:
Q2: Since environ keys must be str, and as you don't want to validate
the keys, what is the value of using TypedDict[...] over, say,
dict[str, Any]?
Paul
On Wed, 2021-04-07 at 14:49 +0800, NGC4039 wrote:
> I think the value type of the undefined key should be Any.
>
> The significance of the TypedDict that allows undefined keys is that
> we can constrain the types of some keys and values.
>
> ------------------ 原始邮件 ------------------
> 发件人: "Paul Bryan" <pbryan(a)anode.ca>;
> 发送时间: 2021年4月7日(星期三) 下午2:42
> 收件人: "NGC4039"<me(a)abersheeran.com>;"typing-sig"<typing-
> sig(a)python.org>;
>
> 主题: [Typing-sig] Re: More forgiving TypedDict?
>
> 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/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
>
> _______________________________________________
> Typing-sig mailing list -- typing-sig(a)python.org
> To unsubscribe send an email to typing-sig-leave(a)python.org
> https://mail.python.org/mailman3/lists/typing-sig.python.org/
> Member address: pbryan(a)anode.ca
>
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/f3723029387fe7f6584c6b157b90a…
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