Skip to content

Instantly share code, notes, and snippets.

@ryuheechul
Last active March 27, 2024 21:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryuheechul/3f26ac6eae0bbe9b508a1a6200ecb2da to your computer and use it in GitHub Desktop.
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
"""
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()
@ryuheechul
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment