state: Leading underscore functions are not backend vars (#1268)

This commit is contained in:
Masen Furer 2023-06-28 15:32:49 -07:00 committed by GitHub
parent 3b3fc4f965
commit cd63297fe4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 0 deletions

View File

@ -10,6 +10,7 @@ import traceback
import urllib.parse
from abc import ABC
from collections import defaultdict
from types import FunctionType
from typing import (
Any,
AsyncIterator,
@ -197,6 +198,7 @@ class State(Base, ABC, extra=pydantic.Extra.allow):
for name, value in cls.__dict__.items()
if types.is_backend_variable(name)
and name not in cls.inherited_backend_vars
and not isinstance(value, FunctionType)
}
cls.backend_vars = {**cls.inherited_backend_vars, **cls.new_backend_vars}

View File

@ -1087,3 +1087,18 @@ def test_computed_var_depends_on_parent_non_cached():
IS_HYDRATED: False,
}
assert counter == 6
def test_backend_method():
"""A method with leading underscore should be callable from event handler."""
class BackendMethodState(State):
def _be_method(self):
return True
def handler(self):
assert self._be_method()
bms = BackendMethodState()
bms.handler()
assert bms._be_method()