googleapis / googleapis/google-cloud-python

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

Abierto
#15,637 3 comentarios 0 reacciones 1 asignado Reclamado por @abbrowne126 Ver en GitHub
api: pubsub priority: p2 type: bug
Lenguaje dominante
Python
Estrellas
5.4k
Forks
1.8k
Merge medio
3 d 4 h
PR fusionados (30 d)
122

Descripción

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.

Guía de contribución

Abrir la guía de contribución

Evaluación

Este issue todavía no se ha evaluado.

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.