This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author ncoghlan
Recipients NeilGirdhar, eric.smith, ncoghlan, rhettinger, serhiy.storchaka
Date 2018-05-05.15:08:08
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1525532888.3.0.682650639539.issue33419@psf.upfronthosting.co.za>
In-reply-to
Content
Note that Neil did start with a python-ideas discussion, and was directed over here, since the idea seemed simple enough, and worth pursuing.

As Serhiy notes though, there are many more subtleties to be addressed here than I first thought.

That said, as long as __init__, __new__ and __slots__ are handled appropriately in the partial subclass, I think custom metaclasses should largely take care of themselves, so it would be better to avoid the compatibility implications of injecting an implicit metaclass.

The slots case should be adequately handled by doing:

    if hasattr(cls, "__slots__"):
        __slots__ = ()

The __new__ case may have some quirks due to the fact it's technically implemented as an implicitly static method, but I *think* that can be covered by defining it as:

    if cls.__new__ is not object.__new__:
        __new__ = partialmethod(cls.__new__, *args, **kwds)

and relying on the native class machinery to include the same kind of fixup that it applies for any other __new__ method implementation.

__init__ will need a similar "has it been overridden?" check to the one in __new__ (comparing the unbound methods via "cls.__init__ is not object.__init__").


Some of the other issues that Serhiy mentions are real problems with the approach (like pickle compatibility, alternate constructor support, etc), but I think those can simply be noted in the documentation, with the following double-subclassing recipe noted as a suggested way of handling them:

    class MySubclass(partialclass(BaseClass, *args, **kwds)):
        ...
        # MySubclass supports pickle as it's reachable by name
        # Custom constructors can be overloaded as needed here

(Note: if you're not seeing https://github.com/python/cpython/blob/master/Doc/whatsnew/3.8.rst locally, check that you're accidentally working on the 3.7 branch, instead of the master branch)
History
Date User Action Args
2018-05-05 15:08:08ncoghlansetrecipients: + ncoghlan, rhettinger, eric.smith, serhiy.storchaka, NeilGirdhar
2018-05-05 15:08:08ncoghlansetmessageid: <1525532888.3.0.682650639539.issue33419@psf.upfronthosting.co.za>
2018-05-05 15:08:08ncoghlanlinkissue33419 messages
2018-05-05 15:08:08ncoghlancreate