-
Notifications
You must be signed in to change notification settings - Fork 214
Description
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 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-pubsubversion: 2.23.0
Steps to reproduce
- ?
- ?
Code example
from google.protobuf.timestamp_pb2 import Timestamp
from <myprotobufpkg> 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 belowStack 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 "<stdin>", line 1, in <module>
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)
<class 'proto.datetime_helpers.DatetimeWithNanoseconds'>
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, and downstream usage like the call to pubsub_v1.subscriber.message.Message, expect it to populate a Timestamp object.