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 eltoder
Recipients Andrew.Lutomirski, belopolsky, eddygeek, eltoder, r.david.murray, serhiy.storchaka, yselivanov
Date 2016-05-15.15:40:54
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1463326854.66.0.736519203855.issue20371@psf.upfronthosting.co.za>
In-reply-to
Content
Properly supporting subclasses in replace() is hard, at least without some cooperation from subclasses. For a proper replace()

x.replace(a=newval).b == x.b

should hold for every b not dependent on a, including ones added by subclasses. That is, it should replicate subclass state. Arguably, ideal replace() would also allow changing attributes defined by subclasses -- otherwise subclasses need to override it anyway, and all this effort was for nothing.

The best I can think of is to assume that subclasses are immutable and all "primary" properties are settable via constructor arguments with the same names. Then replace() can be implemented like this:

def replace(self, *args, **kwargs):
    sig = inspect.signature(self.__new__)
    bound = sig.bind_partial(type(self), *args, **kwargs)
    for arg in sig.parameters:
        if arg not in bound.arguments:
            bound.arguments[arg] = getattr(self, arg)
    return self.__new__(*bound.args, **bound.kwargs)

This will not work for subclasses defined in C, but at least we can show a nice error about that. This will also not work if __new__ uses *args or **kwargs instead of listing every property as its own argument.

(Another approach I can think of is patching properties on self, making a copy of self via __reduce__, and reverting values on self back. This doesn't rely on any signatures, but gets really dirty really quick.)

So I don't know if we want to implement this, or if returning base class from replace() is a better choice.
History
Date User Action Args
2016-05-15 15:40:54eltodersetrecipients: + eltoder, belopolsky, r.david.murray, serhiy.storchaka, yselivanov, Andrew.Lutomirski, eddygeek
2016-05-15 15:40:54eltodersetmessageid: <1463326854.66.0.736519203855.issue20371@psf.upfronthosting.co.za>
2016-05-15 15:40:54eltoderlinkissue20371 messages
2016-05-15 15:40:54eltodercreate