methodtools icon indicating copy to clipboard operation
methodtools copied to clipboard

sphinx documentation with `methodtools.lru_cache`

Open jbloom opened this issue 6 years ago • 11 comments

When I decorate a method with methodtools.lru_cache and then build sphinx documentation, the docstring shows as <methodtools._LruCacheWire object> rather than the documentation for the method in its docstring.

I think this type of problem is addressed here, but I can't figure out how to modify methodtools to fix the problem.

jbloom avatar Oct 24 '19 21:10 jbloom

Thanks for reporting missing feature. Because it is implemented out of methodtools project, I made a PR to wirerope. https://github.com/youknowone/wirerope/pull/5

~Unfortunately, I made working version for methods but not about properties yet.~

youknowone avatar Oct 26 '19 05:10 youknowone

I released 0.1.2 with this document fix: https://pypi.org/project/methodtools/0.1.2/ Will you test it please?

youknowone avatar Oct 26 '19 07:10 youknowone

This did not seem to fix the problem for me.

jbloom avatar Oct 26 '19 14:10 jbloom

I wanted to test this problem with actual example of sphinx but didn't have enough time to do that. Do you know any small and simple sphinx example proejct?

youknowone avatar Nov 09 '19 13:11 youknowone

Dear @youknowone , Thank you for your helpful package. I started to use methodtools in my project and found the same issue. https://lisphilar.github.io/covid19-sir/covsirphy.cleaning.html#covsirphy.cleaning.term.Term.num2str

Do we need to create a new decorator to show the docstring? Please help me out.

lisphilar avatar Jul 25 '20 14:07 lisphilar

Sorry for late fix. I want to fix this problem but almost forgot about it for a while. I think wrapping once more will be a simple workaround for simple use case, if you don't use cache_info()

image

It seems sphinx doesn't support function-like object. I need to dig in sphinx a bit.

youknowone avatar Jul 26 '20 07:07 youknowone

Dear @youknowone , Thank you for your prompt reply! I tried the following code.

from methodtools import lru_cache

def show_info(func):
    def wrapper(*args, **kwargs):
        return func.cache_info(*args, **kwargs)
    return wrapper


class ClassA(object):
    @show_info
    @lru_cache(maxsize=None)
    def method_a(self, arg1):
        """
        This may be a method to do something.
        """
        pass

In sphinx document, <methodtools._LruCacheWire object> was successfully removed! However, it shows method_a(**kwargs) even when arguments (arg1) exist. Docsting was not shown. Sorry for asking basic question of decorator, but please tell me how to improve it.

lisphilar avatar Jul 27 '20 16:07 lisphilar

I don't think you need cache_info here. Check this way: https://docs.python.org/3/library/functools.html#functools.wraps

youknowone avatar Jul 29 '20 04:07 youknowone

Dear @youknowone , I'm sorry, but I couldn't find the solution in my project. Please clarify the solution with the example codes I used in the previous comment.

lisphilar avatar Jul 30 '20 17:07 lisphilar

It seems sphinx only support actual functions

youknowone avatar Aug 10 '20 10:08 youknowone

Thank you for your help.

lisphilar avatar Aug 13 '20 09:08 lisphilar