Skip to content

Commit

Permalink
Allow tuples of exceptions in ExceptionCounter
Browse files Browse the repository at this point in the history
`ExceptionCounter.__exit__` kicks down to `isinstance`, which accepts
either a single type or a tuple of types (or in Python 3.10, a
UnionType).  Update type annotations to reflect this.

Signed-off-by: Jakob van Santen <jvansanten@gmail.com>
  • Loading branch information
jvansanten committed Jan 19, 2023
1 parent 1fd0ded commit 8a27f80
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
6 changes: 4 additions & 2 deletions prometheus_client/context_managers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import sys
from timeit import default_timer
from types import TracebackType
from typing import Any, Callable, Optional, Type, TYPE_CHECKING, TypeVar
from typing import (
Any, Callable, Optional, Tuple, Type, TYPE_CHECKING, TypeVar, Union,
)

if sys.version_info >= (3, 8, 0):
from typing import Literal
Expand All @@ -14,7 +16,7 @@


class ExceptionCounter:
def __init__(self, counter: "Counter", exception: Type[BaseException]) -> None:
def __init__(self, counter: "Counter", exception: Union[Type[BaseException], Tuple[Type[BaseException], ...]]) -> None:
self._counter = counter
self._exception = exception

Expand Down
6 changes: 3 additions & 3 deletions prometheus_client/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import time
import types
from typing import (
Any, Callable, Dict, Iterable, List, Optional, Sequence, Type, TypeVar,
Union,
Any, Callable, Dict, Iterable, List, Optional, Sequence, Tuple, Type,
TypeVar, Union,
)

from . import values # retain this import style for testability
Expand Down Expand Up @@ -288,7 +288,7 @@ def inc(self, amount: float = 1, exemplar: Optional[Dict[str, str]] = None) -> N
_validate_exemplar(exemplar)
self._value.set_exemplar(Exemplar(exemplar, amount, time.time()))

def count_exceptions(self, exception: Type[BaseException] = Exception) -> ExceptionCounter:
def count_exceptions(self, exception: Union[Type[BaseException], Tuple[Type[BaseException], ...]] = Exception) -> ExceptionCounter:
"""Count exceptions in a block of code or function.
Can be used as a function decorator or context manager.
Expand Down

0 comments on commit 8a27f80

Please sign in to comment.