4 from typing import Any, Optional
6 logger = logging.getLogger(__name__)
9 def unwrap_optional(x: Optional[Any]) -> Any:
10 """Unwrap an Optional[Type] argument returning a Type value back.
11 If the Optional[Type] argument is None, however, raise an exception.
12 Use this to satisfy most type checkers that a value that could
13 be None isn't so as to drop the Optional typing hint.
15 >>> x: Optional[bool] = True
16 >>> unwrap_optional(x)
19 >>> y: Optional[str] = None
20 >>> unwrap_optional(y)
21 Traceback (most recent call last):
23 AssertionError: Argument to unwrap_optional was unexpectedly None
27 msg = 'Argument to unwrap_optional was unexpectedly None'
29 raise AssertionError(msg)
33 if __name__ == '__main__':