googleapis / googleapis/google-cloud-python

class `pubsub_v1.types.PubsubMessage` is mangling `Timestamps` into `DatetimeWithNanoseconds`

未关闭
#15,637 3 条评论 0 个 reaction 已指派 1 人 已被 @abbrowne126 认领 在 GitHub 查看
api: pubsub priority: p2 type: bug
主要语言
Python
星标
5.4k
派生
1.8k
平均合并
3 天 4 小时
30 天内合并 PR
122

描述

Thanks for stopping by to let us know something could be better!

**PLEASE READ**: If you have a support contract with Google, please create an issue in the [support console](https://cloud.google.com/support/) instead of filing on GitHub. This will ensure a timely response.

Please run down the following list and make sure you've tried the usual "quick fixes":

- Search the issues already opened: https://github.com/googleapis/python-pubsub/issues
- Search StackOverflow: https://stackoverflow.com/questions/tagged/google-cloud-platform+python

If you are still having issues, please be sure to include as much information as possible:

#### Environment details

- OS type and version: Mac OS 15.3.1
- Python version: 3.11
- pip version: 25.0.1
- `google-cloud-pubsub` version: 2.23.0

#### Steps to reproduce

1. ?
2. ?

#### Code example

```python
from google.protobuf.timestamp_pb2 import Timestamp
from import protoclass # Here just use *any `Message`* from any `_pb2.py` protobuf file.
from google.cloud import pubsub_v1
from queue import Queue

payload = protoclass()
publish_time = Timestamp()
pubsub_v1.types.PubsubMessage(data=payload.SerializeToString(), publish_time = publish_time)

queue = Queue()
message = pubsub_v1.subscriber.message.Message(message=pubsub_message, ack_id="whatever", delivery_attempt=2, request_queue=queue) # This will fail with wrong data type, see below
```

#### Stack trace
```
>>> message = pubsub_v1.subscriber.message.Message(message=pubsub_message, ack_id="whatever", delivery_attempt=2, request_queue=queue)
Traceback (most recent call last):
File "", line 1, in
File ".../3.11/lib/python/site-packages/google/cloud/pubsub_v1/subscriber/message.py", line 141, in __init__
message.publish_time.seconds + message.publish_time.nanos / 1e9,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'DatetimeWithNanoseconds' object has no attribute 'seconds'. Did you mean: 'second'?
```

#### Further Details

As you can see above, *somehow* the `pubsub_message` object has altered the `publish_time` from a `Timestamp()` object to a `DatetimeWithNanoseconds` object.

If you step back, you can see this actually happens *not* when trying to create the `Message` object, but when *successfully* creating the `PubsubMessage` object:

```
>>> pubsub_message.publish_time
DatetimeWithNanoseconds(1970, 1, 1, 0, 0, tzinfo=datetime.timezone.utc)
>>> type(pubsub_message.publish_time)

```

Therefore, when the `pubsub_v1.types.PubsubMessage` object creation call _properly receives a `Timestamp` object_, it is *mangling* it into a `DatetimeWithNanoseconds` object, _even though_ it both [says that it creates a `Timestamp` object](https://cloud.google.com/python/docs/reference/pubsub/latest/google.cloud.pubsub_v1.types.PubsubMessage), and downstream usage like the call to `pubsub_v1.subscriber.message.Message`, *expect it* to populate a `Timestamp` object.

贡献指南

打开贡献指南

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。