You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is not a bug per se, but I'm not sure what the "recommended" way to resolve the situation is.
Basically, there is a slight problem when one wants to use the lru_cache decorator from the functools module on instance methods (described in more detail here), in that one should not do this:
The problem with the above is that pdoc does not document methods that begin with underscores, meaning there are at least 3 not-so-great ways to document the calculate method in the fixed version from above:
leave the docstring where it is; this means that the method is not documented at all (probably not the best)
rename _calculate to something that doesn't begin with an underscore and write a warning in the docstring to call calculate instead; this is a bit strange to both end users and myself
move the whole docstring to self.calculate (the now-member); the downsides of this are a) the full signature of the function is lost and needs to be documented, b) there is no "View Source" next to the method (since pdoc considers it a member), and c) it feels a bit strange to have such an enormous docstring in the middle of the constructor
So far, option 3 looks like the most sane one, but none of them are really "nice" (admittedly, the whole workaround for lru_cache is also a bit inelegant).
Steps to reproduce the behavior:
First (wrong) implementation:
importtimefromfunctoolsimportlru_cacheclassExpensiveComputation:
def__init__(self, delay: int=1):
""" Constructor Parameters ---------- delay, optional the delay """self.delay=delay@lru_cache(maxsize=None)defcalculate(self, arg: float) ->float:
""" Function for calculation Parameters ---------- arg the parameter to square Returns ------- float the result """time.sleep(self.delay)
returnarg**2
Rendering:
Second (fixed) implementation, with docstring fix from option 3:
importtimefromfunctoolsimportlru_cacheclassExpensiveComputation:
def__init__(self, delay: int=1):
""" Constructor Parameters ---------- delay, optional the delay """self.delay=delayself.calculate=lru_cache(maxsize=None)(self._calculate)
""" Function for calculation Parameters ---------- arg the parameter to square Returns ------- float the result """def_calculate(self, arg: float) ->float:
""" Function for calculation Parameters ---------- arg the parameter to square Returns ------- float the result """time.sleep(self.delay)
returnarg**2
Rendering:
I've generated the above using the --docformat numpy flag, but it's not necessary to reproduce the behavior.
For this specific use case, I would probably just do the following:
classCachedInstanceMethod:
def__init__(self):
self.expensive_computation=functools.cache(self.expensive_computation)
# @cache added in __init__defexpensive_computation(self) ->None:
"""Here is a docstring."""
The comment makes sure that a future reader who skips __init__ still knows that caching is happening, which would otherwise be my main complaint about this solution.
Problem Description
This is not a bug per se, but I'm not sure what the "recommended" way to resolve the situation is.
Basically, there is a slight problem when one wants to use the
lru_cache
decorator from thefunctools
module on instance methods (described in more detail here), in that one should not do this:but rather, something like this:
The problem with the above is that
pdoc
does not document methods that begin with underscores, meaning there are at least 3 not-so-great ways to document thecalculate
method in the fixed version from above:_calculate
to something that doesn't begin with an underscore and write a warning in the docstring to callcalculate
instead; this is a bit strange to both end users and myselfself.calculate
(the now-member); the downsides of this are a) the full signature of the function is lost and needs to be documented, b) there is no "View Source" next to the method (sincepdoc
considers it a member), and c) it feels a bit strange to have such an enormous docstring in the middle of the constructorSo far, option 3 looks like the most sane one, but none of them are really "nice" (admittedly, the whole workaround for
lru_cache
is also a bit inelegant).Steps to reproduce the behavior:
First (wrong) implementation:
Rendering:
Second (fixed) implementation, with docstring fix from option 3:
Rendering:
I've generated the above using the
--docformat numpy
flag, but it's not necessary to reproduce the behavior.System Information
The text was updated successfully, but these errors were encountered: