[Python-ideas] Runtime types vs static types

Ivan Levkivskyi levkivskyi at gmail.com
Thu Jun 29 05:40:29 EDT 2017


Sorry, I was not able to completely digest the OP, but I think there are
some points I need to clarify.

1. Distinction between runtime classes and static types is quite sane and a
simple idea.
A runtime class is something associated with an actual object,
while static type is something associated with an AST node.
Mixing them would be misleading, since they "live in parallel planes".
Although it is true that there is a type that corresponds to every runtime
class.

2. Currently isinstance(obj, List[int]) fails with TypeError, ditto for
issubclass and
for user defined generic classes:

class C(Generic[T]):
    ...

isinstance(obj, C)  # works, returns only True or False
isinstance(obj, C[int])  # TypeError
issubclass(cls, C)  # works
issubclass(cls, C[int])  # raisesTypeError

3. User defined protocols will by default raise TypeError with isinstance(),
but the user can opt-in (using @runtime decorator) for the same behavior as
normal generics,
this is how typing.Iterable currently works:

class MyIter:
    def __iter__(self):
        return [42]

isinstance(MyIter(), Iterable)  # True
isinstance(MyIter(), Iterable[int])  # TypeError

class A(Protocol[T]):
    x: T
isinstance(obj, A)  # TypeError

@runtime
class B(Protocol[T]):
    y: T
isinstance(obj, B)  # True or False depending on whether 'obj' has
attribute 'y'
isinstance(obj, B[int])  # Still TypeError

--
Ivan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20170629/b76e7f6d/attachment.html>


More information about the Python-ideas mailing list