In order to be able to fully support managers as a generics, one needs to
write
{{{
from django.db import models
class UserManager(models.Manager[User]):
def get_or_404(self) -> User:
pass
class User(models.Model):
objects = UserManager()
}}}
but it fails at {{{ models.Manager[User] }}}.
There's a new method {{{ __class_getitem__ }}} in python3.7 to get around
that, defined in pep560
https://www.python.org/dev/peps/pep-0560/
--
Ticket URL: <https://code.djangoproject.com/ticket/30019>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* status: new => assigned
* owner: nobody => Greg
--
Ticket URL: <https://code.djangoproject.com/ticket/30019#comment:1>
Comment (by Greg):
Is this the error you are getting:
NameError: name 'User' is not defined
If not, can you please let me know what the error is?
--
Ticket URL: <https://code.djangoproject.com/ticket/30019#comment:2>
Comment (by Maxim Kurnikov):
{{{TypeError: 'type' object is not subscriptable}}}
Look at
https://www.python.org/dev/peps/pep-0560/#class-getitem
and how it was implemented before with {{{GenericMeta}}}
https://www.python.org/dev/peps/pep-0484/#user-defined-generic-types
Minimal repro should be this one
{{{
from django.db import models
class UserManager(models.Manager['User']):
pass
class User(models.Model):
objects = UserManager()
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/30019#comment:3>
* cc: Herbert Fortes (added)
--
Ticket URL: <https://code.djangoproject.com/ticket/30019#comment:4>
Comment (by Greg W):
I got {{{ __class_getitem__}}} implemented. However, I am having an
issue with {{{models.Manager['User']}}}
Isn't {{{'User'}}} just a string which {{{__class_getitem__}}} would not
be valid on? I think the idea is to index the class User, but this class
has not been defined prior to {{{Manager[User]}}}.
Let me know if I am off base.
--
Ticket URL: <https://code.djangoproject.com/ticket/30019#comment:5>
* status: assigned => closed
* resolution: => needsinfo
Comment:
Hi Maxim.
I'm going to close this as needsinfo right now. Not because I'm against it
but because it's part of the wider discussion around Type Hinting in
Django.
I see you posted to the Django-Developers thread about this:
https://groups.google.com/d/topic/django-developers/trTEbURFhEY/discussion
Can I ask you to add your effort there? I think there's a group of people
who are there or there-abouts with this: as ever, it just needs a bit of a
nudge.
(There was talk of a DEP, of a working groups, of... — we just need to get
there.)
Thanks.
I'll post there to try and re-liven it.
--
Ticket URL: <https://code.djangoproject.com/ticket/30019#comment:6>
Comment (by Tim Graham):
(And #29299 is the Someday/Maybe ticket for adding type hints and variable
annotations.)
--
Ticket URL: <https://code.djangoproject.com/ticket/30019#comment:7>