An error about annotations in __future__ annotations #6465
Replies: 4 comments
-
|
This doesn't directly solve your problem, but instead of using a decorator, you could add a dependency to check the logged in user: def get_logged_in_user(req: Request) -> User:
"""Get the logged in user info, otherwise raise 401 unauthorized"""
# get session from cookie, or Bearer Token
# maybe even the built-in http auth basic stuff, https://fastapi.tiangolo.com/tutorial/security/simple-oauth2/
user: Optional[User] = get_user(req)
if not user:
raise HTTPException(401)
return user
@app.post("...") # could also put Depends in dependencies=[] here if you don't need the user object
def create_item(item: Item, user: User = Depends(get_logged_in_user)):
...I think this probably the preferred way to do it, even |
Beta Was this translation helpful? Give feedback.
-
|
Thanks. I added to my project ...
func_globals = getattr(call, "__globals__", {})
func_globals.update(**getattr(func, '__extra_globals__', {}))
...# jwt_tools.py
from functools import wraps
def login_required(func):
@wraps(func)
def wrapper(*args, **kwargs):
print('login')
return func(*args, **kwargs)
# copy __globals__
wrapper.__extra_globals__ = getattr(func, '__globals__', {})
wrapper.__extra_globals__.update(**getattr(func, "__extra_globals__", {}))
return wrapper |
Beta Was this translation helpful? Give feedback.
-
|
PR that should address this issue: #11355 |
Beta Was this translation helpful? Give feedback.
-
|
This was fixed by #5077, works well since FastAPI 0.123.5 |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
First Check
Commit to Help
Example Code
Description
i'm using
__future__ annotationsand i got an error about annotations.error ouput:
and i read the souce code https://github.com/tiangolo/fastapi/blob/0.70.0/fastapi/dependencies/utils.py#L247 :
@login_requiredis decorated by@app, as a resultglobalns = getattr(call, "__globals__", {})not includedItemandannotationisstrin__future__ annotationsenvironment.So what should I do?
Operating System
Windows
Operating System Details
windows 10
FastAPI Version
0.70.0
Python Version
3.8.5
Additional Context
No response
Beta Was this translation helpful? Give feedback.
All reactions