Skip to content

Commit 83c9d61

Browse files
author
Timmy Welch
committed
Fix lstrip
1 parent 8706852 commit 83c9d61

2 files changed

Lines changed: 29 additions & 10 deletions

File tree

src/pyinfra/api/maskstring.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ def index(self, sub, start=0, end=sys.maxsize) -> int:
198198
return self.unmask().index(sub.unmask(), start, end)
199199
return super().index(sub, start, end)
200200

201+
# Not sure if this should return the unmasked result or the masked result
201202
# @override
202203
# def isalpha(self) -> bool:
203204
# return self.unmask().isalpha()
@@ -265,13 +266,14 @@ def lower(self) -> "MaskString":
265266

266267
@override
267268
def lstrip(self, chars=None) -> "MaskString":
268-
s_chars = chars
269+
chars_s = chars
269270
if isinstance(chars, MaskString):
270-
s_chars = chars.unmask()
271-
# explicitly set chars to the empty string as we want the resulting masked string to stay the same
272-
chars = ""
273-
return MaskString(self.unmask().lstrip(s_chars), masked_value=super().lstrip(chars))
271+
# we want the resulting masked string to stay the same as otherwise it would most likely just strip all the characters
272+
chars_s = chars.unmask()
273+
return MaskString(self.unmask().lstrip(chars_s), masked_value=str(self))
274+
return MaskString(self.unmask().lstrip(chars), masked_value=super().lstrip(chars))
274275

276+
# I don't know how maketrans works... default to using the masked value
275277
# maketrans = str.maketrans
276278

277279
@override
@@ -337,11 +339,12 @@ def rpartition(self, sep) -> tuple["MaskString", "MaskString", "MaskString"]:
337339
def rstrip(self, chars=None) -> "MaskString":
338340
chars_s = chars
339341
if isinstance(chars, MaskString):
340-
chars = chars.unmask()
341-
# explicitly set chars to the empty string as we want the resulting masked string to stay the same
342-
chars = ""
342+
# we want the resulting masked string to stay the same as otherwise it would most likely just strip all the characters
343+
chars_s = chars.unmask()
344+
return MaskString(self.unmask().rstrip(chars_s), masked_value=str(self))
343345
return MaskString(self.unmask().rstrip(chars_s), masked_value=super().rstrip(chars))
344346

347+
# There is no way to keep the split functions "in-sync" with the masked string and the raw_value
345348
# @override
346349
# def split(self, sep=None, maxsplit=-1) -> list[MaskString]:
347350
# if isinstance(sep, MaskString):
@@ -371,9 +374,9 @@ def startswith(self, prefix, start=0, end=sys.maxsize) -> bool:
371374
def strip(self, chars=None) -> "MaskString":
372375
chars_s = chars
373376
if isinstance(chars, MaskString):
377+
# we want the resulting masked string to stay the same as otherwise it would most likely just strip all the characters
374378
chars_s = chars.unmask()
375-
# explicitly set chars to the empty string as we want the resulting masked string to stay the same
376-
chars = ""
379+
return MaskString(self.unmask().strip(chars_s), masked_value=str(self))
377380
return MaskString(self.unmask().strip(chars_s), masked_value=super().strip(chars))
378381

379382
@override
@@ -384,6 +387,7 @@ def swapcase(self) -> "MaskString":
384387
def title(self) -> "MaskString":
385388
return MaskString(self.unmask().title(), masked_value=super().title())
386389

390+
# I don't know how translate works... default to using the masked value
387391
# @override
388392
# def translate(self, *args) -> 'MaskString':
389393
# return MaskString(self.unmask().translate(*args))

tests/test_api/test_api_maskstring.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,11 @@ def test_lstrip(self):
256256
assert new_s.unmask() == "secret**"
257257
assert new_s == "*MASKED*"
258258

259+
s = MaskString(" secret ")
260+
new_s = s.lstrip(MaskString(" "))
261+
assert new_s.unmask() == "secret "
262+
assert new_s == "*MASKED*"
263+
259264
def test_partition(self):
260265
s = MaskString("secret:value")
261266
before, sep, after = s.partition(":")
@@ -448,6 +453,11 @@ def test_rstrip(self):
448453
assert new_s.unmask() == "**secret"
449454
assert new_s == "*MASKED*"
450455

456+
s = MaskString(" secret ")
457+
new_s = s.rstrip(MaskString(" "))
458+
assert new_s.unmask() == " secret"
459+
assert new_s == "*MASKED*"
460+
451461
def test_startswith(self):
452462
s = MaskString("secret value")
453463
assert s.startswith("*")
@@ -478,6 +488,11 @@ def test_strip(self):
478488
assert new_s.unmask() == "secret"
479489
assert new_s == "*MASKED*"
480490

491+
s = MaskString(" secret ")
492+
new_s = s.strip(MaskString(" "))
493+
assert new_s.unmask() == "secret"
494+
assert new_s == "*MASKED*"
495+
481496
def test_swapcase(self):
482497
s = MaskString("Secret")
483498
new_s = s.swapcase()

0 commit comments

Comments
 (0)