Skip to main content

Make your functions return something meaningful, typed, and safe!

Project description

Returns logo


Build Status Coverage Status Documentation Status Python Version wemake-python-styleguide Checked with mypy


Make your functions return something meaningful, typed, and safe!

Features

  • Provides a bunch of primitives to write declarative business logic
  • Enforces better architecture
  • Fully typed with annotations and checked with mypy, PEP561 compatible
  • Has a bunch of helpers for better composition
  • Pythonic and pleasant to write and to read (!)
  • Support functions and coroutines, framework agnostic
  • Easy to start: has lots of docs, tests, and tutorials

Installation

pip install returns

You might also want to configure mypy correctly and install our plugin to fix this existing issue:

# In setup.cfg or mypy.ini:
[mypy]
plugins =
  returns.contrib.mypy.decorator_plugin

We also recommend to use the same mypy settings we use.

Make sure you know how to get started, check out our docs!

Contents

Maybe container

None is called the worst mistake in the history of Computer Science.

So, what can we do to check for None in our programs? You can use Optional and write a lot of if some is not None: conditions. But, having them here and there makes your code unreadable.

Or you can use Maybe container! It consists of Some and Nothing types, representing existing state and empty (instead of None) state respectively.

from typing import Optional
from returns.maybe import Maybe, maybe

@maybe  # decorator to convert existing Optional[int] to Maybe[int]
def bad_function() -> Optional[int]:
    ...

maybe_result: Maybe[float] = bad_function().map(
    lambda number: number / 2,
)
# => Maybe will return Some[float] only if there's a non-None value
#    Otherwise, will return Nothing

You can be sure that .map() method won't be called for Nothing. Forget about None-related errors forever!

Result container

Please, make sure that you are also aware of Railway Oriented Programming.

Straight-forward approach

Consider this code that you can find in any python project.

import requests

def fetch_user_profile(user_id: int) -> 'UserProfile':
    """Fetches UserProfile dict from foreign API."""
    response = requests.get('/api/users/{0}'.format(user_id))
    response.raise_for_status()
    return response.json()

Seems legit, does not it? It also seems like a pretty straight forward code to test. All you need is to mock requests.get to return the structure you need.

But, there are hidden problems in this tiny code sample that are almost impossible to spot at the first glance.

Hidden problems

Let's have a look at the exact same code, but with the all hidden problems explained.

import requests

def fetch_user_profile(user_id: int) -> 'UserProfile':
    """Fetches UserProfile dict from foreign API."""
    response = requests.get('/api/users/{0}'.format(user_id))

    # What if we try to find user that does not exist?
    # Or network will go down? Or the server will return 500?
    # In this case the next line will fail with an exception.
    # We need to handle all possible errors in this function
    # and do not return corrupt data to consumers.
    response.raise_for_status()

    # What if we have received invalid JSON?
    # Next line will raise an exception!
    return response.json()

Now, all (probably all?) problems are clear. How can we be sure that this function will be safe to use inside our complex business logic?

We really can not be sure! We will have to create lots of try and except cases just to catch the expected exceptions.

Our code will become complex and unreadable with all this mess!

Pipe example

import requests
from returns.result import Result, safe
from returns.pipeline import pipe
from returns.functions import box

def fetch_user_profile(user_id: int) -> Result['UserProfile', Exception]:
    """Fetches `UserProfile` TypedDict from foreign API."""
    return pipe(
        _make_request,
        box(_parse_json),
    )(user_id)

@safe
def _make_request(user_id: int) -> requests.Response:
    response = requests.get('/api/users/{0}'.format(user_id))
    response.raise_for_status()
    return response

@safe
def _parse_json(response: requests.Response) -> 'UserProfile':
    return response.json()

Now we have a clean and a safe and declarative way to express our business need. We start from making a request, that might fail at any moment. Then parsing the response if the request was successful. And then return the result. It all happens smoothly due to pipe function.

We also use box for handy composition.

Now, instead of returning a regular value it returns a wrapped value inside a special container thanks to the @safe decorator.

It will return Success[Response] or Failure[Exception]. And will never throw this exception at us.

And we can clearly see all result patterns that might happen in this particular case:

  • Success[UserProfile]
  • Failure[Exception]

For more complex cases there's a @pipeline decorator to help you with the composition.

And we can work with each of them precisely. It is a good practice to create Enum classes or Union sum type with all the possible errors.

IO marker

But is that all we can improve? Let's look at FetchUserProfile from another angle. All its methods look like regular ones: it is impossible to tell whether they are pure or impure from the first sight.

It leads to a very important consequence: we start to mix pure and impure code together. We should not do that!

When these two concepts are mixed we suffer really bad when testing or reusing it. Almost everything should be pure by default. And we should explicitly mark impure parts of the program.

Explicit IO

Let's refactor it to make our IO explicit!

import requests
from returns.io import IO, impure
from returns.result import Result, safe
from returns.pipeline import pipe
from returns.functions import box

def fetch_user_profile(user_id: int) -> Result['UserProfile', Exception]:
    """Fetches `UserProfile` TypedDict from foreign API."""
    return pipe(
        _make_request,
        # after box: def (Result) -> Result
        # after IO.lift: def (IO[Result]) -> IO[Result]
        IO.lift(box(_parse_json)),
    )(user_id)

@impure
@safe
def _make_request(user_id: int) -> requests.Response:
    response = requests.get('/api/users/{0}'.format(user_id))
    response.raise_for_status()
    return response

@safe
def _parse_json(response: requests.Response) -> 'UserProfile':
    return response.json()

Now we have explicit markers where the IO did happen and these markers cannot be removed.

Whenever we access FetchUserProfile we now know that it does IO and might fail. So, we act accordingly!

More!

Want more? Go to the docs! Or read these articles:

Do you have an article to submit? Feel free to open a pull request!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

returns-0.12.0.tar.gz (21.6 kB view details)

Uploaded Source

Built Distribution

returns-0.12.0-py3-none-any.whl (24.8 kB view details)

Uploaded Python 3

File details

Details for the file returns-0.12.0.tar.gz.

File metadata

  • Download URL: returns-0.12.0.tar.gz
  • Upload date:
  • Size: 21.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.0 CPython/3.7.4 Darwin/18.6.0

File hashes

Hashes for returns-0.12.0.tar.gz
Algorithm Hash digest
SHA256 b45b71ed7ae9e4f8d4ca4b1253f3d55574b495b486bd4221000d16053f1935a8
MD5 e6298294204d60b4971c83027ae1a158
BLAKE2b-256 b9287a626d69bffa03b694b2cdff64d6e244a27123696d7d1ddc9591023319b3

See more details on using hashes here.

File details

Details for the file returns-0.12.0-py3-none-any.whl.

File metadata

  • Download URL: returns-0.12.0-py3-none-any.whl
  • Upload date:
  • Size: 24.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.0 CPython/3.7.4 Darwin/18.6.0

File hashes

Hashes for returns-0.12.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0f8355aa0e67a595a3a9fc8713dc1dfe1fe9a00fe291edd3fc779033af1a6216
MD5 4872ac5c4b251cfc54feb311567a2749
BLAKE2b-256 00f5bd17c851168bdf1c9b45192c6e3b4deb1368c6f9a4b8bf3b5a985483cb62

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page