Skip to content

Warning of utils.deprecated for properties is ugly #8113

Closed

Description

@jnothman

The contributor's guide recommends using the deprecated decorator for attributes as follows:

    @property
    @deprecated("Attribute labels_ was deprecated in version 0.13 and "
                "will be removed in 0.15. Use 'classes_' instead")
    def labels_(self):
        return self.classes_

However the warning produced begins "Function labels_ is deprecated; Attribute labels_ was deprecated ...". This is ugly. It should just say "Attribute labels_ is deprecated; ...".

We should either:

  • remove the boilerplate "Function x is deprecated" as I think we're not counting on it being there in our deprecation messages elsewhere
  • handle explicitly the case where deprecated decorates a property

(No doubt futurepast aspires to do all of this right.)

Activity

  1. Dorozhko-Anton commented on Dec 24, 2016

    @Dorozhko-Anton

    Can I get this one for a start?

  2. jnothman commented on Dec 25, 2016

    @jnothman
    MemberAuthor

    I'm not sure there is consensus about the solution, but if you understand the problem well, you should be able to come up with a persuasive solution.

  3. Dorozhko-Anton commented on Dec 25, 2016

    @Dorozhko-Anton

    I tried to handle property explicitly. It is ugly but works. What do you think?

    def _decorate_fun(self, fun):
            """Decorate function fun"""
            msg = " %s is deprecated" % fun.__name__
            if self.extra:
                msg += "; %s" % self.extra
    
            @functools.wraps(fun)
            def wrapped(*args, **kwargs):
    
                def ismethod(f):  // check that our function is method
                    spec = inspect.getfullargspec(f)
                    return 'self' in spec.args
    
                if ismethod(fun): 
                    // find all properties 
                    properties = inspect.getmembers(args[0].__class__, lambda o: isinstance(o, property))
                    properties_names = [x[0] for x in properties] 
                    
                    if fun.__name__ in properties_names: 
                        entity = "Property"
                    else:
                        entity = "Function"
    
                warnings.warn(entity + msg, category=None)
                return fun(*args, **kwargs)
    
            return wrapped

    and I have checked on small example

    class TestObject:
    ...
        @deprecated("deprecated method")
        def hello(self):
            pass
    
        @property
        @deprecated("deprecated property")
        def name(self):
            """Get name"""
            return self._name
    ...

    output:

    test/deprecation.py:98: UserWarning: Property name is deprecated; deprecated property
    /test/deprecation.py:98: UserWarning: Function hello is deprecated; deprecated method
  4. jnothman commented on Dec 25, 2016

    @jnothman
    MemberAuthor
  5. Dorozhko-Anton commented on Dec 26, 2016

    @Dorozhko-Anton

    I was thinking about that but it will brake property

        @deprecated("deprecated attribute")
        @property
        def name(self):
            """Get name"""
            return self._name
    
        @name.setter // AttributeError: 'function' object has no attribute 'setter'
        def name(self, value):
            self._name = value
    
        @name.deleter // AttributeError: 'function' object has no attribute 'setter' 
        def name(self):
            del self._name

    May be there is a workaround by returning property from deprecated wrapper

  6. Akshay0724 commented on Jan 5, 2017

    @Akshay0724
    Contributor

    Hello @jnothman, In my opinion both part(i.e deprecated(property(funct.)) & property(deprecated(funct.)) ) have to be handled separately.
    Order of execution of both is reversed:

    1. @Property
      @deprecated
      Method()

      In this case first decorated class object is created,than it becomes property.Object which is passed to __ call __(in class deprecated in decorating.py) looks like a method.So this part should be handled in _decorate_fun()(line 62 in decorating.py)
      How to detect a method is property or not?
      from args(passed to method) we can get self(i.e object of class) and from that object through inspect module we can get all the property of that class.Now we have to check if any function name in property(i.e fget, fset, fdel) matches with with the name of function passed to decorator for ever property. If match is found than it is a prperty otherwise a method.

    2. @deprecated
      @Property
      Method()

      In this case method get converted to property first and than deprecated object is created. So object which is passed to decorator can be in investigated at that time that it is a method or property in __ call __() itself.

    I'd like to work in this issue if you allow me .

  7. Akshay0724 commented on Jan 5, 2017

    @Akshay0724
    Contributor

    I have implemented a basic solution to this problem in notebook-
    https://github.com/Akshay0724/soft/blob/master/Depricate.ipynb
    Please correct me if I'm wrong.

  8. jnothman commented on Jan 5, 2017

    @jnothman
    MemberAuthor

    Work is underway at #8113, @Akshay0724. How do your comments compare?

  9. Akshay0724 commented on Jan 6, 2017

    @Akshay0724
    Contributor

    Sorry for the comment, as I'm a beginner that's why I did not know when and where to comment. I thought that this issue still needs a contributor so I proposed a solution.

  10. jnothman commented on Jan 7, 2017

    @jnothman
    MemberAuthor

    Sorry this issue had retained its "Needs Contributor" label despite @Dorozhko-Anton proposing a PR. I also wrote the incorrect PR above, should have been #8113 (as github cross-references above too). I have not had the time to look at your approach, but would welcome comment at that pull-request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions