diff options
Diffstat (limited to 'buildstream/_yaml.py')
-rw-r--r-- | buildstream/_yaml.py | 20 |
1 files changed, 18 insertions, 2 deletions
diff --git a/buildstream/_yaml.py b/buildstream/_yaml.py index 85410caf2..7cf743521 100644 --- a/buildstream/_yaml.py +++ b/buildstream/_yaml.py @@ -305,6 +305,18 @@ def node_get_provenance(node, key=None, indices=None): return provenance +# Helper to use utils.sentinel without unconditional utils import, +# which causes issues for completion. +# +# Local private, but defined here because sphinx appears to break if +# it's not defined before any functions calling it in default kwarg +# values. +# +def _get_sentinel(): + from .utils import _sentinel + return _sentinel + + # node_get() # # Fetches a value from a dictionary node and checks it for @@ -326,10 +338,10 @@ def node_get_provenance(node, key=None, indices=None): # Note: # Returned strings are stripped of leading and trailing whitespace # -def node_get(node, expected_type, key, indices=None, default_value=None): +def node_get(node, expected_type, key, indices=None, default_value=_get_sentinel()): value = node.get(key, default_value) provenance = node_get_provenance(node) - if value is None: + if value is _get_sentinel(): raise LoadError(LoadErrorReason.INVALID_DATA, "{}: Dictionary did not contain expected key '{}'".format(provenance, key)) @@ -341,6 +353,10 @@ def node_get(node, expected_type, key, indices=None, default_value=None): value = value[index] path += '[{:d}]'.format(index) + # We want to allow None as a valid value for any type + if value is None: + return None + if not isinstance(value, expected_type): # Attempt basic conversions if possible, typically we want to # be able to specify numeric values and convert them to strings, |