[Python-ideas] Stub class for Generic to further improve PEP 560
levkivskyi at gmail.com
Thu Nov 30 17:24:08 EST 2017
On 30 November 2017 at 22:38, Ilya Kulakov <kulakov.ilya at gmail.com> wrote:
> A very rough implementation:
> class _GenericMetaNoop(type):
> def __getitem__(self, params):
> return self
> class _GenericNoop(metaclass=_GenericMetaNoop)
> GenericStub = Generic if TYPE_CHECKING else _GenericNoop
> import typing
> T = typing.TypeVar('T')
> class MyClass(typing.GenericStub[T]):
OK, I see.
Note that all type checkers known to me never actually read content of
typing.py (it is always heavily special cased).
Therefore, one can safely write `GenericStub = _GenericNoop`.
Anyway, my expectation is that going along this way (i.e. removing all
runtime API apart from a necessary minimum)
will give a minor speed-up as compared to PEP 560 at the cost of a breaking
change (even for small number of developers).
PEP 560 already gives overhead of 80% as compared to normal classes in
worst case scenario
(empty body with a single generic base). This is actually less than for
ABCs (they can give up to 120% in worst case scenario).
Moreover, performance is not a single motivation for PEP 560, there are
other arguments such as metaclass conflicts which will
not be solved without the machinery proposed by the PEP.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Python-ideas