Python Pydantic 错误:类型错误:__init__() 恰好需要 1 个位置参数(给定 2 个)

问题描述 投票:0回答:2

我目前正在为大学开发一个 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 非常陌生,非常感谢您的帮助!

python fastapi pydantic
2个回答
14
投票

您必须向 Pydantic 提供您正在为其提供值的key

token_data = schemas.TokenData(username=username)

否则 Pydantic 不知道父作用域中的变量

username
应该分配给模式中的
username
属性。


0
投票

在我的例子中,我使用

response_class
而不是
response_model
装饰器。这是 vscode 代码建议的愚蠢错误。所以只需在装饰器中检查它并将其更改为
response_model

希望这有帮助

© www.soinside.com 2019 - 2024. All rights reserved.