diff --git a/mentions/config.py b/mentions/config.py index 8536b5d..c3cbfb0 100644 --- a/mentions/config.py +++ b/mentions/config.py @@ -3,7 +3,7 @@ from urllib.parse import urljoin from mentions import options -from mentions.util import get_domain +from mentions.util import compatibility, get_domain def base_url() -> str: @@ -97,8 +97,8 @@ def _domain_in_set(domain: str, domains: Set[str]) -> bool: return True if d.startswith("*."): - root_domain = d.removeprefix("*.") - remaining_prefix = domain.removesuffix(root_domain) + root_domain = compatibility.removeprefix(d, "*.") + remaining_prefix = compatibility.removesuffix(domain, root_domain) if remaining_prefix == "" or remaining_prefix.endswith("."): return True diff --git a/mentions/util/compatibility.py b/mentions/util/compatibility.py new file mode 100644 index 0000000..83e1eb3 --- /dev/null +++ b/mentions/util/compatibility.py @@ -0,0 +1,21 @@ +"""Backport features of newer Python releases than our minimum target version. + +All functions should contain information about their standard library equivalent +and the Python version in which it becomes available. + +These should be removed and replaced with standard library equivalents when +we update our minimum target version.""" + + +def removeprefix(text: str, prefix: str) -> str: + """Backport of `str.removeprefix` introduced in Python 3.9""" + if text.startswith(prefix): + return text[len(prefix) :] + return text + + +def removesuffix(text: str, suffix: str) -> str: + """Backport of `str.removesuffix` introduced in Python 3.9""" + if text.endswith(suffix): + return text[: -len(suffix)] + return text diff --git a/tests/tests/test_util/__init__.py b/tests/tests/test_util/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/tests/test_util/test_compatibility.py b/tests/tests/test_util/test_compatibility.py new file mode 100644 index 0000000..968bc23 --- /dev/null +++ b/tests/tests/test_util/test_compatibility.py @@ -0,0 +1,22 @@ +from mentions.util.compatibility import removeprefix, removesuffix +from tests.tests.util.testcase import SimpleTestCase + + +class CompatibilityTests(SimpleTestCase): + def test_str_removeprefix(self): + func = removeprefix + self.assertEqual(func("abcde", "ab"), "cde") + self.assertEqual(func("1248", "1"), "248") + + self.assertEqual(func("abcde", "bc"), "abcde") + self.assertEqual(func("abcde", "de"), "abcde") + self.assertEqual(func("abcde", "abcdef"), "abcde") + + def test_str_removesuffix(self): + func = removesuffix + self.assertEqual(func("abcde", "de"), "abc") + self.assertEqual(func("1248", "248"), "1") + + self.assertEqual(func("abcde", "ab"), "abcde") + self.assertEqual(func("abcde", "abcd"), "abcde") + self.assertEqual(func("abcde", "abcdef"), "abcde")