mypy, type hint: Union[float, int] -> is there a Number type?

后端 未结 3 1837
醉酒成梦
醉酒成梦 2021-02-01 12:28

mypy is really handy and catches a lot of bugs, but when I write \"scientific\" applications, I often end up doing:

def my_func(number: Union[float, int]):
    #         


        
3条回答
  •  闹比i
    闹比i (楼主)
    2021-02-01 13:20

    For people who come to this question for the more general problem of Union typing hints for entities which don't have an existing supertype in common, for example Union[int, numpy.ndarray], the solution is to import Union from typing.

    Example 1:

    from typing import Union
    
    def my_func(number: Union[float, int]):
        # Do something
    

    Example 2:

    from typing import Union
    import numpy as np
    
    def my_func(x: Union[float, np.ndarray]):
        # do something
        # Do something
    

提交回复
热议问题