From 11695a52c8d545684d1ed33c8efe804ddc7d691c Mon Sep 17 00:00:00 2001 From: addielarue <173251542+addielaruee@users.noreply.github.com> Date: Sun, 6 Dec 2026 11:28:25 +0530 Subject: [PATCH] Explain how to choose between bind functions Show how one Result-returning callback composes with Result and IOResult, including both failure paths. Correct the settings link that prevented the strict documentation build from publishing the example. --- CHANGELOG.md | 4 ++++ docs/pages/pointfree.rst | 30 ++++++++++++++++++++++++++++-- returns/contrib/hypothesis/laws.py | 4 ++-- 3 files changed, 34 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 435f27f08..8f93f8fe9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,10 @@ See [0Ver](https://0ver.org/). - Fix type parameter variance of `Law{1,2,3}`, `Unwrappable`, `Contextable`, and `Trampoline` +### Documentation + +- Explain how to choose between `bind` and the `bind_*` functions + ## 0.30.0 ### Features diff --git a/docs/pages/pointfree.rst b/docs/pages/pointfree.rst index f41f14d8e..ebe344861 100644 --- a/docs/pages/pointfree.rst +++ b/docs/pages/pointfree.rst @@ -148,10 +148,36 @@ but how can we do it independently? >>> assert pointfree.bind(index_of_1)(container) == Some(1) >>> # This produces the same result, but in a different order -That's it! +Choosing a bind function +~~~~~~~~~~~~~~~~~~~~~~~~ -We also have a long list of other ``bind_*`` functions, like: +Look at the return type of the function you want to call. +Use ``bind`` when it returns the same kind of container you already have. +For example, a function returning ``Result`` can be passed to ``bind`` +on a ``Result``. To use that same function on an ``IOResult``, +choose ``bind_result``. The suffix describes what the function returns, +and the outer container stays an ``IOResult``. +.. code:: python + + >>> from returns import pointfree + >>> from returns.io import IOFailure, IOSuccess + >>> from returns.result import Failure, Result, Success + + >>> def validate_port(port: int) -> Result[int, str]: + ... if 1 <= port <= 65535: + ... return Success(port) + ... return Failure('invalid port') + + >>> assert pointfree.bind(validate_port)(Success(443)) == Success(443) + >>> validate_io_port = pointfree.bind_result(validate_port) + >>> assert validate_io_port(IOSuccess(443)) == IOSuccess(443) + >>> assert validate_io_port(IOSuccess(0)) == IOFailure('invalid port') + >>> assert validate_io_port(IOFailure('read error')) == IOFailure('read error') + +The available variants depend on which methods the outer container supports: + +- ``bind_optional`` to bind functions returning a value or ``None`` on a ``Maybe`` - ``bind_io`` to bind functions returning ``IO`` container - ``bind_result`` to bind functions returning ``Result`` container - ``bind_ioresult`` to bind functions returning ``IOResult`` container diff --git a/returns/contrib/hypothesis/laws.py b/returns/contrib/hypothesis/laws.py index b9b8a7854..382e34021 100644 --- a/returns/contrib/hypothesis/laws.py +++ b/returns/contrib/hypothesis/laws.py @@ -37,8 +37,8 @@ class Settings: settings_kwargs: dict[str, Any] """Settings directly passed on to ``hypothesis``. - We support all kwargs from ``@settings``, see ``@settings docs - ``_. + We support all kwargs from ``@settings``, see the `settings documentation + `_. """ use_init: bool