typing.Literal: Specify the Possible Values for a Function Parameter

If you want to use type hints to check that a variable or a function parameter is in a set of literal values, use typing.Literal.

In the example above, since grape is not in the set of literal values, mypy raises an error.

This method is available in Python 3.8 and above. 

Related Posts

Related Posts

Scroll to Top