Last active
March 27, 2024 21:38
-
-
Save ryuheechul/3f26ac6eae0bbe9b508a1a6200ecb2da to your computer and use it in GitHub Desktop.
ensure literal type from a dynamic value by employing "guard" function in python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
seems like hard coding is the only way to make literal type to accept anything to be literal | |
(which makes sense if you think about it) | |
""" | |
from typing import Any, Literal | |
my_literal_type = Literal["a", "b", "c"] | |
# I used `Any` type here but you could use `str` if you want to restrict the dynamic value's type to be `str` only | |
def guard_mlt(value: Any) -> my_literal_type: | |
""" | |
only allow values in my_literal_type to pass | |
and otherwise raise TypeError | |
this requires hard coding the return values as it's explained at the top | |
>>> guard_mlt('a') | |
'a' | |
>>> guard_mlt('b') | |
'b' | |
>>> guard_mlt('c') | |
'c' | |
>>> guard_mlt('d') | |
Traceback (most recent call last): | |
... | |
TypeError: d is not in typing.Literal['a', 'b', 'c'] | |
""" | |
match value: | |
case "a": | |
return "a" | |
case "b": | |
return "b" | |
case "c": | |
return "c" | |
raise TypeError(f"{value} is not in {my_literal_type}") | |
if __name__ == "__main__": | |
import doctest | |
doctest.testmod() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is when you need to type check a dynamic value against a literal type similar to the case described at https://stackoverflow.com/questions/72650610/how-can-i-dynamically-test-whether-a-value-conforms-to-a-python-literal-type.