From e0a30611f13d07138408678c5ffd9e204c7c1ba8 Mon Sep 17 00:00:00 2001 From: SKairinos Date: Tue, 5 Nov 2024 13:55:15 +0000 Subject: [PATCH] fix: types --- codeforlife/request.py | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/codeforlife/request.py b/codeforlife/request.py index ce00056..41fa056 100644 --- a/codeforlife/request.py +++ b/codeforlife/request.py @@ -7,7 +7,7 @@ import typing as t -from django.contrib.auth.models import AnonymousUser +from django.contrib.auth.models import AbstractBaseUser, AnonymousUser from django.core.handlers.wsgi import WSGIRequest as _WSGIRequest from django.http import HttpRequest as _HttpRequest from rest_framework.request import Request as _Request @@ -22,24 +22,25 @@ else: AnyUser = t.TypeVar("AnyUser") +AnyAbstractBaseUser = t.TypeVar("AnyAbstractBaseUser", bound=AbstractBaseUser) + # pylint: disable-next=missing-class-docstring -class WSGIRequest(_WSGIRequest): +class WSGIRequest(_WSGIRequest, t.Generic[AnyUser]): session: "SessionStore" - user: t.Union["User", AnonymousUser] + user: t.Union[AnyUser, AnonymousUser] # pylint: disable-next=missing-class-docstring -class HttpRequest(_HttpRequest): +class HttpRequest(_HttpRequest, t.Generic[AnyUser]): session: "SessionStore" - user: t.Union["User", AnonymousUser] + user: t.Union[AnyUser, AnonymousUser] # pylint: disable-next=missing-class-docstring,abstract-method -class BaseRequest(_Request, t.Generic[AnyUser]): +class BaseRequest(_Request, t.Generic[AnyAbstractBaseUser]): data: t.Any - session: "SessionStore" - user: t.Union[AnyUser, AnonymousUser] + user: t.Union[AnyAbstractBaseUser, AnonymousUser] @property def query_params(self) -> t.Dict[str, str]: # type: ignore[override] @@ -53,7 +54,7 @@ def anon_user(self): @property def auth_user(self): """The authenticated user that made the request.""" - return t.cast(AnyUser, self.user) + return t.cast(AnyAbstractBaseUser, self.user) @property def json_dict(self): @@ -68,6 +69,8 @@ def json_list(self): # pylint: disable-next=missing-class-docstring,abstract-method class Request(BaseRequest[AnyUser], t.Generic[AnyUser]): + session: "SessionStore" + def __init__(self, user_class: t.Type[AnyUser], *args, **kwargs): super().__init__(*args, **kwargs) self.user_class = user_class