Commit 429ae15d by Xiaofei Wang Committed by Copybara-Service

Show a different error message when loading Python None as StatusOr<T>.

PiperOrigin-RevId: 520495543
parent 59827596
...@@ -50,6 +50,10 @@ struct type_caster<absl::StatusOr<PayloadType>> { ...@@ -50,6 +50,10 @@ struct type_caster<absl::StatusOr<PayloadType>> {
value = cast_op<PayloadType>(std::move(payload_caster)); value = cast_op<PayloadType>(std::move(payload_caster));
return true; return true;
} }
if (src.is_none()) {
throw cast_error(
"None is not a valid value for a StatusOr<T> argument.");
}
StatusCaster status_caster; StatusCaster status_caster;
if (status_caster.load(src, convert)) { if (status_caster.load(src, convert)) {
absl::Status status = cast_op<absl::Status>(std::move(status_caster)); absl::Status status = cast_op<absl::Status>(std::move(status_caster));
......
...@@ -57,7 +57,7 @@ class StatusTest(parameterized.TestCase): ...@@ -57,7 +57,7 @@ class StatusTest(parameterized.TestCase):
status_example.check_statusor(None, status.StatusCode.CANCELLED) status_example.check_statusor(None, status.StatusCode.CANCELLED)
self.assertEqual( self.assertEqual(
str(ctx.exception), str(ctx.exception),
'An OK status is not a valid constructor argument to StatusOr<T>.') 'None is not a valid value for a StatusOr<T> argument.')
def test_return_status_return_type_from_doc(self): def test_return_status_return_type_from_doc(self):
self.assertEndsWith( self.assertEndsWith(
......
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