diff --git a/src/browserbase/_utils/_utils.py b/src/browserbase/_utils/_utils.py index 199cd231..39e3fe82 100644 --- a/src/browserbase/_utils/_utils.py +++ b/src/browserbase/_utils/_utils.py @@ -370,7 +370,7 @@ def removesuffix(string: str, suffix: str) -> str: Backport of `str.removesuffix` for Python < 3.9 """ - if string.endswith(suffix): + if suffix and string.endswith(suffix): return string[: -len(suffix)] return string diff --git a/tests/test_utils/test_utils.py b/tests/test_utils/test_utils.py new file mode 100644 index 00000000..6b523538 --- /dev/null +++ b/tests/test_utils/test_utils.py @@ -0,0 +1,31 @@ +from __future__ import annotations + +import pytest + +from browserbase._utils import removeprefix, removesuffix + + +@pytest.mark.parametrize( + ("string", "prefix"), + [ + ("browserbase", "browser"), + ("browserbase", "base"), + ("browserbase", ""), + ("", ""), + ], +) +def test_removeprefix_matches_stdlib(string: str, prefix: str) -> None: + assert removeprefix(string, prefix) == string.removeprefix(prefix) + + +@pytest.mark.parametrize( + ("string", "suffix"), + [ + ("browserbase", "base"), + ("browserbase", "browser"), + ("browserbase", ""), + ("", ""), + ], +) +def test_removesuffix_matches_stdlib(string: str, suffix: str) -> None: + assert removesuffix(string, suffix) == string.removesuffix(suffix)