Skip to content
Snippets Groups Projects
Commit c7addcf7 authored by Markus Holzer's avatar Markus Holzer
Browse files

Merge branch 'fix_sharedmethodcache_docstrings' into 'master'

Fix: sharedmethodcache didn't preserve docstrings

See merge request pycodegen/pystencils!287
parents 1f3b1300 b1dccb49
No related merge requests found
import os import os
from collections.abc import Hashable from collections.abc import Hashable
from functools import partial from functools import partial, wraps
from itertools import chain from itertools import chain
try: try:
...@@ -43,6 +43,7 @@ def sharedmethodcache(cache_id: str): ...@@ -43,6 +43,7 @@ def sharedmethodcache(cache_id: str):
Of course, for this to be useful, said methods must have the same signature (up to additional kwargs) Of course, for this to be useful, said methods must have the same signature (up to additional kwargs)
and must return the same result when called with the same arguments.""" and must return the same result when called with the same arguments."""
def _decorator(user_method): def _decorator(user_method):
@wraps(user_method)
def _decorated_func(self, *args, **kwargs): def _decorated_func(self, *args, **kwargs):
objdict = self.__dict__ objdict = self.__dict__
cache = objdict.setdefault(cache_id, dict()) cache = objdict.setdefault(cache_id, dict())
......
...@@ -60,13 +60,16 @@ class Triad: ...@@ -60,13 +60,16 @@ class Triad:
@sharedmethodcache("triad_cache") @sharedmethodcache("triad_cache")
def triad(self, a, b, c=0): def triad(self, a, b, c=0):
"""Computes the triad a*b+c."""
self.triad_called += 1 self.triad_called += 1
return a * b + c return a * b + c
def test_triab_memoization(): def test_triad_memoization():
triad = Triad() triad = Triad()
assert triad.triad.__doc__ == "Computes the triad a*b+c."
t = triad.triad(12, 4, 15) t = triad.triad(12, 4, 15)
assert triad.triad_called == 1 assert triad.triad_called == 1
assert triad.triad_cache[(12, 4, 15)] == t assert triad.triad_cache[(12, 4, 15)] == t
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment