Hone logo
Hone
Problems

Robust Type Checking with Type Guards in Python

Python's gradual typing system allows for more robust code through type hints. However, sometimes a variable's type isn't immediately obvious, especially when dealing with complex data structures or user input. This challenge focuses on implementing type guards – functions that narrow down the type of a variable within a specific scope, enabling more precise type checking and preventing runtime errors.

Problem Description

You are tasked with creating a set of type guard functions in Python. A type guard is a function that takes a variable as input and returns True if the variable is of a specific type, and False otherwise. These guards will be used within if statements to conditionally narrow the type of a variable, allowing you to perform type-specific operations safely.

Specifically, you need to implement the following type guards:

  1. is_string(val: Any) -> bool: Returns True if val is a string, False otherwise.
  2. is_int(val: Any) -> bool: Returns True if val is an integer, False otherwise.
  3. is_list(val: Any) -> bool: Returns True if val is a list, False otherwise.
  4. is_dict(val: Any) -> bool: Returns True if val is a dictionary, False otherwise.
  5. is_tuple(val: Any) -> bool: Returns True if val is a tuple, False otherwise.

These type guards should be robust and handle various input types correctly. The goal is to provide functions that can be used within type-checked code to ensure that operations are performed on variables of the expected type, improving code reliability and maintainability.

Examples

Example 1:

Input: "hello"
Output: True
Explanation: The input is a string, so is_string returns True.

Example 2:

Input: 123
Output: True
Explanation: The input is an integer, so is_int returns True.

Example 3:

Input: [1, 2, 3]
Output: True
Explanation: The input is a list, so is_list returns True.

Example 4:

Input: {"a": 1, "b": 2}
Output: True
Explanation: The input is a dictionary, so is_dict returns True.

Example 5:

Input: (1, 2, 3)
Output: True
Explanation: The input is a tuple, so is_tuple returns True.

Example 6:

Input: 1.5
Output: False
Explanation: The input is a float, not an integer, so is_int returns False.

Constraints

  • The input val to each type guard can be of any type (Any).
  • The type guards must return a boolean value (True or False).
  • The functions should be efficient and avoid unnecessary computations.
  • The functions should correctly identify the type of the input variable.

Notes

  • You can use the isinstance() function to check the type of a variable.
  • Consider edge cases such as None or empty collections when implementing the type guards.
  • The primary purpose of these type guards is to be used within if statements to conditionally narrow the type of a variable. For example: if is_string(my_variable): ...
  • This challenge focuses on the implementation of the type guards themselves; you don't need to write code that uses the type guards, just the guards themselves.
  • Remember that type guards are a key part of Python's gradual typing system, enabling more precise type checking and improved code reliability.
Loading editor...
python