Skip to content

Commit

Permalink
Add GenericModel to types
Browse files Browse the repository at this point in the history
Provides a workaround for a bug in Django that prevents models from
inheriting from Generic.
  • Loading branch information
RJPercival committed May 6, 2022
1 parent 77e8435 commit 149e236
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 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,23 @@ 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

0 comments on commit 149e236

Please sign in to comment.