Repository navigation
Open
Labels
Description
I tried to implement something like Rust's Result type. The interesting thing is that it has a linting attribute called #[must_use] which makes the linter enforce that the value is not ignored when used as a return type.
I think this could be very helpful when implementing functions that returns status and you want to ensure that it is not unhandled.
Consider the following toy implementation:
from typing import cast, Generic, Optional, TypeVar, Union
from whereever import MustUse
T = TypeVar("T")
E = TypeVar("E")
# We inherit from MustUse to ensure this type isn't ignore
class Result(Generic[T, E], MustUse):
def __init__(self, value: Union[T, E], is_ok: bool):
self._value = value
self.is_ok = is_ok
@classmethod
def ok(cls, value: T) -> "Result[T, E]":
return cls(value, is_ok=True)
@classmethod
def error(cls, value: E) -> "Result[T, E]":
return cls(value, is_ok=True)
@property
def is_error(self) -> bool:
return not self.is_ok
def get_value(self) -> Optional[T]:
if self.is_ok:
return cast(T, self._value)
return None
def get_error(self) -> Optional[E]:
if not self.is_ok:
return cast(E, self._value)
return None
def f(success: bool) -> Result[str, int]:
if not success:
return Result[str, int].error(123)
return Result[str, int].ok("Hello")
# No warning is raised since the value is saved to a variable
a = f(True)
# No warning is raised since we convert the Result into an Optional
f(False).get_value()
# This will raise a warning since we never do anything with the Result like:
# error: unused return value with attribute MustUse
# or
# error: return value of function call must be used
f(True)Reacted by Shawn McElroy, oakkitten, Justin Austin, Keji Li, Finite State Machine, Adrien Pensart, Jonah Y-M, Fritz Obermeyer, Siddhartha Gandhi, Tamika Nomara and 8 more
Activity
ilevkivskyi commentedon Jun 5, 2019
In principle I like this idea, although it is probably relatively low priority. If you want this to end up in
typing, then you need to discuss it on typing tracker and/or typing-sig mailing list.JelleZijlstra commentedon Jun 5, 2019
This is similar to #2499, although the proposed implementation mechanism here is different.
bluetech commentedon Jun 5, 2019
Not relevant for the issue, just wanted to say you can get closer to Rust with something like the following:
result.py
Example:
ilevkivskyi commentedon Jun 5, 2019
@JelleZijlstra Yeah, this may be one of the possible ways to move towards #2499
JukkaL commentedon Jun 6, 2019
Here is another random idea about handling errors using return values with more minimal syntax (at the cost of requiring some special-purpose primitives):
is_error(x)would behave mostly likeisinstance(x, Exception), but mypy would require that eachResultreturn value is handled withis_errorsomewhere in the function body or returned (errors can't be just ignored).In Python 3.8
foocould be written like this:bluetech commentedon Jun 6, 2019
@JukkaL This would be neat. One problem that comes up is, what if
Tis itself an exception value? :)JukkaL commentedon Jun 6, 2019
It would have to be wrapped in another object, something like
Result[X[Exception], RuntimeError], for some generic classX. Since this is rare, I like it better than wrapping all return values.refi64 commentedon Jun 7, 2019
One thing IMO worth noting, once you introduce monadic types you start having some oddities with e.g. callbacks, where the given and expected return types don't necessarily match, or a lot of utility unwrapping functions. Languages built around monads (e.g. Haskell) don't really have this type of issue because the style of the language itself blends with monads really well.
Maybe what Swift does would be worth a look? If what we really want is checked error handling that's not as verbose as Java's
throws, Swift allows you to simply addthrowsand it infers the error types. Then, you have to use special syntax at the call site if you want the error to propagate up, in order to acknowledge its presence.Of course I just have to plug my old error handling article at the same time. 😂 I think Swift largely did better than my idea did, but if we're really looking at return values it's not half bad.
JukkaL commentedon Jun 10, 2019
@refi64 I once thought about borrowing the error handling mechanism from Swift. I don't remember if I wrote anything about this.
Here is another idea inspired by Swift and @refi64's article:
Random notes:
ZeroDivisionError,IndexError, etc.). These would work as currently.t | Errorwheretis the normal return type.Errormeans that the function may raise some checked exception type(s).check(...). At runtime,check(x)just returns the argument.check(...)is required.Errorreturn type is needed.def f(...) -> t | Error[FooError, BarError].check(main())at module top level results in a traceback on error.t | Errorsyntax.foo.read()orfoo.write()should raise checked exceptions.print()probably shouldn't.RuntimeErrorcan be used as checked or unchecked, depending on context. In user code each raise would be checked by default, but we'd probably have something likeraise_unchecked(e)to raise an arbitrary unchecked exception.refi64 commentedon Jun 11, 2019
Honestly that looks amazing, though I feel like the semantics around this being, or at least looking like, a return value seem to be a bit confusing.
E.g.
Given the overall style of the proposal, maybe something simply like
@raisesmight work a bit better?As for the stuff like RuntimeError, maybe declare that only Exception subclasses are checked, not all Error subclasses? That way stuff like SystemExit and AssertionError don't make things weird.
JukkaL commentedon Jun 11, 2019
No,
check(<expr>)would be needed if<expr>can raise an unchecked exception. It's the callspam()that can raise an exception, not reading the value of variablex.Yeah, that is an option, but it wouldn't interact as nicely with
Callable[...]types, in my opinion. For example, something like this could be supported (it may be too difficult to infer exception types automatically here):Not sure how we'd represent this using the decorator.
Also, in some real-world code the majority of functions might raise unchecked exceptions, and the
@raisesdecorators could make the code look kind of noisy. I don't have a strong opinion on this, however. It would be useful to annotate some actual code using checked exceptions to get a better idea of how these would look in practice.Most built-in exception classes, including
RuntimeError,IndexErrorandAssertionErrorare derived fromException, and it should be possible to treat at least some of these as unchecked. It may be reasonable that exceptions not derived fromExceptioncannot be raised as checked exceptions, but I'm not sure if this would actually be helpful. Somebody could plausibly want to treatSystemExitas a checked exception, even though it's not derived fromException. I'd rather treat all exception types identically at least until we understand the practicalities better.refi64 commentedon Jun 12, 2019
Hmm true, I understand that, it just seems kinda odd that it looks like a return type but doesn't act one.
Ugh right, this is what I get for writing comments on bug reports around midnight...
remdragon commentedon Aug 13, 2019
I would be happy to volunteer to annotate a real project to test this. I don’t have any significant open source projects but I wouldn’t mind testing this on my internal code and sharing it with a few individuals under NDA as a test bed for this idea.
Personally I feel like all subclasses of Exception should be checked, as missing them leads to unexpected application errors. My experience with IndexError in particular is that I rarely want that to bubble out of a function because I lose sight of its meaning. I will usually convert it into a more meaningful exception if I need it to travel up further.
With regards to syntax, I like raises better than the pipe proposal even though it doesn’t jive as well with Callable. My proposal with Callable would be to add an optional 3rd element to it, like so:
FooFunc = Callable[[],None,Union[IndexError,IOError]]
I would even propose a step farther and propose something like the following for readability (I don’t know if Callable already supports kwargs):
FooFunc = Callable[
params=[],
returns=None,
raises=Union[IndexError,IOError]
]
As far as @raises goes, multiple types could be passed as *args but for a nod towards Callable maybe it would be better to require this:
@raises ( Union[IndexError,IOError] )
def foo() -> None:
pass
I had another idea about how we could solve this is create the ability to apply a Callable to a function definition, like so:
def foo(): # type: FooFunc
pass
This disadvantage here is it makes it harder to read a function’s signature in context but it’s less work to change function signatures. I’m not convinced this is a net win but I thought I’d throw it out there anyway.
[Edited because autocorrect sucks]
remdragon commentedon Aug 22, 2019
Would someone please advise: How could I go about putting a bounty on this feature?
ilevkivskyi commentedon Aug 22, 2019
@remdragon
I don't think we support anything like this, but maybe others know @gvanrossum @JukkaL
gvanrossum commentedon Aug 22, 2019
No, there are no bounties here.
stereobutter commentedon Oct 9, 2019
I really like the monadic way or error handling as demonstrated by https://github.com/dry-python/returns. As someone else already noted this requires some machinery for wrapping, unwrapping and composing functions written in this style. The upside of having helper functions like
map,bind,applyand friends for error handling is that it gives names to commonly occurring patterns of nested and/or branching control flow that are easier to reason about (once one is comfortable with the concept) than error handling via control flow statements.gvanrossum commentedon Oct 9, 2019
Only a tiny number of people are going to adopt that style. It looks quite unpythonic to me.
stereobutter commentedon Oct 9, 2019
@gvanrossum I agree with your sentiment when applied to the python community as a whole; when only applied to the part of the community that uses mypy things might look different. The challenge is making a feature like this look and feel like python.
gvanrossum commentedon Oct 9, 2019
autoferrit commentedon Sep 19, 2020
I believe this would be a great idea as someone who just wants to write safer code. I found the project https://github.com/dry-python/returns some time ago and loved the idea, but didn't use it because of the same reason @gvanrossum mentioned is that it's not very pythonic. And felt that those using my code and not familiar with that
returnswould just be confused. I would much rather implement something that mypy could work with.My question as just an average developer is, until something like this is worked out in mypy or even a stand alone package, how would you guys recommend trying to at least start on something like this for a new code base? I like the syntax proposed in the comment #6936 (comment) with
OkandErr, but I agree having to unwrap the value in places wherever we need it, isn't super pretty. MaybeOkfor example could implement__str__,__dict__, etc or infer the type somehow so if we simple access theOk("foo")it returns the value. That seems like it could help with having to usesomething.valueanywhere.I agree in a way that
-> Result[str, Err]:isn't as clean as some other options but I like it more if it helps me write more stable code and it is pretty readable.I guess my question is what could be a recommended pattern if I wanted to try to wrap my return values in one of the methods described here, or to have this style of error checking, in a way that might later worth with mypy. I know the method isn't decided yet but if done cleanly enough, migrating to the accepted pattern when/if mypy implements this or supports it, shouldn't be too bad with good testing in place. My favorite is the ability for a mypy setting when using these, to enforce that we always handle the Ok/Err whenever it is used.
Edit: Another library that is trying to do a
Resulttype as well asOkandErrhttps://github.com/dbrgn/result/
fritzo commentedon May 4, 2023
I just want to point out one use case is
asyncio.create_task(). According to the docs, users must retain a reference to the return value to prevent the event loop from garbage collecting the task before completion. It would be great if those docs could be promoted to a static check.Details
Important Save a reference to the result of this function, to avoid a task disappearing mid-execution. The event loop only keeps weak references to tasks. A task that isn’t referenced elsewhere may get garbage collected at any time, even before it’s done. For reliable “fire-and-forget” background tasks, gather them in a collection:
emmatyping commentedon May 4, 2023
I'm not sure if something like
must_usewould really help withcreate_task, since we would only be able to check if the item returned bycreate_taskis used in some way, not that a reference is maintained throughout the items lifetime. If you create a task, do one thing to it, then immediately delete all references to it, it is still used but may die before the task is completed.jgarvin commentedon Jul 29, 2024
There are tons of functions that should work this way; in fact since error handling is typically done via exceptions in python requiring return values to be used should probably be the default for everything, it's almost always a bug in python when you ignore a return. The occasional thing that has a frequently unused return value should be manually opted out with some kind of annotation like
MayNotUse[int]instead ofMustUse[int].Feuermurmel commentedon May 4, 2026
@jgarvin
I'm not sure I understand the reasoning behind your statement:
Result, HaskellsEither) → Return values of these function need to be inspected, otherwise errors get silently lost.So it seems to me that Python falls into the category of programming languages where diligently using/inspecting all return values is less important because the language provides exceptions.
But I for one would like to see such a feature in the form of a decorator or a wrapper for the function's return type.
remdragon commentedon May 4, 2026
I love mypy and I use it on all new code too, not just on existing code bases. Mypy is quite good at catching places where I forgot to await something, but it's not good at telling me that I forgot to check for a failure or I forgot to catch a certain kind of exception. Python is amazingly good at building robust software in so many ways, which is why I switched to it 15 years ago and haven't looked back. But I still struggle at having confidence where and how things can go wrong. You don't know what Exceptions might be thrown by an operation and there's no mechanism to force you to deal with errors.
davidhalter commentedon May 4, 2026
There has been a discussion around this in a more general sense here: https://discuss.python.org/t/proposal-typing-no-discard-a-decorator-to-indicate-that-the-return-value-should-not-be-discarded/33138
I feel like if someone steps up and implements this in a type checker it has decent chances to be added to the typing library (it could be added to typing_extensions first). There are however a lot of questions that would need to be answered first, ideally in a PEP.
I personally also feel that this could be useful for non-"Result" types, but somebody would need to do the hard work and work out these examples.