diff options
| author | Scott Gasch <[email protected]> | 2022-02-06 18:34:05 -0800 |
|---|---|---|
| committer | Scott Gasch <[email protected]> | 2022-02-06 18:34:05 -0800 |
| commit | 413d28443c7308414e8d283b9c5b9037463274f3 (patch) | |
| tree | a5ceec84d4ff8d3333afeee9bb44ae9c245c1da5 /type_utils.py | |
| parent | 334f806094fa220bd6518b8b30b9d0f9d50daee2 (diff) | |
Add a doctest to type_utils.py.
Diffstat (limited to 'type_utils.py')
| -rw-r--r-- | type_utils.py | 19 |
1 files changed, 18 insertions, 1 deletions
diff --git a/type_utils.py b/type_utils.py index ee52444..1584597 100644 --- a/type_utils.py +++ b/type_utils.py @@ -10,10 +10,27 @@ def unwrap_optional(x: Optional[Any]) -> Any: """Unwrap an Optional[Type] argument returning a Type value back. If the Optional[Type] argument is None, however, raise an exception. Use this to satisfy most type checkers that a value that could - be None isn't so as to drop the Optional. + be None isn't so as to drop the Optional typing hint. + + >>> x: Optional[bool] = True + >>> unwrap_optional(x) + True + + >>> y: Optional[str] = None + >>> unwrap_optional(y) + Traceback (most recent call last): + ... + AssertionError: Argument to unwrap_optional was unexpectedly None + """ if x is None: msg = 'Argument to unwrap_optional was unexpectedly None' logger.critical(msg) raise AssertionError(msg) return x + + +if __name__ == '__main__': + import doctest + + doctest.testmod() |
