Skip to content
This repository has been archived by the owner on Feb 1, 2024. It is now read-only.

Creating a task fails with "expected google.protobuf.Timestamp got datetime.datetime." #147

Closed
jceresini opened this issue Jul 30, 2021 · 5 comments
Assignees
Labels
api: cloudtasks Issues related to the googleapis/python-tasks API. priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.

Comments

@jceresini
Copy link

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: MacOS
  • Python version: python --version 3.9.6
  • pip version: pip --version 21.1.3
  • google-cloud-tasks version: pip show google-cloud-tasks 2.5.1

Steps to reproduce

  1. Download the code sample found here: https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/appengine/flexible/tasks/create_app_engine_queue_task.py
  2. Execute it with valid parameters: python create_app_engine_queue_task.py --project my-project --location us-central1 --in_seconds 30 --queue myqueue
  3. See error:
Traceback (most recent call last):
  File "/path/to/create_app_engine_queue_task.py", line 110, in <module>
    create_task(
  File "/path/to/create_app_engine_queue_task.py", line 68, in create_task
    response = client.create_task(parent=parent, task=task)
  File "/path/to/.venv/lib/python3.9/site-packages/google/cloud/tasks_v2/services/cloud_tasks/client.py", line 1700, in create_task
    request.task = task
  File "/path/to/.venv/lib/python3.9/site-packages/proto/message.py", line 632, in __setattr__
    pb_value = marshal.to_proto(pb_type, value)
  File "/path/to/.venv/lib/python3.9/site-packages/proto/marshal/marshal.py", line 208, in to_proto
    pb_value = rule.to_proto(value)
  File "/path/to/.venv/lib/python3.9/site-packages/proto/marshal/rules/message.py", line 32, in to_proto
    return self._descriptor(**value)
TypeError: Parameter to MergeFrom() must be instance of same class: expected google.protobuf.Timestamp got datetime.datetime.

Code example

See python code sample here: https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/appengine/flexible/tasks/create_app_engine_queue_task.py
Also linked to from the google cloud documentation

Stack trace

Traceback (most recent call last):
  File "/path/to/create_app_engine_queue_task.py", line 110, in <module>
    create_task(
  File "/path/to/create_app_engine_queue_task.py", line 68, in create_task
    response = client.create_task(parent=parent, task=task)
  File "/path/to/.venv/lib/python3.9/site-packages/google/cloud/tasks_v2/services/cloud_tasks/client.py", line 1700, in create_task
    request.task = task
  File "/path/to/.venv/lib/python3.9/site-packages/proto/message.py", line 632, in __setattr__
    pb_value = marshal.to_proto(pb_type, value)
  File "/path/to/.venv/lib/python3.9/site-packages/proto/marshal/marshal.py", line 208, in to_proto
    pb_value = rule.to_proto(value)
  File "/path/to/.venv/lib/python3.9/site-packages/proto/marshal/rules/message.py", line 32, in to_proto
    return self._descriptor(**value)
TypeError: Parameter to MergeFrom() must be instance of same class: expected google.protobuf.Timestamp got datetime.datetime.

Making sure to follow these steps will guarantee the quickest resolution possible.

Thanks!

@product-auto-label product-auto-label bot added the api: cloudtasks Issues related to the googleapis/python-tasks API. label Jul 30, 2021
@yoshi-automation yoshi-automation added the triage me I really want to be triaged. label Aug 1, 2021
@jceresini
Copy link
Author

I found another issue related to this, and I found the solution.

The python link to "View on github" on this page: https://cloud.google.com/tasks/docs/samples/cloud-tasks-taskqueues-new-task links to this python sample: https://github.com/GoogleCloudPlatform/python-docs-samples/blob/HEAD/appengine/flexible/tasks/snippets.py

I think that documentation needs to be updated to link to the samples in this repo. Is that something I should raise here?

Here's a link to the solution for anyone that might find this issue: #62 (comment)

@averikitsch averikitsch added priority: p1 Important issue which blocks shipping the next release. Will be fixed prior to next release. type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design. and removed triage me I really want to be triaged. labels Aug 3, 2021
@averikitsch
Copy link
Contributor

This has been fixed: GoogleCloudPlatform/python-docs-samples#6509. However, @busunkim96 is it possible to require either datetime or the timestamp protobuf?

@jceresini
Copy link
Author

One potential issue with using a datetime is that functions like datetime.datetime.now() and datetime.datetime.utcnow() are timezone unaware. As a result, datetime.datetime.utcnow().isoformat() returns something like this 2021-08-03T17:43:05.349326 with no timezone information.

The cloudtasks API (direct API calls) rejects ISO8601 datetimes that don't contain a timezone. But submitting with datetime.datetime.utcnow().isoformat() + 'Z' works.

It looks like Timestamp.FromDatetime() assumes the input is in UTC.

That being said, I have another issue open where I mention that its hard to tell when to use certain types of objects when using this library: #148
Its a bit more confusing if sometimes I need to use dictionaries, sometimes i have to use types included in this library, and sometimes I have to use objects from google.protobuf. I'm not sure we're going to move forward using this library for these reason, we're likely going to use the discovery client as it's easier to understand and get started with. That's not intended as a complaint, but I wanted to provide feedback on my experience so far.

@busunkim96
Copy link
Contributor

Hmm. Accepting both protobuf timestamps and Python datetimes is by design. The proto-plus library is supposed to convert datetimes to protobufs (https://proto-plus-python.readthedocs.io/en/stable/marshal.html). It looks like that's not happening as expected here. I've assigned this issue to myself to investigate more closely.

@jceresini Thanks for the heads up, it sounds like our samples should attach timezones to datetime objects per recommendation in the Python docs so there are no accidental conversions: https://docs.python.org/3/library/datetime.html#datetime.datetime.utcnow

Warning Because naive datetime objects are treated by many datetime methods as local times, it is preferred to use aware datetimes to represent times in UTC. As such, the recommended way to create an object representing the current time in UTC is by calling datetime.now(timezone.utc).

@busunkim96 busunkim96 self-assigned this Aug 9, 2021
@busunkim96 busunkim96 added priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns. and removed type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design. priority: p1 Important issue which blocks shipping the next release. Will be fixed prior to next release. labels Aug 9, 2021
gcf-merge-on-green bot pushed a commit to GoogleCloudPlatform/python-docs-samples that referenced this issue Aug 17, 2021
Follow up to discussion in googleapis/python-tasks#147 (comment).

If this is an OK blanket recommendation to make, I will follow up with a PR to adjust all the calls to `datetime.datetime` in this repository.

For supporting documentation, see warnings for these two methods:
- https://docs.python.org/3/library/datetime.html#datetime.datetime.utcfromtimestamp
- https://docs.python.org/3/library/datetime.html#datetime.datetime.utcnow
@yoshi-automation yoshi-automation added 🚨 This issue needs some love. and removed 🚨 This issue needs some love. labels Nov 8, 2021
@engelke
Copy link
Contributor

engelke commented Jan 12, 2022

Fixed in #6581

@engelke engelke closed this as completed Jan 12, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api: cloudtasks Issues related to the googleapis/python-tasks API. priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.
Projects
None yet
Development

No branches or pull requests

5 participants