Gummie
Gummie

Reputation: 35

TypeError: 'coroutine' object does not support item assignment

Alright I've been trying to fix this for hours already so heres the error

File "bot.py", line 178, in sms_token
getters[(token)] = {}
TypeError: 'coroutine' object does not support item assignment

Then heres the code thats messing up

async def sms_token(ctx,days,cooldown,name):

tokens = get_tokens_data()


getters = tokens

token = (''.join([secrets.choice
('123456789.=/?@#$nuioqasmkj]{;')
for i in range(20)]))

await ctx.send(f"TOKEN:{token}\nDAYS:{days}\nCOOLDOWN:{cooldown}")
if str(token) in {tokens}:
    return False
else:
    getters[(token)] = {}
    getters[(token)]["days"] = 0
    getters[(token)]["cooldown"] = 0
with open("tokens.json", "w") as f:
    json.dump(tokens,f)
return True

What did I do wrong? please help me!!

Upvotes: 0

Views: 1803

Answers (1)

madbird
madbird

Reputation: 1379

Your getters (aka tokens) are a coroutine object. Try to await it to get the result:

- tokens = get_tokens_data()

->

+ tokens = await get_tokens_data() 

UPD: ok, I'll try to explain.

This is what you have:

async def get_tokens_data():
    return {'tokens': 'data'}

tokens_data = get_tokens_data()
print(tokens_data)
<coroutine object get_tokens_data at 0x7f88821a9d40>

And tokens_data is not a dict or list yet, it is a coroutine object, so you can't assign keys to token_data.

tokens_data['a'] = 10

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-76-03d025bb79e1> in <module>
      4 tokens_data = get_tokens_data()
      5 print(tokens_data)
----> 6 tokens_data['a'] = 10

TypeError: 'coroutine' object does not support item assignment

To fix this problem, just add await between = and get_tokens_data():

async def get_tokens_data():
    return {'tokens': 'data'}

tokens_data = await get_tokens_data()
#             ^^^^^
print(tokens_data)
tokens_data['a'] = 10
{'tokens': 'data'}
{'tokens': 'data', 'a': 10}

So that, tokens_data will contain not a coroutine itself but a result of its execution (dict). That's it.

Upvotes: 1

Related Questions