Is there a pythonic way to constrain the output of a method so that it can only be one of a set? Sort of like typing but for specific values only. I hope you can see what I’m trying to get at with this snippet:
class Rule:
def evaluate(self, user_id: int) -> {"PASS", "FAIL", "ERROR"}:
...
In the above case I would be hoping for evaluate to only return "PASS", "FAIL" or "ERROR"
>Solution :
In python 3.8 or higher, you can use Literal types:
from typing import Literal
class Rule:
def evaluate(self, user_id: int) -> Literal["PASS", "FAIL", "ERROR"]:
...