diff --git a/returns/maybe.py b/returns/maybe.py index 1e04a3e20..938f8ff25 100644 --- a/returns/maybe.py +++ b/returns/maybe.py @@ -303,6 +303,9 @@ def from_optional( return _Nothing(inner_value) return Some(inner_value) + def __bool__(self) -> bool: + """Convert (or treat) an instance of ``Maybe`` as a boolean.""" + @final class _Nothing(Maybe[Any]): @@ -378,6 +381,10 @@ def failure(self) -> None: """Returns failed value.""" return self._inner_value + def __bool__(self): + """Returns ``False``.""" + return False + @final class Some(Maybe[_ValueType_co]): @@ -435,6 +442,15 @@ def failure(self): """Raises exception for successful container.""" raise UnwrapFailedError(self) + def __bool__(self): + """ + Returns ``True```. + + Any instance of ``Something`` is treated + as ``True``, even ``Something(None)``. + """ + return True + #: Public unit value of protected :class:`~_Nothing` type. Nothing: Maybe[Never] = _Nothing() diff --git a/tests/test_maybe/test_maybe_conversions.py b/tests/test_maybe/test_maybe_conversions.py new file mode 100644 index 000000000..3173a4764 --- /dev/null +++ b/tests/test_maybe/test_maybe_conversions.py @@ -0,0 +1,23 @@ +from returns.maybe import Nothing, Some + + +def test_some_is_true() -> None: + """Ensures that ``Something(...)`` is ``True`` when treated as a boolean.""" + assert bool(Some(123)) + assert bool(Some('abc')) + + +def test_nothing_is_false() -> None: + """Ensures that ``Nothing`` is ``False`` when treated as a boolean.""" + assert not bool(Nothing) + + +def test_some_none_is_true() -> None: + """ + Ensures that ``Something(None)`` is ``True`` when treated as a boolean. + + See for the discussion + of this design choice. + """ + assert bool(Some(None)) + assert bool(Some(Nothing))