I want to achieve a simple interface as such:
IsAllowed(perms=[IsAdmin(runtime_value) | HasPerm('*')])
Since some variables upon instantiation are not available, I have to postpone the evaluation of perms=[IsAdmin() | HasPerm('*')] to later point in time (when __call__ is called on IsAllowed by the FastAPI webframework I'm using). The closest I have come to achieving that is the following:
IsAllowed(perms = lambda runtime_value: [IsAdmin(runtime_value) | HasPerm('*')])
class IsAllowed(object):
def __init__(self, perms=None):
self.perms = perms
def __call__(self, runtime_value):
result = self.perms(runtime_value)
return result
No my question is, is there any way to keep the interface as IsAllowed(perms=[IsAdmin(runtime_value) | HasPerm('*')]) but, somehow inject the lambda into it before instantiation?
I am thinking of maybe metaclasses or leveraging the __init_subclass__ hook?
Any ideas?