How can I type-hint a function where the return type depends on the input type of an argument?

前端 未结 1 1313
天涯浪人
天涯浪人 2020-12-18 23:42

Assume that I have a function which converts Python data-types to Postgres data-types like this:

def map_type(input):
    if isinstance(input, int):
                 


        
相关标签:
1条回答
  • 2020-12-19 00:08

    This is exactly what function overloads are for.

    In short, you do the following:

    from typing import overload
    
    # ...snip...
    
    @overload
    def map_type(value: int) -> MyEnum: ...
    
    @overload
    def map_type(value: str) -> MyCustomClass: ...
    
    def map_type(value: Union[int, str]) -> Union[MyEnum, MyCustomClass]:
        if isinstance(value, int):
            return MyEnum(value)
        elif isinstance(value, str):
            return MyCustomClass(value)
        raise TypeError('Invalid input type')
    

    Now, when you do map_type(3), mypy will understand that the return type is MyEnum.

    And at runtime, the only function to actually run is the final one -- the first two are completely overridden and ignored.

    0 讨论(0)
提交回复
热议问题