Fix error when initializing histogram with empty labels list #147
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Docs demonstrate initializing a metric's labels with a list, but
labelnames
is internally represented as a tuple. Iflabelnames
is truthy, it gets cast to a tuple (and a reasonable error is thrown if it can't be cast), but_MetricWrapper
simply forwardslabelnames
as-is if it is falsy.This is normally not a problem, but when initializing a histogram with an empty labels list:
this error occurs:
I see no reason why any iterable that can be cast to a tuple shouldn't reasonably be expected to work here (use case for the empty list: building up a list of labels conditionally; it's inconvenient/inconsistent to check if empty, or to cast to tuple when lists are used for all other cases).
I think a reasonable fix is to set
labelnames
to an empty tuple if it evaluates to falsy. (Possible alternative: casting even the falsy value to tuple, in case you would like to throw a clear error if someone calls the function with a falsy non-iterable?)The added test might be a bit simplistic, but it passes with this change and fails without it.