I'm trying to sort out the Python typing rules using PEP-483 as a starting
point and the treatment of is-consistent-with and Any seems unclear.
"The is-consistent-with relationship is defined by three rules:
- A type t1 is consistent with a type t2 if t1 is a subtype of t2. (But not
the other way around.)
- Any is consistent with every type. (But Any is not a subtype of every
type.)
- Every type is consistent with Any. (But every type is not a subtype of
Any.)"
And then, for instance:
"Tuple[u1, u2, ..., um] is a subtype of Tuple[t1, t2, ..., tn] if they have
the same length n==m and each ui is a subtype of ti."
From the above, I would expect type checkers to reject both of the
assignments below:
t0: Tuple[int, Any] = (0, object())
t1: Tuple[int, str] = (1, 't1')
t0 = t1
t1 = t0
Neither of these types are Any so they are only consistent if they have a
subtype relationship.
Mypy and pytype accept both assignments. If we treat Any as a top type
then the first assignment is OK and the second is not, and that's not
what's going on. So there must be some notion of consistent subtyping that
is what is actually implemented. Is this spelled out anywhere other than
the code?
--
[image: logo]
Kevin Millikin
Software Engineer
kmillikin(a)deepmind.com
If you received this email in error, please do not forward it to anyone
else (it may contain confidential or privileged information), erase all
copies and attachments, and let me know that it has gone to the wrong
person.