Skip to content

Commit a91fa3d

Browse files
OpenHTF Ownerscopybara-github
authored andcommitted
Modify PercentTolerance.__str__ to only include the marginal percentage in the description if marginal_percent is not None.
PiperOrigin-RevId: 908371025
1 parent f5c21e2 commit a91fa3d

2 files changed

Lines changed: 17 additions & 7 deletions

File tree

openhtf/util/validators.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -498,8 +498,12 @@ def is_marginal(self, value) -> bool:
498498
self.marginal_maximum <= value < self.maximum)
499499

500500
def __str__(self) -> str:
501-
return "'x' is within {}% of {}. Marginal: {}% of {}".format(
502-
self.percent, self.expected, self.marginal_percent, self.expected)
501+
string_parts = [(f"'x' is within {self.percent}% of {self.expected}.")]
502+
if self.marginal_percent is not None:
503+
string_parts.append(
504+
f'Marginal: {self.marginal_percent}% of {self.expected}'
505+
)
506+
return ' '.join(string_parts)
503507

504508
def __eq__(self, other) -> bool:
505509
return (isinstance(other, type(self)) and

test/util/validators_test.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -314,11 +314,17 @@ def test_not_equals_when_not_equivalent(self):
314314
for validator in [validator_b, validator_c, validator_d]:
315315
self.assertNotEqual(validator_a, validator)
316316

317-
def test_string_representation_does_not_raise(self):
318-
validator_a = validators.WithinPercent(expected=100, percent=10)
319-
str(validator_a)
320-
# Check that we constructed a usable validator.
321-
self.assertTrue(validator_a(100))
317+
def test_string_representation_without_marginal_percent(self):
318+
validator = validators.WithinPercent(expected=100, percent=10)
319+
self.assertEqual(str(validator), "'x' is within 10% of 100.")
320+
321+
def test_string_representation_with_marginal_percent(self):
322+
validator = validators.WithinPercent(
323+
expected=100, percent=10, marginal_percent=5
324+
)
325+
self.assertEqual(
326+
str(validator), "'x' is within 10% of 100. Marginal: 5% of 100"
327+
)
322328

323329
def test_is_deep_copyable(self):
324330
validator_a = validators.WithinPercent(expected=100, percent=10)

0 commit comments

Comments
 (0)