How to use type argument as return type?
17:34 10 Nov 2025

Is it possible to do something like:

from __future__ import annotations

from typing import TypeVar

T = TypeVar("T", bound=str | int)

def get_parameter(parameter: str, _rtype: type[T]) -> T:
    """Get the parameter"""
    if parameter == "SIZE":
        return 5
    return "potato"

This yields error in python 3.12 with mypy 1.18.2

python 3.10 solution preferable

see this mypy playground example: https://mypy-play.net/?mypy=latest&python=3.12&gist=b0801cc0b4b4e1791b79e721d3180bc7

mypy python-3.10