projects
/
python_utils.git
/ commitdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
| commitdiff |
tree
raw
|
patch
|
inline
| side by side (parent:
334f806
)
Add a doctest to type_utils.py.
author
Scott Gasch
<
[email protected]
>
Mon, 7 Feb 2022 02:34:05 +0000
(18:34 -0800)
committer
Scott Gasch
<
[email protected]
>
Mon, 7 Feb 2022 02:34:05 +0000
(18:34 -0800)
type_utils.py
patch
|
blob
|
history
diff --git
a/type_utils.py
b/type_utils.py
index ee52444e7828c1d2b7920749a800dbf77e653983..1584597c039bc6a78900f28e3182ed64e601059d 100644
(file)
--- 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
"""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 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()