Comments (3)
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.
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.
Much appreciated; it seems to be working @ https://review.openstack.org/#/c/129507/
from wrapt.
Related Issues (20)
- Methods of proxied objects are bound to the original object, not the proxy HOT 4
- PySide6 behaves differently between decorated and non-decorated function HOT 16
- Idea: Sticky/Viral ObjectProxy HOT 3
- ObjectProxy does not play well with GenericAlias, such as isinstance(proxy, Dict) HOT 12
- RFE: is it possible to start making github releases?🤔 HOT 2
- Publish cp312 wheel HOT 6
- Release 1.14.1 Py3.11 wheels HOT 7
- __doc__ property HOT 9
- Request to be able to import both py and c wrappers HOT 8
- Unable to install wrapt 1.14.1 via poetry HOT 2
- Accessing ObjectProxy __dict__ HOT 5
- Update to setup.cfg potentially required HOT 4
- Documentation isn't building? HOT 6
- Best way to associate some data with `ObjectProxy`? HOT 3
- Accessing a class attribute that is a wrapt wrapped function will try and bind the function. HOT 4
- pydevd error when debugging with wrapt HOT 28
- Add type hint annotations for user-facing code HOT 2
- classmethod tests fail with Python 3.13 (Python reverted to pre-3.9 behavior) HOT 2
- 1.16.0: pytest fails
- How to use adapter factory to change signature depending on instance HOT 9
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from wrapt.