Added descriptor for backend vars (#2618)
* Added descriptor for backend vars * Fixed darglint issue
This commit is contained in:
parent
9d051d383e
commit
f9d219407f
@ -380,6 +380,9 @@ class BaseState(Base, ABC, extra=pydantic.Extra.allow):
|
|||||||
|
|
||||||
cls._init_var_dependency_dicts()
|
cls._init_var_dependency_dicts()
|
||||||
|
|
||||||
|
for name in cls.new_backend_vars:
|
||||||
|
setattr(cls, name, PrivateVarDescriptor())
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _copy_fn(fn: Callable) -> Callable:
|
def _copy_fn(fn: Callable) -> Callable:
|
||||||
"""Copy a function. Used to copy ComputedVars and EventHandlers from mixins.
|
"""Copy a function. Used to copy ComputedVars and EventHandlers from mixins.
|
||||||
@ -2288,3 +2291,20 @@ def code_uses_state_contexts(javascript_code: str) -> bool:
|
|||||||
True if the code attempts to access a member of StateContexts.
|
True if the code attempts to access a member of StateContexts.
|
||||||
"""
|
"""
|
||||||
return bool("useContext(StateContexts" in javascript_code)
|
return bool("useContext(StateContexts" in javascript_code)
|
||||||
|
|
||||||
|
|
||||||
|
class PrivateVarDescriptor:
|
||||||
|
"""A descriptor to raise error in case private vars is accessed on thr frontend."""
|
||||||
|
|
||||||
|
def __get__(self, *args, **kwargs):
|
||||||
|
"""Raise error on get value.
|
||||||
|
|
||||||
|
|
||||||
|
Args:
|
||||||
|
*args: The args to pass to the function.
|
||||||
|
**kwargs: The kwargs to pass to the function.
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
TypeError: if a background variable is used in frontend.
|
||||||
|
"""
|
||||||
|
raise TypeError("Backend Vars cannot be accessed on the frontend.")
|
||||||
|
Loading…
Reference in New Issue
Block a user