Skip to content

Commit

Permalink
fix: types
Browse files Browse the repository at this point in the history
  • Loading branch information
SKairinos committed Nov 5, 2024
1 parent 1dd44d0 commit e0a3061
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions codeforlife/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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]
Expand All @@ -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):
Expand All @@ -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
Expand Down

0 comments on commit e0a3061

Please sign in to comment.