You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi, I implemented fastapi-users based on provided official examples, but unfortunately I'm getting type checking error (using pyright):
Method "validate_password" overrides class "BaseUserManager" in an incompatible manner
Parameter 3 type mismatch: base parameter is type "UC@validate_password | User", override parameter is type "UserCreate | User"
Type "UC@validate_password | User" is not assignable to type "UserCreate | User"
Type "UC@validate_password" is not assignable to type "UserCreate | User"
"BaseUserCreate*" is not assignable to "UserCreate"
"BaseUserCreate*" is not assignable to "User"
from __future__ import annotations
import contextlib
import logging
from collections.abc import AsyncGenerator
from typing import Any
from fastapi import Depends
from fastapi import Request
from fastapi_users import BaseUserManager
from fastapi_users import FastAPIUsers
from fastapi_users import IntegerIDMixin
from fastapi_users import InvalidPasswordException
from fastapi_users.authentication import AuthenticationBackend
from fastapi_users.authentication import BearerTransport
from fastapi_users.authentication import JWTStrategy
from fastapi_users_db_sqlalchemy import SQLAlchemyUserDatabase
from sqlalchemy.ext.asyncio import AsyncSession
import app.settings as app_settings
from app.db.postgres import PGSQLSessionManager
from app.models.app import User
from app.schemas.user import UserCreate
class UserManager(IntegerIDMixin, BaseUserManager[User, int]):
reset_password_token_secret = app_settings.get_settings().app.auth.jwt_secret
async def validate_password(
self,
password: str,
user: UserCreate | User,
) -> None:
if len(password) < 8:
raise InvalidPasswordException(reason="Password should be at least 8 characters")
if user.email in password:
raise InvalidPasswordException(reason="Password should not contain e-mail")
async def on_after_update(
self,
user: User,
update_dict: dict[str, Any],
request: Request | None = None,
):
if request is None:
return
authenticated_user: User | None = await get_current_auth_user(auth_header=request.headers.get("Authorization"))
if not authenticated_user or not authenticated_user.is_superuser:
return
# This is hacky, but we need to get the current session from the user_db
# otherwise we will work with different objects per session
current_session: AsyncSession | None = getattr(self.user_db, "session", None)
if current_session is None:
return
current_superuser: User | None = await current_session.get(User, authenticated_user.id)
# Do not update self
if current_superuser == user:
return
if current_superuser:
user.last_updated_by = current_superuser
await current_session.commit()
update_dict.pop("password")
logger.info(
"User %s has been updated with %s by %s.",
user.email,
update_dict,
current_superuser.email,
)
async def on_after_register(self, user: User, request: Request | None = None):
logger.info(f"User {user.id} has registered.")
async def on_after_forgot_password(self, user: User, token: str, request: Request | None = None):
logger.info(f"User {user.id} has forgot their password. Reset token: {token}")
# Rest of the file...
I assume that UserCreate should be the model from schemas? As the official example doesn't specify imports.
Can this type error be considered as a bug? or am I doing something wrong? - I could simply add # type: ignore but I don't like that 😊
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi, I implemented
fastapi-users
based on provided official examples, but unfortunately I'm getting type checking error (usingpyright
):My
user
schemas based on https://fastapi-users.github.io/fastapi-users/latest/configuration/full-example/#__tabbed_1_5 + some additional fields:And
UserManager
based on documentation and https://fastapi-users.github.io/fastapi-users/latest/configuration/user-manager/#validate_password:I assume that
UserCreate
should be the model from schemas? As the official example doesn't specify imports.Can this type error be considered as a bug? or am I doing something wrong? - I could simply add
# type: ignore
but I don't like that 😊Beta Was this translation helpful? Give feedback.
All reactions