[Python-ideas] Stub class for Generic to further improve PEP 560

Ilya Kulakov kulakov.ilya at gmail.com
Thu Nov 30 16:38:28 EST 2017


A very rough implementation:

typing.py:

    class _GenericMetaNoop(type):
        def __getitem__(self, params):
            return self

    class _GenericNoop(metaclass=_GenericMetaNoop)
        pass

    GenericStub = Generic if TYPE_CHECKING else _GenericNoop

elsewhere.py:

    import typing

    T = typing.TypeVar('T')

    class MyClass(typing.GenericStub[T]):
        pass

Now when run under type checkers MyClass will inherit typing.Generic with all runtime complexity.
However when run outside of them code will just work.


> On Nov 30, 2017, at 1:35 PM, Ivan Levkivskyi <levkivskyi at gmail.com> wrote:
> 
> Could you please give some examples for these statements? They still look to abstract for me.



More information about the Python-ideas mailing list