Skip to content

Commit 1b535d1

Browse files
committed
Add GenericModel to types
Provides a workaround for a bug in Django that prevents models from inheriting from Generic.
1 parent 77e8435 commit 1b535d1

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

xocto/types.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Utility types to save having to redefine the same things over and over.
33
"""
44

5-
from typing import Generic, NoReturn, Tuple, TypeVar, Union
5+
from typing import TYPE_CHECKING, Generic, NoReturn, Tuple, TypeVar, Union
66

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

6565

66+
# Django does not like models which inherit from Generic.
67+
# This is the recommended workaround (https://code.djangoproject.com/ticket/33174).
68+
if TYPE_CHECKING:
69+
70+
U = TypeVar("U")
71+
72+
class GenericModel(models.Model, Generic[U]):
73+
pass
74+
75+
else:
76+
77+
class GenericModel(models.Model):
78+
def __class_getitem__(cls, _): # noqa: K106
79+
return cls
80+
81+
6682
def assert_never(value: NoReturn) -> NoReturn:
6783
"""
6884
Helper to ensure checks are exhaustive.

0 commit comments

Comments
 (0)