[Python-ideas] Runtime types vs static types

Greg Ewing greg.ewing at canterbury.ac.nz
Sun Jul 2 18:59:15 EDT 2017


Steven D'Aprano wrote:
>     isinstance([], List[int])
>     isinstance([], List[str])
> 
> How can a single value be an instance of two mutually incompatible 
> types?

I don't think there's any contradiction there, because the
compatibility rules are different for static and runtime types.
Statically, when you assign something of type A to a variable
of type B, you're assering that *all* values of type A are
compatible with B. But at runtime, you're only asserting that
one *particular* value is compatible with B.

 > It just seems
> perverse to me to say that it is "meaningless" (in Mark's words) to ask 
> whether
> 
>     isinstance(['a', 'b'], List[int])
>     isinstance(123, List[str])
> 
> for example). If static type checking has any meaning at all, then the 
> answers to those two surely have to be False.

I doubt whether Mark meant "separate" to imply "unrelated".
Static and runtime types are clearly related, although the
relationship is not one-to-one and involves complicated
overlaps.

To my mind, the question isn't whether tests like that are
meaningful -- clearly they are. The question is whether we
should attempt to support answering them at run time, given
that doing so in the general case requires unbounded amounts
of computation.

-- 
Greg



More information about the Python-ideas mailing list