Commit 075bf549 by Ralf W. Grosse-Kunstleve Committed by Copybara-Service

Change `Status.__repr__` to default `__repr__`, to conform to usual expectations.

Before this change, `__repr__` is identical to `__str__`. There are countless articles on the web explaining the expected difference, for example:

* https://www.tutorialspoint.com/str-vs-repr-in-python

Quoting "Key differences":

* str(): Make object readable; Generate output to end user
* repr(): Required code that reproduces object; Generate output for developer

"Required code that reproduces object" is impractical for `Status`, mainly because there is not good way to concisely include the payload, but even just the `.message()` can potentially be very long. Therefore it is best to simply use the default `__repr__`, as is the case for the vast majority of types of significant complexity.

PiperOrigin-RevId: 476406024
parent 389a83e3
...@@ -218,7 +218,7 @@ void RegisterStatusBindings(module m) { ...@@ -218,7 +218,7 @@ void RegisterStatusBindings(module m) {
[](const absl::Status& s) { [](const absl::Status& s) {
return decode_utf8_replace(s.ToString()); return decode_utf8_replace(s.ToString());
}) })
.def("__repr__", .def("__str__",
[](const absl::Status& s) { [](const absl::Status& s) {
return decode_utf8_replace(s.ToString()); return decode_utf8_replace(s.ToString());
}) })
......
...@@ -178,16 +178,18 @@ class StatusTest(parameterized.TestCase): ...@@ -178,16 +178,18 @@ class StatusTest(parameterized.TestCase):
failure_status = status_example.make_status(status.StatusCode.CANCELLED) failure_status = status_example.make_status(status.StatusCode.CANCELLED)
self.assertFalse(status.is_ok(failure_status)) self.assertFalse(status.is_ok(failure_status))
def test_repr(self):
any_status = status_example.make_status(status.StatusCode.DATA_LOSS)
self.assertRegex(repr(any_status), r'<.*\.status.Status object at .*>')
def test_ok_to_string(self): def test_ok_to_string(self):
ok_status = status_example.make_status(status.StatusCode.OK) ok_status = status_example.make_status(status.StatusCode.OK)
self.assertEqual(ok_status.to_string(), 'OK') self.assertEqual(ok_status.to_string(), 'OK')
self.assertEqual(repr(ok_status), 'OK')
self.assertEqual(str(ok_status), 'OK') self.assertEqual(str(ok_status), 'OK')
def test_canonical_error_to_string(self): def test_canonical_error_to_string(self):
test_status = status.aborted_error('test') test_status = status.aborted_error('test')
self.assertEqual(test_status.to_string(), 'ABORTED: test') self.assertEqual(test_status.to_string(), 'ABORTED: test')
self.assertEqual(repr(test_status), 'ABORTED: test')
self.assertEqual(str(test_status), 'ABORTED: test') self.assertEqual(str(test_status), 'ABORTED: test')
def test_create_ok_status(self): def test_create_ok_status(self):
...@@ -206,7 +208,6 @@ class StatusTest(parameterized.TestCase): ...@@ -206,7 +208,6 @@ class StatusTest(parameterized.TestCase):
self.assertEqual(stx80.error_message(), '�') self.assertEqual(stx80.error_message(), '�')
self.assertEqual(stx80.to_string(), 'INVALID_ARGUMENT: �') self.assertEqual(stx80.to_string(), 'INVALID_ARGUMENT: �')
self.assertEqual(str(stx80), 'INVALID_ARGUMENT: �') self.assertEqual(str(stx80), 'INVALID_ARGUMENT: �')
self.assertEqual(repr(stx80), 'INVALID_ARGUMENT: �')
e = status.StatusNotOk(stx80) e = status.StatusNotOk(stx80)
self.assertEqual(str(e), 'INVALID_ARGUMENT: �') self.assertEqual(str(e), 'INVALID_ARGUMENT: �')
...@@ -434,7 +435,6 @@ class StatusOrTest(absltest.TestCase): ...@@ -434,7 +435,6 @@ class StatusOrTest(absltest.TestCase):
failure_result = status_example.make_failure_status_or( failure_result = status_example.make_failure_status_or(
status.StatusCode.CANCELLED) status.StatusCode.CANCELLED)
self.assertEqual(failure_result.to_string(), 'CANCELLED: ') self.assertEqual(failure_result.to_string(), 'CANCELLED: ')
self.assertEqual(repr(failure_result), 'CANCELLED: ')
self.assertEqual(str(failure_result), 'CANCELLED: ') self.assertEqual(str(failure_result), 'CANCELLED: ')
def test_overriding_in_python(self): def test_overriding_in_python(self):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment