#StackBounty: #python #generics #types #mypy #pydantic Python type hinting: function return type, given as argument, but type is Generi…

Bounty: 50

We can specify easily return type of function, even if type we expect is provided as one of it’s argument:

from typing import TypeVar, Type, Union

T = TypeVar('T')


def to(t: Type[T], v) -> T:
    return t(v)


s: str = to(str, 1)
i: int = to(str, 1)  # Ok: Expected type 'int', got 'str' instead

But it does not work if we provide Union[str, int] as first argument (do not see at function itself, we can do something more complex with this Union, i.e. construct pydantic model based on Union provided)

G = Union[str, int]
g: G = to(G, 1) # Expected type 'Type[T]', got 'object' instead 

So how to specify that return type of function should be the one provided as first argument? Even if we provide not pure type like int or str, but also Union?

UPDATE

To be more precise there is the function i’d like to decorate

from typing import Type, Union

from pydantic import validate_arguments, BaseModel


def py_model(t, v: dict):
    def fabric():
        def f(x):
            return x

        f.__annotations__['x'] = t
        return validate_arguments(f)

    f = fabric()
    return f(v)

class Model1(BaseModel): foo: int
class Model2(BaseModel): bar: str

print(repr(py_model(Union[Model1, Model2], {'foo':1}))) # Model1(foo=1)
print(repr(py_model(Union[Model1, Model2], {'bar':1}))) # Model2(bar='1')

So when I call py_model(Union[Model1, Model2], …) I expect it will return one of Model1 or Model2 so Union[Model1, Model2]


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.