gemini-code-assist[bot] commented on code in PR #38985:
URL: https://github.com/apache/beam/pull/38985#discussion_r3422653943
##########
sdks/python/apache_beam/yaml/yaml_io.py:
##########
@@ -393,14 +399,18 @@ def mapper(msg):
values[attr] = msg.attributes[attr]
if attributes_map:
values[attributes_map] = msg.attributes
+ if publish_time_field:
+ values[publish_time_field] = Timestamp.from_utc_datetime(
+ msg.publish_time)
Review Comment:

In production, `msg.publish_time` returned by `ReadFromPubSub` is already an
instance of `Timestamp` (from `apache_beam.utils.timestamp`). Calling
`Timestamp.from_utc_datetime` on it directly will raise a `ValueError` because
it expects a `datetime.datetime` object.
To safely handle both the production case (where it is a `Timestamp`), the
test case (where it is a `datetime.datetime`), and cases where it might be
`None`, we should check the type of `msg.publish_time` before converting.
```python
if publish_time_field:
if isinstance(msg.publish_time, Timestamp):
values[publish_time_field] = msg.publish_time
elif msg.publish_time is not None:
values[publish_time_field] = Timestamp.from_utc_datetime(
msg.publish_time)
else:
values[publish_time_field] = None
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]