Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GenericModel to types #11

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion xocto/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Utility types to save having to redefine the same things over and over.
"""

from typing import Generic, NoReturn, Tuple, TypeVar, Union
from typing import TYPE_CHECKING, Generic, NoReturn, Tuple, TypeVar, Union

from django.contrib.auth import models as auth_models
from django.db import models
Expand Down Expand Up @@ -63,6 +63,22 @@ class AuthenticatedRequest(HttpRequest, Generic[User]):
user: User


# Django does not like models which inherit from Generic.
# This is the recommended workaround (https://code.djangoproject.com/ticket/33174).
if TYPE_CHECKING:

U = TypeVar("U")

class GenericModel(models.Model, Generic[U]):
pass

else:

class GenericModel(models.Model):
def __class_getitem__(cls, _): # noqa: K106
return cls


def assert_never(value: NoReturn) -> NoReturn:
"""
Helper to ensure checks are exhaustive.
Expand Down