Suppose I have a python function def func_AB(param1: str). param1 can only take on values A or B. If it takes on any other string value, an error should appear.
Is it possible to use python type checking to give out error when this happens? Currently, I use assert to check that param1 contains the valid string value.
I am using python 3.8.5
you're looking for Literal
from typing import Literal
def func_AB(param1: Literal['A', 'B']):
...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With