Batch geocoding. Also use format=json for the single address request to
[python_utils.git] / type_utils.py
1 #!/usr/bin/env python3
2
3 # © Copyright 2021-2022, Scott Gasch
4
5 """Utility functions for dealing with typing."""
6
7 import logging
8 from typing import Any, Optional
9
10 logger = logging.getLogger(__name__)
11
12
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.
18
19     >>> x: Optional[bool] = True
20     >>> unwrap_optional(x)
21     True
22
23     >>> y: Optional[str] = None
24     >>> unwrap_optional(y)
25     Traceback (most recent call last):
26     ...
27     AssertionError: Argument to unwrap_optional was unexpectedly None
28
29     """
30     if x is None:
31         msg = 'Argument to unwrap_optional was unexpectedly None'
32         logger.critical(msg)
33         raise AssertionError(msg)
34     return x
35
36
37 if __name__ == '__main__':
38     import doctest
39
40     doctest.testmod()