Difficult to use Optional("key", None)
See original GitHub issueGreat project, just a few usability issues.
Assume the following schema:
import strictyaml as s
s.load("a: 8", s.Map({
s.Optional("a", 1): s.Int(),
s.Optional("key_a", None): s.Str(),
s.Optional("key_b", {}): s.EmptyDict() | s.Map({})
}))
A few issues with usability arise:
- Why is
a: 8
required from the YAML file? It seems like a blank file (or one consisting only of comments) should be perfectly valid with this schema. - The result is
YAML(OrderedDict([('a', 8), ('key_b', {})]))
. It is confusing thatkey_a
is missing entirely from the results, despite having a default value ofNone
. A common pattern for fixing this in Python is to defineOptional
using e.g.:
missing = {} # Creates a unique object
class Optional:
def __init__(self, key, default=missing):
...
# Later
if optional.default is missing:
# User specified no default, OK not to include in output
In this way, Optional
with no default specified can result in the absence of the key, while Optional
with any default would always be present in the output.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:11 (8 by maintainers)
Top Results From Across the Web
TypedDict does not allow optional keys? - Stack Overflow
I read that this class will render all the keys within as required, and a way to make ALL of them as optional...
Read more >26 Reasons Why Using Optional Correctly Is Not Optional
Do not use Optional as fields or in methods' (including setters ) arguments. This is another usage against Optional intention. Optional wraps ...
Read more >PEP 655 – Marking individual TypedDict items as required or ...
Attempting to use any synonym of “optional” to mark potentially-missing keys (like Missing[] ) would be too similar to Optional[] and be easy...
Read more >Design in Dictionary with optional… | Apple Developer Forums
I can't think of a meaningful distinction between nil (key not in the dictionary) and nil (stored). They're both nil, right? If you...
Read more >Dictionary with optional values - Using Swift
The comment for Dictionary subscript says: /// Access the value associated with the given key. /// /// Reading a key that is not...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I’ve admittedly only skimmed this thread but I would like to echo point 2 made by @wwoods in the initial comment: Having non-provided default values be missing in the YAML mapping is … inconvenient.
Effectively this means that my code is littered with the equivalent of
It’s not tragic but it is unnecessary clutter. In particular it means that I can’t use values from the mapping directly in expressions, I first have to assign them to a local variable. For instance, I’d like to be able to write (actual code)
However, this crashes. Instead I’d have to write
Hmm, so you’re saying that all of the
Empty*
schemas were built with empty strings in mind, rather than e.g. the dict which is returned being empty. That makes a bit more sense.Absolutely, but there’s no need to do so at the cost of convenience when developing with StrictYAML. My main issue with usability is that
Map({...})
refuses to validate{}
, e.g. when theMap
is the value attached to anOptional('key_to_map', {})
, and theMap
has all optional keys:If you don’t want to support a blank string in this case, fine, but I don’t think it should be necessary to add
EmptyDict
to the schema in this case.