X-Git-Url: https://wannabe.guru.org/gitweb/?a=blobdiff_plain;f=type_utils.py;h=e760dba90412355579ea042bf0fa760960d644c5;hb=e46158e49121b8a955bb07b73f5bcf9928b79c90;hp=1584597c039bc6a78900f28e3182ed64e601059d;hpb=413d28443c7308414e8d283b9c5b9037463274f3;p=python_utils.git diff --git a/type_utils.py b/type_utils.py index 1584597..e760dba 100644 --- a/type_utils.py +++ b/type_utils.py @@ -1,5 +1,9 @@ #!/usr/bin/env python3 +# © Copyright 2021-2022, Scott Gasch + +"""Utility functions for dealing with typing.""" + import logging from typing import Any, Optional @@ -8,9 +12,16 @@ logger = logging.getLogger(__name__) 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 typing hint. + Use this to satisfy most type checkers that a value that could be + None isn't so as to drop the Optional typing hint. + + Args: + x: an Optional[Type] argument + + Returns: + If the Optional[Type] argument is non-None, return it. + If the Optional[Type] argument is None, however, raise an + exception. >>> x: Optional[bool] = True >>> unwrap_optional(x) @@ -21,7 +32,6 @@ def unwrap_optional(x: Optional[Any]) -> Any: 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'