Skip to content

Commit

Permalink
Restore HiddenText.__eq__ and associated test
Browse files Browse the repository at this point in the history
  • Loading branch information
ichard26 committed Apr 8, 2024
1 parent b8cf8fa commit bde667d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/pip/_internal/utils/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import sys
import sysconfig
import urllib.parse
from dataclasses import dataclass, field
from dataclasses import dataclass
from functools import partial
from io import StringIO
from itertools import filterfalse, tee, zip_longest
Expand Down Expand Up @@ -584,16 +584,23 @@ def redact_auth_from_requirement(req: Requirement) -> str:
@dataclass(frozen=True)
class HiddenText:
secret: str
# The string being used for redaction doesn't also have to match,
# just the raw, original string.
redacted: str = field(compare=False)
redacted: str

def __repr__(self) -> str:
return f"<HiddenText {str(self)!r}>"

def __str__(self) -> str:
return self.redacted

# This is useful for testing.
def __eq__(self, other: Any) -> bool:
if type(self) != type(other):
return False

# The string being used for redaction doesn't also have to match,
# just the raw, original string.
return self.secret == other.secret


def hide_value(value: str) -> HiddenText:
return HiddenText(value, redacted="****")
Expand Down
14 changes: 14 additions & 0 deletions tests/unit/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,20 @@ def test_basic(self) -> None:
assert hidden.redacted == "######"
assert hidden.secret == "my-secret"

def test_equality_with_str(self) -> None:
"""
Test equality (and inequality) with str objects.
"""
hidden = HiddenText("secret", redacted="****")

# Test that the object doesn't compare equal to either its original
# or redacted forms.
assert hidden != hidden.secret
assert hidden.secret != hidden

assert hidden != hidden.redacted
assert hidden.redacted != hidden

def test_equality_same_secret(self) -> None:
"""
Test equality with an object having the same secret.
Expand Down

0 comments on commit bde667d

Please sign in to comment.