Open MiaAltieri opened 1 year ago
Thanks for this. I think you're right it would be a nicer API to return None
in the case where a config key isn't present (and it matches Python's dict.get
behaviour). However, strictly speaking this wouldn't be a backwards-compatible change, because some callers might be relying on the SnapError
to be raised in this case. And I don't think it's worth creating a new major version for a relatively trivial change this like.
One possibility to keep it backwards compatible is to add a default
argument that, if provided, would return that value instead of raising (the default would still be to raise SnapError
). Something like so:
_raise = object() # unique object to signal a "raise exception" default
...
def get(self, key, default=_raise) -> str:
"""Fetch a snap configuration value.
Args:
key: the key to retrieve
default: if specified, return that value if the configuration key is not found
(the default is to raise SnapError)
Raises:
SnapError: if default not provided and the configuration key is not found
"""
if default is _raise:
return self._snap("get", [key]).strip()
else:
try:
return self._snap("get", [key]).strip()
except SnapError:
return default
Hi @benhoyt my apologies for missing this message - that implementation sounds perfect thank you!
Ben is out for a couple of weeks - @MiaAltieri are you able to get a PR together and we can get it through review?
Problem
When performing snap get:
this will cause an error if
monitor-uri
is not setIdeal usage
Current usage
Desired Behavior
Return
None
if value is not set