Open
Description
The typing spec currently says this:
Python’s numeric types complex, float and int are not subtypes of each other, but to support common use cases, the type system contains a straightforward shortcut: 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.
This is helpful in that it clarifies that there is not a subtype relationship here. It remains unclear (to me, at least) in the following ways:
- It implies that this special case applies only to function argument annotations, and not to any other annotation (e.g. an annotation of the type of an attribute of a class). I find this surprising, but perhaps it is intended? I think it is surprising enough that if it's intended, the wording should be even clearer, and explicitly show examples where the special case does not apply outside an argument annotation.
- The wording "is acceptable" in the absence of a subtype relationship does not clarify how this special case actually fits into the type system; we can accept an
int
to afloat
typed argument, but then how do we type that name within the function body? If what we actually mean here is thatfloat
should be interpreted asfloat | int
, then we should say that clearly.
There was some discussion of this on #1663, which is about the different but related question of whether to mention the numeric tower and PEP 3141.
Activity