Python Pydantic Error: TypeError: __init__() takes exactly 1 positional argument (2 given)
Python Pydantic Error: TypeError: __init__() takes exactly 1 positional argument (2 given)
我目前正在为大学做一个 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)
这是我的相关代码:
快速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 提供您要为其提供价值的键:
token_data = schemas.TokenData(username=username)
否则 Pydantic 不知道父作用域中的变量 username
应该分配给架构中的 username
属性。
我目前正在为大学做一个 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)
这是我的相关代码:
快速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 提供您要为其提供价值的键:
token_data = schemas.TokenData(username=username)
否则 Pydantic 不知道父作用域中的变量 username
应该分配给架构中的 username
属性。