remove_prefix method working same for all python versions

This commit is contained in:
Dashgin 2022-06-30 16:27:36 +04:00 committed by Ken Cochrane
parent d90dfa8db7
commit a4b3f9f332
2 changed files with 6 additions and 7 deletions

View file

@ -1176,3 +1176,6 @@ class TestUtils(DefenderTestCase):
"defender:blocked:ip:192.168.24.24", "defender:blocked:"), "ip:192.168.24.24")
self.assertEqual(utils.remove_prefix(
"defender:blocked:username:johndoe", "defender:blocked:"), "username:johndoe")
self.assertEqual(utils.remove_prefix(
"defender:blocked:username:johndoe", "blocked:username:"),
"defender:blocked:username:johndoe")

View file

@ -129,14 +129,10 @@ def get_username_blocked_cache_key(username):
def remove_prefix(string, prefix):
if string.startswith(prefix):
return string[len(prefix):]
return string
# backwards compatibility for str.removeprefix for python < 3.9
if sys.version_info < (3, 9):
if string.startswith(prefix):
return string[len(prefix) :]
return string
return string.removeprefix(prefix)
def strip_keys(key_list):