Skip to content

Commit

Permalink
remove_prefix method working same for all python versions
Browse files Browse the repository at this point in the history
  • Loading branch information
dashgin authored and kencochrane committed Feb 21, 2023
1 parent d90dfa8 commit a4b3f9f
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
3 changes: 3 additions & 0 deletions defender/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1176,3 +1176,6 @@ def test_remove_prefix(self):
"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")
10 changes: 3 additions & 7 deletions defender/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit a4b3f9f

Please sign in to comment.