From ca9df0f86c2d85d7670ff9779934c31effbe54d5 Mon Sep 17 00:00:00 2001 From: Benedikt Bartscher Date: Sat, 10 Aug 2024 00:24:28 +0200 Subject: [PATCH] i hate python 3.8 --- reflex/state.py | 7 ++++--- reflex/utils/string.py | 21 +++++++++++++++++++++ 2 files changed, 25 insertions(+), 3 deletions(-) create mode 100644 reflex/utils/string.py diff --git a/reflex/state.py b/reflex/state.py index f28d427505..a996ab59a2 100644 --- a/reflex/state.py +++ b/reflex/state.py @@ -57,6 +57,7 @@ from reflex.utils.exceptions import ImmutableStateError, LockExpiredError from reflex.utils.exec import is_testing_env from reflex.utils.serializers import SerializedType, serialize, serializer +from reflex.utils.string import remove_prefix from reflex.vars import BaseVar, ComputedVar, Var, computed_var if TYPE_CHECKING: @@ -1175,9 +1176,9 @@ def get_substate(self, path: Sequence[str] | Type[S]) -> BaseState | S: ValueError: If the substate is not found. """ if isinstance(path, type): - path = ( - path.get_full_name().removeprefix(f"{self.get_full_name()}.").split(".") - ) + path = remove_prefix( + text=path.get_full_name(), prefix=f"{self.get_full_name()}." + ).split(".") if len(path) == 0: return self if path[0] == self.get_name(): diff --git a/reflex/utils/string.py b/reflex/utils/string.py new file mode 100644 index 0000000000..e758302278 --- /dev/null +++ b/reflex/utils/string.py @@ -0,0 +1,21 @@ +"""String utility functions.""" + +import sys + + +def remove_prefix(text: str, prefix: str) -> str: + """Remove a prefix from a string, if present. + This can be removed once we drop support for Python 3.8. + + Args: + text: The string to remove the prefix from. + prefix: The prefix to remove. + + Returns: + The string with the prefix removed, if present. + """ + if sys.version_info >= (3, 9): + return text.removeprefix(prefix) + if text.startswith(prefix): + return text[len(prefix) :] + return text