Skip to content

class pubsub_v1.types.PubsubMessage is mangling Timestamps into DatetimeWithNanoseconds #1375

Open
@lazarillo

Description

@lazarillo

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":

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

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 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 "<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.

Metadata

Metadata

Labels

api: pubsubIssues related to the googleapis/python-pubsub API.priority: p2Moderately-important priority. Fix may not be included in next release.type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions