From 9a47ca8990cc63aa14061ee9ddbbd0eb7fc49e79 Mon Sep 17 00:00:00 2001 From: nyanpasu64 Date: Fri, 16 Nov 2018 23:52:53 -0800 Subject: [PATCH] Alter coalesce() to raise exception if all None --- ovgenpy/util.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/ovgenpy/util.py b/ovgenpy/util.py index 6cfb2a0..4eb17a5 100644 --- a/ovgenpy/util.py +++ b/ovgenpy/util.py @@ -2,7 +2,7 @@ import os from contextlib import contextmanager from itertools import chain from pathlib import Path -from typing import Callable, Tuple, TypeVar, Iterator, Union +from typing import Callable, Tuple, TypeVar, Iterator, Union, Optional import numpy as np @@ -11,21 +11,22 @@ def ceildiv(n, d): return -(-n // d) -def coalesce(*args): +T = TypeVar('T') + + +def coalesce(*args: Optional[T]) -> T: if len(args) == 0: raise TypeError('coalesce expected 1 argument, got 0') for arg in args: if arg is not None: return arg - return args[-1] + raise TypeError('coalesce() called with all None') def obj_name(obj) -> str: return type(obj).__name__ -T = TypeVar('T') - # Adapted from https://github.com/numpy/numpy/issues/2269#issuecomment-14436725 def find(a: 'np.ndarray[T]', predicate: 'Callable[[np.ndarray[T]], np.ndarray[bool]]', chunk_size=1024) -> Iterator[Tuple[Tuple[int], T]]: