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

Pythonic and pleasant to write and to read (!)

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

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

Maybe container that allows you to write None -free code

IO marker that marks all impure operations and structures them

Result container that let’s you to get rid of exceptions

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!