Skip to content

Commit

Permalink
i hate python 3.8
Browse files Browse the repository at this point in the history
  • Loading branch information
benedikt-bartscher committed Aug 9, 2024
1 parent f4295df commit ca9df0f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
7 changes: 4 additions & 3 deletions reflex/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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():
Expand Down
21 changes: 21 additions & 0 deletions reflex/utils/string.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit ca9df0f

Please sign in to comment.