3 # © Copyright 2021-2022, Scott Gasch
5 """Utility functions for dealing with typing."""
8 from typing import Any, Optional
10 logger = logging.getLogger(__name__)
13 def unwrap_optional(x: Optional[Any]) -> Any:
14 """Unwrap an Optional[Type] argument returning a Type value back.
15 If the Optional[Type] argument is None, however, raise an exception.
16 Use this to satisfy most type checkers that a value that could
17 be None isn't so as to drop the Optional typing hint.
19 >>> x: Optional[bool] = True
20 >>> unwrap_optional(x)
23 >>> y: Optional[str] = None
24 >>> unwrap_optional(y)
25 Traceback (most recent call last):
27 AssertionError: Argument to unwrap_optional was unexpectedly None
31 msg = 'Argument to unwrap_optional was unexpectedly None'
33 raise AssertionError(msg)
37 if __name__ == '__main__':