question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Difficult to use Optional("key", None)

See original GitHub issue

Great 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:

  1. 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.
  2. The result is YAML(OrderedDict([('a', 8), ('key_b', {})])). It is confusing that key_a is missing entirely from the results, despite having a default value of None. A common pattern for fixing this in Python is to define Optional 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:open
  • Created 4 years ago
  • Reactions:1
  • Comments:11 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
klmrcommented, Jul 3, 2019

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

name = data['name'] if 'name' in data else None

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)

if not test['disabled'] or test['name'] in self.include_tests:
    self.enable(test)

However, this crashes. Instead I’d have to write

if not test['disabled'] or ('name' in test and test['name'] in self.include_tests):
    self.enable(test)
1reaction
wwoodscommented, Jun 23, 2019

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.

I think the distinction between an empty mapping and a nominally blank value is somewhat confusing for a non-developer, and StrictYAML, unlike YAML, is supposed to be super simple for non developers to use.

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 the Map is the value attached to an Optional('key_to_map', {}), and the Map has all optional keys:

import strictyaml as s
s.load('a: 8', s.Map({'a': s.Int(), s.Optional('b', {}): s.Map({s.Optional('key', 8): s.Int()})}))
# InvalidOptionalDefault: Optional default for 'b' failed validation

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found