viur-framework/viur-core

Implement `conf["viur.paramFilterFunction"]` more efficiently

phorward opened this issue · 0 comments

Maybe with an @property like

@property
def tasks_custom_environment_handler(self) -> t.Optional["CustomEnvironmentHandler"]:
"""
Preserve additional environment in deferred tasks.
If set, it must be an instance of CustomEnvironmentHandler
for serializing/restoring environment data.
"""
return self._tasks_custom_environment_handler
@tasks_custom_environment_handler.setter
def tasks_custom_environment_handler(self, value: "CustomEnvironmentHandler") -> None:
from .tasks import CustomEnvironmentHandler
if isinstance(value, CustomEnvironmentHandler) or value is None:
self._tasks_custom_environment_handler = value
elif isinstance(value, tuple):
if len(value) != 2:
raise ValueError(f"Expected a (serialize_env_func, restore_env_func) pair")
warnings.warn(
f"tuple is deprecated, please provide a CustomEnvironmentHandler object!",
DeprecationWarning, stacklevel=2,
)
# Construct an CustomEnvironmentHandler class on the fly to be backward compatible
cls = type("ProjectCustomEnvironmentHandler", (CustomEnvironmentHandler,),
# serialize and restore will be bound methods.
# Therefore, consume the self argument with lambda.
{"serialize": lambda self: value[0](),
"restore": lambda self, obj: value[1](obj)})
self._tasks_custom_environment_handler = cls()
else:
raise ValueError(f"Invalid type {type(value)}. Expected a CustomEnvironmentHandler object.")

In the longer term, we could also look to validate the values in setattr against the type hints.

Originally posted by @sveneberth in #1106 (comment)