3 """Utility functions for dealing with typing."""
6 from typing import Any, Optional
8 logger = logging.getLogger(__name__)
11 def unwrap_optional(x: Optional[Any]) -> Any:
12 """Unwrap an Optional[Type] argument returning a Type value back.
13 If the Optional[Type] argument is None, however, raise an exception.
14 Use this to satisfy most type checkers that a value that could
15 be None isn't so as to drop the Optional typing hint.
17 >>> x: Optional[bool] = True
18 >>> unwrap_optional(x)
21 >>> y: Optional[str] = None
22 >>> unwrap_optional(y)
23 Traceback (most recent call last):
25 AssertionError: Argument to unwrap_optional was unexpectedly None
29 msg = 'Argument to unwrap_optional was unexpectedly None'
31 raise AssertionError(msg)
35 if __name__ == '__main__':