Prevent crashes when using generic class typing syntax

Python 3.12 introduced generic class typing syntax (PEP 695), but trying to use this on a Django model causes the migrations framework to crash.

We have ticket-33174 reporting this (back when importing from typing.Generic was required.) The use case presented was annotating the return of a queryset method. I’ll cross-post what I added on the ticket.

Do folks agree it’s time we should look into a minimally invasive patch to prevent the crash?

re comment:1

  • We do have an updated story from Python, as of PEP 695
  • echoing comment:7 the hope is that a small patch is possible
  • the decision on annotations was re: django’s source, versus here we have a user’s valid python class
  • 2023 django developers ​survey shows 70% of users use or plan to use type annotations

I agree, we shouldn’t block the ability to add type hints.

This would be similar to ticket-31223, where we started adding __class_getitem__ to some types in order to allow users to add type hints.

1 Like