Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

i am currenty working on a python fastapi project for university. Every time i run my authorization dependencies i get the following error:

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)

Here is my relevant code:

FAST API Endpoint:

@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

Used functions in my 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

Used functions in my crud.py:

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

Used sqlalchemy models:

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")

Used pydantic models:

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
      

I am really new to fastapi and python in general and would really appreciate help!

like image 572
Zito Avatar asked Feb 04 '26 19:02

Zito


2 Answers

You have to give Pydantic which key you are providing a value for:

token_data = schemas.TokenData(username=username)

Otherwise Pydantic has no idea that the variable username from the parent scope should be assigned to the username property in the schema.

like image 177
MatsLindh Avatar answered Feb 07 '26 08:02

MatsLindh


In my case I used response_class instead of response_model decorator. It was silly mistake by vscode code suggestion. So just check it in the decorator and change it to response_model.

Hope this helps

like image 34
Yalchin Mammadli Avatar answered Feb 07 '26 08:02

Yalchin Mammadli



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!