gpt4 book ai didi

Python Pydantic 错误 : TypeError: __init__() takes exactly 1 positional argument (2 given)

转载 作者:行者123 更新时间:2023-12-05 08:35:53 25 4
gpt4 key购买 nike

我目前正在为大学开发一个 python fastapi 项目。每次我运行我的授权依赖项时,我都会收到以下错误:

ERROR:    Exception in ASGI application
Traceback (most recent call last):
File "C:\Python39\lib\site-packages\uvicorn\protocols\http\h11_impl.py", line 366, in run_asgi
result = await app(self.scope, self.receive, self.send)
File "C:\Python39\lib\site-packages\uvicorn\middleware\proxy_headers.py", line 75, in __call__
return await self.app(scope, receive, send)
File "C:\Python39\lib\site-packages\fastapi\applications.py", line 208, in __call__
await super().__call__(scope, receive, send)
File "C:\Python39\lib\site-packages\starlette\applications.py", line 112, in __call__
await self.middleware_stack(scope, receive, send)
File "C:\Python39\lib\site-packages\starlette\middleware\errors.py", line 181, in __call__
raise exc
File "C:\Python39\lib\site-packages\starlette\middleware\errors.py", line 159, in __call__
await self.app(scope, receive, _send)
File "C:\Python39\lib\site-packages\starlette\exceptions.py", line 82, in __call__
raise exc
File "C:\Python39\lib\site-packages\starlette\exceptions.py", line 71, in __call__
await self.app(scope, receive, sender)
File "C:\Python39\lib\site-packages\starlette\routing.py", line 656, in __call__
await route.handle(scope, receive, send)
File "C:\Python39\lib\site-packages\starlette\routing.py", line 259, in handle
await self.app(scope, receive, send)
File "C:\Python39\lib\site-packages\starlette\routing.py", line 61, in app
response = await func(request)
File "C:\Python39\lib\site-packages\fastapi\routing.py", line 216, in app
solved_result = await solve_dependencies(
File "C:\Python39\lib\site-packages\fastapi\dependencies\utils.py", line 496, in solve_dependencies
solved_result = await solve_dependencies(
File "C:\Python39\lib\site-packages\fastapi\dependencies\utils.py", line 525, in solve_dependencies
solved = await call(**sub_values)
File "e:\Dev\Ottomize\Ottomize\backend\app\auth_handler.py", line 60, in get_current_user
token_data = schemas.TokenData(username)
File "pydantic\main.py", line 322, in pydantic.main.BaseModel.__init__
TypeError: __init__() takes exactly 1 positional argument (2 given)

这是我的相关代码:

FAST API 端点:

@app.get("/user/current/info/", response_model=schemas.User)
async def user_info(current_user: schemas.User = Depends(auth_handler.get_current_active_user)):
return current_user

我的 auth_handler.py 中使用的函数:

from fastapi.security import OAuth2PasswordBearer
from jose import JWTError, jwt
from passlib.context import CryptContext
from datetime import datetime, timedelta
from typing import Optional


from fastapi import Depends, HTTPException, status
from . import crud, schemas, config, database

oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")

#gets user out of db
def get_user(username: str):
db = database.SessionLocal()
return crud.get_user_by_username(db, username)

#gets current user
async def get_current_user(token: str = Depends(oauth2_scheme)):
credentials_exception = HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Could not validate credentials",
headers={"WWW-Authenticate": "Bearer"},
)
try:
payload = jwt.decode(token, config.SECRET_KEY, algorithms=[config.ALGORITHM])
username: str = payload.get("sub")
if username is None:
raise credentials_exception
token_data = schemas.TokenData(username)
except JWTError:
raise credentials_exception
user = get_user(token_data.username)
if user is None:
raise credentials_exception
return user

#gets current user if active
async def get_current_active_user(current_user: schemas.User = Depends(get_current_user)):
if current_user.disabled:
raise HTTPException(status_code=400, detail="Inactive user")
return current_user

我的 crud.py 中使用的函数:

def get_user_by_username(db: Session, username: str):
return db.query(models.User).filter(models.User.username == username).first()

使用的 sqlalchemy 模型:

from sqlalchemy import Boolean, Column, ForeignKey, Integer, String
from sqlalchemy.orm import relationship

from .database import Base

class User(Base):
__tablename__ = "user"

id = Column(Integer, primary_key=True, index=True)
username = Column(String(100), unique=True, index=True)
mail = Column(String(100), unique=True, index=True)
hashed_password = Column(String(100))
is_active = Column(Boolean, default=True)

permissions = relationship("Permission", back_populates="user")

class Permission(Base):
__tablename__ = "permission"

id = Column(Integer, primary_key=True, index=True)
name = Column(String(100))
user_id = Column(Integer, ForeignKey("user.id"))

user = relationship("User", back_populates="permissions")

使用过的 pydantic 模型:

from typing import List, Optional
from pydantic import BaseModel

#Define Datatype Token
class Token(BaseModel):
access_token: str
token_type: str

#Define Datatype TokenData
class TokenData(BaseModel):
username: str

class Config:
orm_mode = True


#Define Datatype User
class User(BaseModel):
id: int
username: str
mail: str
is_active: Optional[bool]
permissions: List[Permission] = []

class Config:
orm_mode = True

总的来说,我对 fastapi 和 python 真的很陌生,非常感谢帮助!

最佳答案

您必须向 Pydantic 提供您为其提供值(value)的:

token_data = schemas.TokenData(username=username)

否则 Pydantic 不知道父作用域中的变量 username 应该分配给模式中的 username 属性。

关于Python Pydantic 错误 : TypeError: __init__() takes exactly 1 positional argument (2 given),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71546126/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com