]I’ve recently written a piece of code that looks something like this:
def display_info(input_data: tuple[int, str, dict]):
numeric_value, text, key_value_store = input_data
print(numeric_value, text, key_value_store)
However, I’ve noticed that this type hinting approach might not be the most flexible. The function performs adequately with any iterable object ordered as int, str, dict that is passed to ‘input_data’, not just tuples.
display_info((1, 'foo', {})) # This executes perfectly and satisfies the type checker.
display_info([1, 'foo', {}]) # This also executes perfectly but doesn't satisfy the type checker.
display_info(i for i in (1, 'foo', {})) # Once more this works but doesn't satisfy the type checker.
While users can manually convert their input into a tuple before passing it to the function as shown below,
def get_information() -> list:
"""Simulating data retrieval from some external source."""
return [1, 'foo', {}]
display_info(tuple(get_information())) # This functions but involves unnecessary steps.
I’d prefer not to require them to perform this additional step which is essentially unnecessary.
So I have two main inquiries. Does Python provide a way to implement positional type hints for iterables? If such functionality does not currently exist within Python’s typing system could you explain why it might be absent? It would be interesting if there were potential enhancements on this concept in future versions of Python. Thank you!