Add doctests to some of this stuff.
[python_utils.git] / type_utils.py
1 #!/usr/bin/env python3
2
3 import logging
4 from typing import Any, Optional
5
6 logger = logging.getLogger(__name__)
7
8
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.
14     """
15     if x is None:
16         msg = 'Argument to unwrap_optional was unexpectedly None'
17         logger.critical(msg)
18         raise AssertionError(msg)
19     return x