Just going to say it one more time and then I’m bowing out, because at this point we’re just talking past each other
int
is currently treated as a subtype of float
by all type checkers. Regardless of your opinion on that, it is the status quo and changing that is a major backwards compatibility break that would affect millions of lines of code
The Subtype relationships section of PEP 483 gives 3 examples:
Dog
is a subtype ofAnimal
int
is a subtype offloat
List[int]
is not a subtype ofList[float]
(becauseList
is invariant)
and has this code example
lucky_number = 3.14 # type: float
lucky_number = 42 # Safe
lucky_number * 2 # This works
lucky_number << 5 # Fails
unlucky_number = 13 # type: int
unlucky_number << 5 # This works
unlucky_number = 2.72 # Unsafe
The numeric tower section of PEP 484 says:
PEP 3141 defines Python’s numeric tower, and the stdlib module
numbers
implements the corresponding ABCs (Number
,Complex
,Real
,Rational
andIntegral
). There are some issues with these ABCs, but the built-in concrete numeric classescomplex
,float
andint
are ubiquitous (especially the latter two :-).Rather than requiring that users write
import numbers
and then usenumbers.Float
etc., this PEP proposes a straightforward shortcut that is almost as effective: when an argument is annotated as having typefloat
, an argument of typeint
is acceptable; similar, for an argument annotated as having typecomplex
, arguments of typefloat
orint
are acceptable. This does not handle classes implementing the corresponding ABCs or thefractions.Fraction
class, but we believe those use cases are exceedingly rare.
Both assigning an int
to a variable annotated as float
and passing an int
as an argument to a function parameter annotated as float
are unambiguously stated as allowed behavior.
The discussion was started here because one of the people working on implementing a type checker correctly pointed out that the current language in the typing specification does not clearly and unambiguously state how allowing those behaviors should be implemented.
Changing what behavior should be allowed is a different topic