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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…