4

I have made a very simple Python helper, to update a task in Asana with custom field on a task. it works on my local machine in terminal.

I am trying to add it to a Zapier 'Run Python' block, but get what looks like a generic error 'str' object has no attribute 'copy'

Here's the Python code which I'd appreciate any advice on why it wont run in a "Run Python" module in Zapier -- there's no str in these lines!!?

import requests

headers = {'Authorization':'Bearer 1/xxxxx'}
task_id = input_data['task_id']
data = {"data": {"custom_fields": {"1200278184463303":"#" + input_data['row_number']}}}

response = requests.put('https://app.asana.com/api/1.0/tasks/' + task_id, headers=headers, json=data)

return 'task #' + input_data['row_number'] + 'assigned'
Brad Sullivan
  • 97
  • 1
  • 1
  • 8
  • Did you forget to include the relevant code block? – Justin Oberle May 03 '21 at 15:20
  • You need to include the reproducable code. However, the error is self explanatory. You are trying to run a function, copy, on a variable that has a string value and not an object value. – Justin Oberle May 03 '21 at 15:22
  • @JustinOberle thats the entire code! it works in Visual Studio terminal, and my OS X terminal. Zapier tells me to start a "Run Python" code block as a step -- and to paste my code in there. im not trying to run any copy function. i guess Zapiers parser is? https://zapier.com/help/create/code-webhooks/use-python-code-in-zaps – Brad Sullivan May 03 '21 at 15:33
  • There is no possible way that is the entire code. When I copy and paste it into my editor I have syntax errors on basically every line saying certain things are not defined. for example, where is input_data defined? – Justin Oberle May 03 '21 at 15:36
  • 1
    I appreciate your replies but i am following this advice from the Zapier guide; If you encounter a problem, we recommend asking questions on StackOverflow and tagging them with "Zapier". Seems you're not familiar with the Zapier code block "Run Python". The input_data[] comes from that. – Brad Sullivan May 03 '21 at 15:54
  • Yup, no idea what that is. I will leave this for someone else. Good luck! – Justin Oberle May 03 '21 at 16:02

2 Answers2

9

I realize this is answered, but I wanted to add some context. Code by Zapier steps expect a dict to be returned; you're returning a string.

Zapier should throw a more explicit error here (something like "expected dict, got str), but isn't. Instead, it's calling .copy() on the output, which results in the error you're seeing:

>>> {}.copy()
{}

>>> ''.copy()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'str' object has no attribute 'copy'

'str' object has no attribute 'copy'

There are two options to fix it:

  1. Set a key on the pre-defined output dict (the currently accepted answer)
  2. Manually return a dict: return {'field_name': 'task #' + input_data['row_number'] + 'assigned'}

Either will work here.

xavdid
  • 5,092
  • 3
  • 20
  • 32
2

I had better luck with the following syntax (with your code in place):

output['outputfieldname'] = 'task #' + input_data['row_number'] + 'assigned'
Dan
  • 425
  • 2
  • 13