I need clarification on Type HInts

I just learned about Type Hints and I’ve been adding them to one of my older programs because it’s definitely making the logic clearer. But I’m wondering how I can hint at input/output types more specifically. For example, if I have a function that processes a list of numbers, I would write this:

def sum(numbers: list) -> int:
    total = 0
    for i in numbers:
        total += i
    return total

Above I have hints that let you know the parameter should be a list and the function returns an integer. What if I want to specify that the list should be specifically a list of integers? Is there a way to write a hint that is more specific in that way?

I"ll give another example form the program I’m working on. I have a function that takes a list as a parameter. Specifically, I know it is a list of Path objects. If a list with any other types of objects were to be passed in, it wouldn’t work. I would like to use Type Hints as a note for my future self: “Hey, this function takes a list of path objects.”

Hope that makes sense.

Generally it’s list[Type], so in your example that would be list[int]

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.