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 Use this to satisfy most type checkers that a value that could be
16 None isn't so as to drop the Optional typing hint.
19 x: an Optional[Type] argument
22 If the Optional[Type] argument is non-None, return it.
23 If the Optional[Type] argument is None, however, raise an
26 >>> x: Optional[bool] = True
27 >>> unwrap_optional(x)
30 >>> y: Optional[str] = None
31 >>> unwrap_optional(y)
32 Traceback (most recent call last):
34 AssertionError: Argument to unwrap_optional was unexpectedly None
37 msg = 'Argument to unwrap_optional was unexpectedly None'
39 raise AssertionError(msg)
43 if __name__ == '__main__':