#33174: Having a model inherit from Generic[T] breaks makemigrations
--------------------------------+--------------------------------------
Reporter: Antoine Humeau | Owner: nobody
Type: New feature | Status: closed
Component: Migrations | Version: 3.2
Severity: Normal | Resolution: needsinfo
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------+--------------------------------------
Comment (by Adam Johnson):
`typing` itself provides a workaround here - `Generic` is only needed for
type checking, so use `TYPE_CHECKING` as in the third pattern documented
here: https://mypy.readthedocs.io/en/stable/runtime_troubles.html#using-
classes-that-are-generic-in-stubs-but-not-at-runtime
For example:
{{{
import typing
from django.db import models
import stripe
from stripe.stripe_object import StripeObject
StripeClassT = typing.TypeVar('StripeClassT', bound=StripeObject)
if typing.TYPE_CHECKING:
class GenericBase(typing.Generic[StripeClassT]):
pass
else:
class GenericBase:
pass
class StripeObjectModel(GenericBase, models.Model):
...
}}}
I don't think there's anything Django should do here at the moment. Typing
is, as Carlton says, very much in flux. Plus Mypy + typing provide clear,
if verbose, workarounds for most situations.
--
Ticket URL: <https://code.djangoproject.com/ticket/33174#comment:3>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/065.c3472e54ec4135f1e62893a83daeaea3%40djangoproject.com.