Dynamic Testing of Values Against Python Literal Types
Last Updated :
01 Jul, 2024
In Python, Literal types are a way to specify that a value must be one of a set of specific values. This can be useful for type checking and ensuring your code handles only expected values. However, dynamically testing whether a value conforms to a Literal type can be tricky. This article will guide you through understanding Literal types and implementing dynamic tests to verify values against these types.
Understanding Literal Types
Literal types in Python allow you to restrict the values of a variable to a specific set. This is done using the Literal type from the typing module. For example, you can specify that a variable must be either apple, banana, or cherry:
Python
# code
from typing import Literal
def get_fruit(fruit: Literal['apple', 'banana', 'cherry']) -> str:
return fruit
This function will only accept 'apple', 'banana', or 'cherry' as valid arguments. Any other value will result in a type error if checked by a type checker like mypy.
Dynamically Testing for Python Literal Types
To dynamically test if a value conforms to a Python Literal type, you can use the typing_extensions
module, which provides compatibility with older versions of Python (prior to 3.8) and allows you to define and check for literal types using Literal
. Here’s how you can do it:
Implementing Dynamic Tests (Compatible with Python < 3.8)
Install typing-extensions:
If you haven't already installed the typing-extensions
package, you can install it using pip:
pip install typing-extensions
Import and Use Literal
:
Import Literal
from typing_extensions
and use it to define your literal types.
Python
from typing_extensions import Literal
# Example literal types
MyLiteralInt = Literal[1, 2, 3]
MyLiteralStr = Literal["apple", "orange", "banana"]
MyLiteralBool = Literal[True, False]
Dynamically Test the Value:
Use isinstance()
to dynamically test if a value conforms to the literal type.
Python
from typing import get_args, get_origin, Literal
def check_literal_type(value, literal_type):
if get_origin(literal_type) is Literal:
return value in get_args(literal_type)
raise TypeError(f"{literal_type} is not a Literal type")
# Example usage
print(check_literal_type(2, MyLiteralInt)) # True
print(check_literal_type("apple", MyLiteralStr)) # True
print(check_literal_type(False, MyLiteralBool)) # True
print(check_literal_type(4, MyLiteralInt)) # False
print(check_literal_type("banana", MyLiteralStr))# True
print(check_literal_type(True, MyLiteralBool)) # True
print(check_literal_type("grape", MyLiteralStr)) # False
Output:
True
True
True
False
True
True
False
Using Python 3.8+ Native Support
Starting from Python 3.8, you can directly use the Literal
type from the typing
module without needing typing_extensions
.
Define Literal Types: Define literal types using Literal
directly from typing
.
Python
from typing import Literal
# Example literal types
MyLiteralInt = Literal[1, 2, 3]
MyLiteralStr = Literal["apple", "orange", "banana"]
MyLiteralBool = Literal[True, False]
Dynamically Test the Value: Use isinstance()
to check if a value conforms to the literal type.
Python
from typing import get_args, get_origin, Literal
def check_literal_type(value, literal_type):
if get_origin(literal_type) is Literal:
return value in get_args(literal_type)
raise TypeError(f"{literal_type} is not a Literal type")
# Example usage
print(check_literal_type(2, MyLiteralInt)) # True
print(check_literal_type("apple", MyLiteralStr)) # True
print(check_literal_type(False, MyLiteralBool)) # True
print(check_literal_type(4, MyLiteralInt)) # False
print(check_literal_type("banana", MyLiteralStr))# True
print(check_literal_type(True, MyLiteralBool)) # True
print(check_literal_type("grape", MyLiteralStr)) # False
Output:
True
True
True
False
True
True
False
Example Implementations
Example 1: Testing Fruit Values
Python
# code
from typing import Literal, get_args
Fruit = Literal['apple', 'banana', 'cherry']
def is_fruit(value) -> bool:
return value in get_args(Fruit)
print(is_fruit('apple'))
print(is_fruit('orange'))
Output:
True
False
Example 2: Testing Status Codes
Python
# code
StatusCode = Literal[200, 404, 500]
def is_status_code(value) -> bool:
return value in get_args(StatusCode)
print(is_status_code(200))
print(is_status_code(403))
Output:
True
False
Testing Different Scenarios
We can use the is_literal_value function to test various scenarios dynamically. Here are some examples:
Scenario 1: User Roles
Python
# code
UserRole = Literal['admin', 'user', 'guest']
def is_user_role(value) -> bool:
return is_literal_value(value, UserRole)
print(is_user_role('admin'))
print(is_user_role('moderator'))
Output:
True
False
Scenario 2: File Extensions
Python
# code
FileExtension = Literal['.txt', '.pdf', '.docx']
def is_file_extension(value) -> bool:
return is_literal_value(value, FileExtension)
print(is_file_extension('.txt'))
print(is_file_extension('.exe'))
Output:
True
False
Conclusion
Dynamic testing of values against Python Literal types can help you ensure that your code only processes expected values. By using the get_args function from the typing module, you can implement flexible and reusable tests for various scenarios. This approach can improve the robustness of your code and catch errors early in the development process.
Similar Reads
How to find the int value of a string in Python?
In Python, we can represent an integer value in the form of string. Int value of a string can be obtained by using inbuilt function in python called as int(). Here we can pass string as argument to this function which returns int value of a string. int() Syntax : int(string, base) Parameters : strin
2 min read
Sum of list (with string types) in Python
In Python, type of behavior will not change for data type. Below example is list containing integer type in string.so, we have to take all int type numbers from list even it is declared in string. Examples: Input : list1 = [12, 'geek', 2, '41', 'for', 10, '8', 6, 4, 'geeks', 7, '10'] Output : 100 In
4 min read
Returning Multiple Values in Python
In Python, we can return multiple values from a function. Following are different ways 1) Using Object: This is similar to C/C++ and Java, we can create a class (in C, struct) to hold multiple values and return an object of the class. Python # A Python program to return multiple # values from a meth
4 min read
Get Variable Name As String In Python
In Python, getting the name of a variable as a string is not as straightforward as it may seem, as Python itself does not provide a built-in function for this purpose. However, several clever techniques and workarounds can be used to achieve this. In this article, we will explore some simple methods
3 min read
Internal implementation of Data Structures in Python
Python provides a variety of built-in data structures, each with its own characteristics and internal implementations optimized for specific use cases. In this article we are going to discuss about the most commonly used Data structures in Python and a brief overview of their internal implementation
3 min read
Primitive Data Types vs Non Primitive Data Types in Python
Python, known for its versatility and user-friendliness, is a dynamic language that doesn't explicitly categorize data types into primitive and non-primitive as some languages like Java do. However, for clarity and understanding, it's useful to draw parallels and discuss similar concepts. In this ar
4 min read
Python | Set 3 (Strings, Lists, Tuples, Iterations)
In the previous article, we read about the basics of Python. Now, we continue with some more python concepts. Strings in Python: A string is a sequence of characters that can be a combination of letters, numbers, and special characters. It can be declared in python by using single quotes, double quo
3 min read
Setting and Retrieving values of Tkinter variable - Python
In Tkinter, control variables are special variables used to link Python values with widgets like Entry, Label and Checkbutton. They act like regular variables but are designed to work with the GUI. These special variables are wrappers around Python data types and can be linked to widget values using
4 min read
Create Constant Variable in Python using Pconst Library
Constant variable the name itself says that it is constant. We have to define a constant variable at the time of declaration. After that, we will not able to change the value of a constant variable. In some cases, constant variables are very useful. Creating constant variables, functions, objects is
2 min read
How To Print A Variable's Name In Python
In Python, printing a variable's name can be a useful debugging technique or a way to enhance code readability. While the language itself does not provide a built-in method to directly obtain a variable's name, there are several creative ways to achieve this. In this article, we'll explore five simp
3 min read