question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TypeError: 'Request' object does not support item assignment

See original GitHub issue

I’m creating an API gateway using fastAPI for some microservices, which in I authenticate user via Azure AD via this library. So I’m trying to take out the user info from the request object (request.state.user) and inject my own token in it to later pass to other microservices. I can’t do this in middleware, cause user info is getting added after it. But I tried creating an decorator:

from functools import wraps

def token_injector(function):
    @wraps(function)
    def wrap_function(*args, **kwargs):
        user: User = kwargs['request'].state.user
        new_token = generate_token(user)
        kwargs["new_token"] = new_token # or kwargs['request']["new_token"] = new_token
        return function(*args, **kwargs)
    return wrap_function

and for this approach, I’m getting these errors:

ValueError: [TypeError("'coroutine' object is not iterable"), TypeError('vars() argument must have dict attribute')] and

TypeError: 'Request' object does not support item assignment

Can you please guide me how did you inject user in request.state, so I can do the same?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
JonasKscommented, Nov 4, 2021

Hi! Thank you for following up letting me know it’s been resolved. I don’t have enough reputation on SO to follow up there.

There are a few questions here, so I’ll try to answer them as best as I can. Let me know if anything is unclear.

First off, I agree with the approach you’ve landed on. Chaining dependencies is the way to make sure something is “in the right order”. It is also the approach I’ve described in the docs here, and also deomonstrated in the demo project in this view, using this dependency.

The user model is defined here, and is created here.

I would recommend you to not “get rid of” the User object, but maybe create your own user model and append to it. Here’s an example:

from pydantic import Field
from fastapi_azure_auth.user import User
from typing import Optional

class CustomUser(User):
    new_token: Optional[str] = Field(default=None)

and then in your own dependency do this:

from fastapi import Depends
from fastapi_azure_auth.auth import InvalidAuth
from fastapi_azure_auth.user import User

async def new_user(user: User = Depends(azure_scheme)) -> CustomUser:
    new_token = 'ey....'
    new_user = CustomUser(**user.dict() | {'new_token': new_token})

I hope this code runs (I haven’t been able to test it), but if not, let me know and I’ll write a real example for you tomorrow.

0reactions
JonasKscommented, Nov 5, 2021

Ah, yes, I forgot about that part of the question 👍

Read more comments on GitHub >

github_iconTop Results From Across the Web

Object does not support item assignment error - Stack Overflow
The error seems clear: model objects do not support item assignment. MyModel.objects.latest('id')['foo'] = 'bar' will throw this same error.
Read more >
TypeError: 'type' object does not support item assignment
The python error TypeError: 'type' object does not support item assignment occurs when an index value is inserted or changed in a variable...
Read more >
'Request' object does not support item assignment #36 - GitHub
Sanic no longer allows for item assignment in the request object. I am using sanic==20.6.3 and sanic-prometheus==0.2.0. ERROR:sanic.root: ...
Read more >
'Response' object does not support item assignment - mkt.api ...
This error used to happen when the returned response is not a Django response (but a dict or a list for instance). I...
Read more >
Tuple Object Does Not Support Item Assignment. Why?
The error “tuple object does not support item assignment” is raised in Python when you try to modify an element of a tuple....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found