@@ -107,20 +107,24 @@ just to catch the expected exceptions.
107107
108108Our code will become complex and unreadable with all this mess!
109109
110- ### Pipeline example
110+ ### Pipe example
111111
112112``` python
113113import requests
114- from returns.result import Result, pipeline, safe
114+ from returns.result import Result, safe
115+ from returns.pipeline import pipe
116+ from returns.functions import lift
115117
116118class FetchUserProfile (object ):
117119 """ Single responsibility callable object that fetches user profile."""
118120
119- @pipeline
120121 def __call__ (self , user_id : int ) -> Result[' UserProfile' , Exception ]:
121- """ Fetches UserProfile dict from foreign API."""
122- response = self ._make_request(user_id).unwrap()
123- return self ._parse_json(response)
122+ """ Fetches `UserProfile` TypedDict from foreign API."""
123+ return pipe(
124+ user_id,
125+ self ._make_request,
126+ lift(self ._parse_json),
127+ )
124128
125129 @safe
126130 def _make_request (self , user_id : int ) -> requests.Response:
@@ -145,20 +149,14 @@ decorator.
145149It will return [ Success[ Response] or Failure[ Exception]] ( https://returns.readthedocs.io/en/latest/pages/result.html ) .
146150And will never throw this exception at us.
147151
148- When we will need raw value, we can use ` .unwrap() ` method to get it.
149- If the result is ` Failure[Exception] `
150- we will actually raise an exception at this point.
151- But it is safe to use ` .unwrap() ` inside
152- [ @pipeline ] ( https://returns.readthedocs.io/en/latest/pages/functions.html#returns.functions.pipeline )
153- functions.
154- Because it will catch this exception
155- and wrap it inside a new ` Failure[Exception] ` !
156-
157152And we can clearly see all result patterns
158153that might happen in this particular case:
159154- ` Success[UserProfile] `
160155- ` Failure[Exception] `
161156
157+ For more complex cases there's a [ @pipeline ] ( https://returns.readthedocs.io/en/latest/pages/functions.html#returns.functions.pipeline )
158+ decorator to help you with the composition.
159+
162160And we can work with each of them precisely.
163161It is a good practice to create ` Enum ` classes or ` Union ` sum type
164162with all the possible errors.
@@ -188,16 +186,21 @@ Let's refactor it to make our
188186``` python
189187import requests
190188from returns.io import IO , impure
191- from returns.result import Result, pipeline, safe
189+ from returns.result import Result, safe
190+ from returns.pipeline import pipe
191+ from returns.functions import lift, lift_io
192192
193193class FetchUserProfile (object ):
194194 """ Single responsibility callable object that fetches user profile."""
195195
196- @pipeline
197196 def __call__ (self , user_id : int ) -> IO [Result[' UserProfile' , Exception ]]:
198- """ Fetches UserProfile dict from foreign API."""
199- return self ._make_request(user_id).map(
200- lambda response : self ._parse_json(response.unwrap())
197+ """ Fetches `UserProfile` TypedDict from foreign API."""
198+ return pipe(
199+ user_id,
200+ self ._make_request,
201+ # lift: def (Result) -> Result
202+ # lift_io: def (IO[Result]) -> IO[Result]
203+ lift(box(self ._parse_json), IO ),
201204 )
202205
203206 @impure
@@ -208,7 +211,7 @@ class FetchUserProfile(object):
208211 return response
209212
210213 @safe
211- def _parse_json (self ,response : requests.Response) -> ' UserProfile' :
214+ def _parse_json (self , response : requests.Response) -> ' UserProfile' :
212215 return response.json()
213216```
214217
0 commit comments