Search code examples
pythonpython-3.xreturn-typetype-hinting

Type hint function where return type depends on input list item type


What is the best type hint for a function that returns an item from an input argument which is a list?

Here is the function:

def ask_mcq(title: str, prompt: str, options: list) -> ???:
    ...snip...
    v = tk.IntVar()
    for i, option in enumerate(options):
        tk.Radiobutton(root, text=option, variable=v, value=i).pack(anchor="w")
    ...snip...
    return options[v.get()]

I tried checking other questions, like this one, but none of them seem to answer this question in specific. I am not an expert.


EDIT: By reading the comments and answers, I realize now that the question doesn't really make sense in the case where I ask the user to choose an option. It's better to return a string and handle it afterwards. I accepted @balderman's answer because it's the most applicable to this unlikely situation.


Solution

  • See the example below (assuming options is a list of Any)

    from typing import List,Any
    
    
    def ask_mcq(title: str, prompt: str, options: List[Any]) -> Any:
        return options[0]