Skip to content

Docstrings of deprecated attributes aren't rendered properly #13489

Closed

Description

@NicolasHug

Function docstrings are handled such that the 'summary' of the function is whatever comes before the first period. For example with this method:

    def fit(self, X, y=None):
        """Fit data X. This is another part that isn't rendered in the summary."""
        ...

The summary will be Fit data X., and the full docstring will be rendered in the detailed method description, as expected.

Now, deprecated attributes become properties, and properties are functions. What sphinx renders as the deprecated attribute docstring is its summary. So deprecating an attribute with

    @property
    @deprecated("The foo parameter is deprecated. Use bar instead")
    def foo(self):
        return self.bar

will only describe foo with The foo parameter is deprecated., that is, whatever comes before the first period.

This is related to #8113, i.e. fixing #8113 could probably fix this.

Activity

  1. NicolasHug commented on Mar 22, 2019

    @NicolasHug
    MemberAuthor

    As noted here #8122 (comment), the fix should be easy as long as we enforce the convention that @deprecated should be put on top of @property:

        @deprecated("blah blah is deprecated")
        @property
        def some_attribute(self):
    		# ...

    This is because with this order, it is easy for the depreacted decorator to know whether it's decorating a classical method or a property.

    @jnothman , according to #8122 (comment), you seem to be OK with enforcing this. Is that still the case? Also ping @adrinjalali @qinhanmin2014 @thomasjpfan , in case one of you would not like this.

    If you're all OK, I'll submit a PR that:

    • fixes this issue
    • fixes Warning of utils.deprecated for properties is ugly #8113
    • update the dev guide and make it clear that this order should be enforced
    • update all use of property(deprecated(...)) to reverse the order (there are only 3 instances, all for OneHotEncoder. You can use git grep -A1 property | grep deprecated -B1 to check).

    Please let me know.

  2. adrinjalali commented on Mar 22, 2019

    @adrinjalali
    Member

    sounds good to me, any chance we could have a common test for it?

  3. NicolasHug commented on Mar 22, 2019

    @NicolasHug
    MemberAuthor

    You mean a test ensuring that every deprecated attribute uses the decorators in the correct order? Unfortunately I think it would be pretty hard since it would have to use e.g. the technique proposed in #8122 (comment)

    That being said the worst case scenario is that the orderer is reversed, which is exactly where we are today. So it's not that critical.

  4. adrinjalali commented on Mar 22, 2019

    @adrinjalali
    Member

    wouldn't some grep magic be enough for that? I know it's not critical, it's just that it's not easy to remember and things again get where they are now over time.

  5. adrinjalali commented on Mar 22, 2019

    @adrinjalali
    Member

    What I mean is just to check that between a @property and a def , there's no @deprecated.

  6. NicolasHug commented on Mar 22, 2019

    @NicolasHug
    MemberAuthor

    I'm not a grep wizard so I'm not sure but in my experience, grep isn't meant to work well with multiple line matches, and it's going to be a pain to ensure that a sequence of @property and @deprecated is bound to the same def.

  7. adrinjalali commented on Mar 22, 2019

    @adrinjalali
    Member

    I guess the output of this has to be empty?

    $ git grep -A 10 "@property" | awk '/@property/,/def /' | grep -B1 "@deprecated"
    doc/developers/contributing.rst:    @property
    doc/developers/contributing.rst-    @deprecated("Attribute labels_ was deprecated in version 0.13 and "
    --
    sklearn/preprocessing/_encoders.py:    @property
    sklearn/preprocessing/_encoders.py-    @deprecated("The ``active_features_`` attribute was deprecated in version "
    --
    sklearn/preprocessing/_encoders.py:    @property
    sklearn/preprocessing/_encoders.py-    @deprecated("The ``feature_indices_`` attribute was deprecated in version "
    --
    sklearn/preprocessing/_encoders.py:    @property
    sklearn/preprocessing/_encoders.py-    @deprecated("The ``n_values_`` attribute was deprecated in version "
    

    As is the output of the following line right now:

    $ git grep -A 10 "@deprecated" | awk '/@deprecated/,/def /' | grep -B1 "@property"
    

    Or is it missing anything? I'm certain a perl command could also do it all, but this is what I could come up with.

  8. NicolasHug commented on Mar 22, 2019

    @NicolasHug
    MemberAuthor

    Or this git grep -A1 "@property" | grep -B1 "@deprecated"

    But can we really write pytest tests that use git grep and grep?

  9. adrinjalali commented on Mar 22, 2019

    @adrinjalali
    Member

    Or this git grep -A1 "@Property" | grep -B1 "@deprecated"

    This may miss the potential cases where there are multiple decorators there, and @deprecated is not right after @property, but I'm not sure if that ever happens.

    If not pytest, a command in the linter step of the CIs could do that maybe.

  10. NicolasHug commented on Mar 23, 2019

    @NicolasHug
    MemberAuthor

    Got it, thanks for the feedback Adrin. Waiting for the others input now.

  11. jnothman commented on Mar 24, 2019

    @jnothman
    Member
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