Giter VIP home page Giter VIP logo

Comments (3)

GrahamDumpleton avatar GrahamDumpleton commented on May 25, 2024

Does anyone ever derive from the class in its old location after having being wrapped as deprecated?

The existing deprecating wrapper will as far as I can tell not work in that case. Unfortunately I don't know as yet a way around that specific problem with ObjectProxy either, although I haven't looked at the problem as much as I probably should at this point. It is quite possible it could be solved with a __new__ and/or metaclasses in some way.

Anyway, if I understand what you want, see code below.

from wrapt import ObjectProxy

class MovedClassProxy(ObjectProxy):

    def __init__(self, wrapped, message, stacklevel):
        super(MovedClassProxy, self).__init__(wrapped)
        self._self_message = message
        self._self_stacklevel = stacklevel

    def __call__(self, *args, **kwargs):
        print('__call__', self._self_message, self._self_stacklevel)
        return self.__wrapped__(*args, **kwargs)

    def __instancecheck__(self, instance):
        print('__instancecheck__', self._self_message, self._self_stacklevel)
        return isinstance(instance, self.__wrapped__)

    def __subclasscheck__(self, instance):
        print('__subclasscheck__', self._self_message, self._self_stacklevel)
        return issubclass(instance, self.__wrapped__)

class New(object):
    pass

print('New')

n = New()

Old = MovedClassProxy(New, 'message-1', 1)

print('isinstance(n, Old)')
print(isinstance(n, Old))

print('Old')

o = Old()

print('isinstance(o, New)')
print(isinstance(o, New))

print('Old == New')
print(Old == New)

class DerivedNew(New):
    pass

print('issubclass(DerivedNew, New)')
print(issubclass(DerivedNew, New))

print('issubclass(DerivedNew, Old)')
print(issubclass(DerivedNew, Old))

print('DerivedNew')

dn = DerivedNew()

print('isinstance(dn, New)')
print(isinstance(dn, New))

# You can't derive from the proxy, it would be necessary to access
# the __wrapped__ attribute to get the original.

# class DerivedOld(Old):
#     pass

class DerivedOld(Old.__wrapped__):
    pass

Note that the _self_ prefix on attributes updated on the proxy instance are special and the proxy object itself will always treat those as local to the proxy and not apply the change on the wrapped object. You can see more about local proxy attributes in:

from wrapt.

harlowja avatar harlowja commented on May 25, 2024

Thanks much, I'm gonna give this a try and see how it goes 😄 (much appreciated).

As for anyone deriving from it, no they should not be. They weren't even supposed to be referencing the location that exists but they did (so I'm moving some classes to a more top level/non-utility module that they can reference without using my libraries internal utility files...); so I just want to avoid breaking the existing code (mainly openstack code) that is using these in the wrong manner by at least giving them a chance to move there references to the new/better location (without borking there existing code).

from wrapt.

harlowja avatar harlowja commented on May 25, 2024

Much appreciated; it seems to be working @ https://review.openstack.org/#/c/129507/

from wrapt.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.