Discussion:
[Twisted-Python] How to get the class of the wrapped fget/fset property methods
Adi Roiban
2015-11-22 12:04:17 UTC
Permalink
The @deprecated decorator (at leas on py2.7) does not work when paired
@property

For deprecated instance variables, our deprecation policy recommend
converting them into properties and emitted the warning from there.
It would be nice if we could use the standard @deprecated decorator here.

The problem is that when property are used the fget/fset are received by
the @deprecated wrapper as functions and not as methods

Is there a way to get the class in which a property is defined... or there
is no way to use the @deprecated decorator with the @property .. and we
should create a dedicated deprecatedMember method which is called as a
normal method.

Thanks!
--
Adi Roiban
Glyph Lefkowitz
2015-11-23 00:32:38 UTC
Permalink
For deprecated instance variables, our deprecation policy recommend converting them into properties and emitted the warning from there.
If you care about inheritance, the implementation is a little bit tricky, because you have to manually walk the class hierarchy looking for the attribute. But conceptually it's pretty simple: just look at the type of 'oself' in the __get__ method of the returned descriptor.

Right now, @deprecated is hard-coded to assume a function, but it could be repurposed to work with specific other descriptor types reasonably easily. Arbitrary descriptors might be hard, because it's not clear when to emit the message, but specific types like @property should be pretty straightforward with an instance check.

-glyph
Adi Roiban
2015-11-23 12:30:53 UTC
Permalink
Post by Adi Roiban
For deprecated instance variables, our deprecation policy recommend
converting them into properties and emitted the warning from there.
The problem is that when property are used the fget/fset are received by
Is there a way to get the class in which a property is defined... or there
should create a dedicated deprecatedMember method which is called as a
normal method.
If you care about inheritance, the implementation is a little bit tricky,
because you have to manually walk the class hierarchy looking for the
attribute. But conceptually it's pretty simple: just look at the type of
'oself' in the __get__ method of the returned descriptor.
repurposed to work with specific other descriptor types reasonably easily.
*Arbitrary* descriptors might be hard, because it's not clear when to
straightforward with an instance check.
I think that we can go with a dedicated instance check for properties...
this is about the @deprecated decorator and using it together with the
deprecation policy for instance members.

I have created a dedicated ticket https://twistedmatrix.com/trac/ticket/8124


I still don't know how to fix it... I tried following property.__get__ and
property.getter and property.fget

From what I understand of how Python works, at the time when the decorator
is called, the class is not yet created / does not yet exists.

This will not work

class SomeClass(object):

@deprecated(
Version("Twisted", 16, 0, 0), 'the deferred returned by start()')
@property
def deferred(self):
"""
DEPRECATED. Deferred fired when loop stops or fails.
"""
return self._deferred

And I should use something like this

class SomeClass(object):

@property
def deferred(self):
"""
DEPRECATED. Deferred fired when loop stops or fails.
"""
deprecated(
Version("Twisted", 16, 0, 0), 'the deferred returned by
start()')
return self._deferred

---------


Will push the tests and will ask for help in the review queue.

Thanks!
--
Adi Roiban
Adi Roiban
2015-11-23 12:38:40 UTC
Permalink
Post by Adi Roiban
Will push the tests and will ask for help in the review queue.
Thanks!
Another option is to fix @deprecated to call getDeprecationWarningString
when the method is called not when it is created.

Regards
--
Adi Roiban
Glyph Lefkowitz
2015-11-24 05:27:30 UTC
Permalink
Post by Adi Roiban
Will push the tests and will ask for help in the review queue.
Thanks!
I think you mean the other way around? property() objects are not necessarily called.

-g
Adi Roiban
2016-03-16 08:40:52 UTC
Permalink
FYI. Amber has merged the branch adding @deprecatedProperty
https://twistedmatrix.com/trac/ticket/8124 and the issue raised in this
ticket is solved


Thanks!
--
Adi Roiban
Loading...