[New-bugs-announce] [issue40336] Refactor typing._SpecialForm

Serhiy Storchaka report at bugs.python.org
Mon Apr 20 07:27:57 EDT 2020


New submission from Serhiy Storchaka <storchaka+cpython at gmail.com>:

The proposed PR refactors the implementation of typing._SpecialForm.

Different special forms are different by name, docstring, and the behavior of __getitem__. Instead of special casing by name in __getitem__, instances are now parametrized by the implementation of __getitem__. _SpecialForm is now used as a decorator and takes the name and the docstring from the decorated function. It looks nicer to me now.

Also removed implementations of some methods:

* Inheriting from _Immutable no longer needed because __reduce__ makes instance atomic for copying.

* __new__ was only needed when _SpecialForm was a subclass of type. It prevented using it as a metaclass. Now it is no longer needed.

* Docstring is now assigned directly to the __doc__ slot. Previously it conflicted with the class docstring, but now the class docstring was converted into the class comment. _SpecialForm is not a public class, and pydoc shows the class comment if there is no a class docstring.

* __eq__ and __hash__ are no longer needed. Instances are singletons, and comparing by identity works as well as comparing by name. They could be needed before implementing __reduce__().

* __call__ no longer needed. Instances are not callable, and the error message is good enough. The bonus -- callable() now returns False.

----------
components: Library (Lib)
messages: 366817
nosy: gvanrossum, levkivskyi, serhiy.storchaka
priority: normal
severity: normal
status: open
title: Refactor typing._SpecialForm
type: enhancement
versions: Python 3.9

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue40336>
_______________________________________


More information about the New-bugs-announce mailing list