Python Type Hints: Specifying a type to be a List of numbers (ints and/or floats)?
问题 How do I specific a function can take a list of numbers which can be ints or floats? I tried making a new type using Union like so: num = Union[int, float] def quick_sort(arr: List[num]) -> List[num]: ... However, mypy didn't like this: quickSortLomutoFirst.py:32: error: Argument 1 to "quickSortOuter" has incompatible type List[int]; expected List[Union[int, float]] Is there a Type that encompasses ints and floats? 回答1: The short answer to your question is you should use either TypeVars or