Menu

1 / 11

Stop Letting Python Functions Lie to You: Introducing explicit-result

DEV Community·chukwunwike obodo·6 months ago
#u61G4w2d
Reading 0:00
15s threshold

Python is beautiful, readable, and highly expressive. But its built-in error handling mechanism, exceptions, has a fundamental flaw: Exceptions are invisible.

Take a look at this standard Python function signature:

def get_user ( user_id : int ) -> User : ...

Enter fullscreen mode

Exit fullscreen mode

What does this signature tell you? It promises to return a User . But does it? What happens if the database connection drops? What if user_id doesn't exist? What if there's a permission issue?

You can’t know without digging into the implementation, hoping the docstrings are accurate, or waiting for a surprise stack trace in production. The type system provides zero guarantees about what can fail.

That’s where explicit-result comes in.

The Philosophy of explicit-result

explicit-result is a zero-dependency, fully-typed Python library that brings Result and Option primitives to your Python code. It is designed around three core ideas:

Errors should be part of the contract.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.