cachier icon indicating copy to clipboard operation
cachier copied to clipboard

weak assumptions on class methods

Open Borda opened this issue 1 year ago • 4 comments

In the readme, it is set that a class method shall not be cased if they use self, but nothing prevents top so, and the user can easily fall to wrong values; I think that we shall allow ONLY static methods and raise exceptions on anything else without making make many assumptions if the code is safe...

import cachier


class DummyClass():
    def __init__(self, state=0):
        self.state = state

    @cachier.cachier()
    def add(self, a):
        return self.state + a


object_1 = DummyClass(1)
print(object_1.add(2))
object_2 = DummyClass(2)
print(object_2.add(3))

Borda avatar Feb 13 '24 17:02 Borda

Here is the code for detecting static:

def is_function_static(func):
    for obj in globals().values():
        if inspect.isclass(obj) and hasattr(obj, func.__name__) and func.__name__ in obj.__dict__:
            method = obj.__dict__[func.__name__]
            if method is not None and isinstance(method, staticmethod):
                return True
    return False

Borda avatar Feb 15 '24 23:02 Borda

OK. Finally read that.

I tend to agree.

Every useful functionality of the current implementation with methods can be used by writing a static helper function and caching it using cachier instead. Thus, I would feel comfortable with blocking this use, with a note of this proper related use.

But is the same true for class methods? Help me think. Let's try to challenge our selves here.

shaypal5 avatar Feb 16 '24 11:02 shaypal5

well, you can set by default some global config with strict validation, and if a user wants he would allow weak checks..

Borda avatar Feb 19 '24 19:02 Borda

Ok. Agreed. So it can be on by default, and you can opt-out with a config.

shaypal5 avatar Feb 24 '24 11:02 shaypal5