You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
However, there is certain limit, when this starts to make sense. If you only have a few fields, the drawback of having to break the data into two classes and splitting up fields from their context (see point 3 below) outweighs the benefit of saving a few characters.
Furthermore, the problem of verbosity is oftentimes solved by ? or similar in other languages:
This adds only single character per field, and is completely acceptable.
Like we are now getting | for Union, maybe we'll get ? some day, and it will solve the problem of verbosity. Even without, I would be glad to add some visual clutter to avoid having to split up my structure into two different sections.
3. Breaks the "flow" of fields
While the order of items in a dictionary-like structure is insignificant to a machine (though we got ordered dicts by default in 3.6+), it often has meaning to a human. For example, a structure in our application looks something like:
The result is much more verbose, and more difficult to follow.
Support for total=False was discussed/added in #2632. I don't see much debate about the syntax there - the examples/suggestion in most of the messages are about doing something like MayExist[str], NotRequired[str], Checked[str], OptionalItem[str] or (the other way around) Required[str]. But then, suddenly, per TypeDict field total=False got chosen. So, I wonder if there was or has been any more discussion about supporting field-specific way to declare optional fields.
The text was updated successfully, but these errors were encountered:
I opened the ticket here, because I wasn't sure if typing would be correct place, as it pointed to typing-sig, which seemed too, uh, professional / closed for my level of competence. Also, I'm not sure if typing-sig is indexed in search engines, at least I don't recall finding anything related to this when I've searched for it every now and then (might be lack of my google skills). At least there's now ticket about it, so others can find it if they're looking for the same.
True, I didn't exactly propose an alternative. I think any of the proposed ones (Checked et al.) would do. But, I'm not aware of any discussions that you've probably already had, so maybe someone more knowledgeable can chime in.
Feature
We can define optional fields for
TypedDict
as in:Compare this to some other languages (like Typescript):
The problem is with syntax here - the Python version is less intuitive, more verbose (for short declarations) and it breaks the "flow" of fields.
Pitch
1. Less intuitive
When you think about:
Why do you need to split up the required and optional fields of one single structure into 2 classes? It's just... not nice.
2. More verbose
While I understand that Python syntax saves characters when there are lots of optional fields, which would be represented like this:
Omitting
Checked[]
we save 9 chars per field and the result looks nicer as:However, there is certain limit, when this starts to make sense. If you only have a few fields, the drawback of having to break the data into two classes and splitting up fields from their context (see point 3 below) outweighs the benefit of saving a few characters.
Furthermore, the problem of verbosity is oftentimes solved by
?
or similar in other languages:This adds only single character per field, and is completely acceptable.
Like we are now getting
|
forUnion
, maybe we'll get?
some day, and it will solve the problem of verbosity. Even without, I would be glad to add some visual clutter to avoid having to split up my structure into two different sections.3. Breaks the "flow" of fields
While the order of items in a dictionary-like structure is insignificant to a machine (though we got ordered dicts by default in 3.6+), it often has meaning to a human. For example, a structure in our application looks something like:
As you can see, there are sort of field "groups" there. This grouping/ordering must be broken when defining a TypedDict for the structure:
The result is much more verbose, and more difficult to follow.
Support for
total=False
was discussed/added in #2632. I don't see much debate about the syntax there - the examples/suggestion in most of the messages are about doing something likeMayExist[str]
,NotRequired[str]
,Checked[str]
,OptionalItem[str]
or (the other way around)Required[str]
. But then, suddenly, perTypeDict
fieldtotal=False
got chosen. So, I wonder if there was or has been any more discussion about supporting field-specific way to declare optional fields.The text was updated successfully, but these errors were encountered: