Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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]):     # Do something 

number is either a float or int, depending on the user's input. Is there an official way to do that?

like image 854
JPFrancoia Avatar asked Jun 19 '18 12:06

JPFrancoia


People also ask

What is a type hint?

PEP 484 introduced type hints — a way to make Python feel statically typed. While type hints can help structure your projects better, they are just that — hints — and by default do not affect the runtime.

What is Python union type?

Union type; Union[X, Y] is equivalent to X | Y and means either X or Y. To define a union, use e.g. Union[int, str] or the shorthand int | str . Using that shorthand is recommended.

What is any type in Python?

The purpose of the Any type is to indicate to the type checker that a part of the program should not be checked. A variable (or function parameter) that is annotated with the Any type accepts any value, and the type checker allows any operation on it.

What is list from typing in Python?

List. Lists are used to store multiple items in a single variable. Lists are one of 4 built-in data types in Python used to store collections of data, the other 3 are Tuple, Set, and Dictionary, all with different qualities and usage.


2 Answers

You can define your own type to address this and keep your code cleaner.

FloatInt = Union[float, int]  def my_func(number: FloatInt):     # Do something 
like image 31
Curt Welch Avatar answered Oct 04 '22 05:10

Curt Welch


Use float only, as int is implied in that type:

def my_func(number: float): 

PEP 484 Type Hints specifically states that:

Rather than requiring that users write import numbers and then use numbers.Float etc., this PEP proposes a straightforward shortcut that is almost as effective: when an argument is annotated as having type float, an argument of type int is acceptable; similar, for an argument annotated as having type complex, arguments of type float or int are acceptable.

(Bold emphasis mine).

Ideally you would still use numbers.Real:

from numbers import Real  def my_func(number: Real): 

as that would accept fractions.Fraction() and decimal.Decimal() objects as well; the number pyramid is broader than just integers and floating point values.

However, these are not currently working when using mypy to do your type checking, see Mypy #3186.

like image 150
Martijn Pieters Avatar answered Oct 04 '22 05:10

Martijn Pieters