Skip to content

Pub/Sub API lists the wrong (slightly incompatible) type for Message.attributes #4390

Closed
@dlj-NaN

Description

@dlj-NaN

When a subscriber receives a message, the API says it has an 'attributes' member that is a dict:

https://github.com/GoogleCloudPlatform/google-cloud-python/blob/11e310ab9f63f06252ff2b9ada201fd8c11ce06c/pubsub/google/cloud/pubsub_v1/subscriber/message.py#L36

However, it is not, in fact, a dict; it is a protobuf map. The behaviour is quite different, which the Python protobuf API documentation explains at some length:

https://developers.google.com/protocol-buffers/docs/reference/python-generated#map-fields

This is not a small difference... the semantic gap is substantial, because using the .attributes type in place of a dict breaks code like this:

message_rev = None
if some_condition:
  try:
    message_rev = int(message.attributes['revision'])
  except KeyError:
    message_rev = DEFAULT

In this case, instead of assigning the value DEFAULT, this code simply raises a new exception that doesn't happen with dicts (it's a ValueError, because the try-block will execute int('')). This is an incompatible, breaking change.

Again, this is a subtle, but important, API difference. At a minimum, it seems like the PubSub API docs should point to the Protobuf docs, instead of simply listing the incorrect type.

Metadata

Metadata

Labels

api: pubsubIssues related to the Pub/Sub API.priority: p2Moderately-important priority. Fix may not be included in next release.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions