Search code examples
google-bigquery

Pub/Sub randomly gives "Missing 1 required positional argument: 'context'" when testing in-platform


I set up Google Cloud Functions in-platform in Python, typically using a Pub/Sub as a trigger.

My main function is always formatted as follows:

def sync(event, context):

When I edit the source code in-platform and use the in-platform testing tool, about 70% of the time, I get an error message that reads:

Missing 1 required positional argument: 'context'" 

I've spent hours and hours trying to figure out why it happens sometimes and doesn't others, but today was just repeatedly testing the same function again and again and noticed that, after a while, it started giving this error even though I had not changed a single letter of the code.

This is very frustrating, because it means that I have to deploy my code and trigger it manually to test it, which is time-consuming.

Any idea how to get around this?


Solution

  • It's possible that the context isn't supplied during testing. The test environment might not be simulating the same functional behavior as a real deployment.

    Your main call def sync(event, context) expects two arguments but it's only receiving one (the event data), hence the "Missing..." error message.

    Consider reviewing this document as it outlines the same situation as yours and suggests a solution accordingly. I also found an old discussion which relates to your use case.

    The discussion describes that background triggers will indeed follow your main call format but if it is a HTTP request then you need to use something like def test2(request).