[Django] #30019: Add __class_getitem__ method to managers and querysets

10 views
Skip to first unread message

Django

unread,
Dec 7, 2018, 1:21:55 PM12/7/18
to django-...@googlegroups.com
#30019: Add __class_getitem__ method to managers and querysets
-------------------------------------+-------------------------------------
Reporter: mkurnikov | Owner: nobody
Type: New | Status: new
feature |
Component: Database | Version: master
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 1
UI/UX: 0 |
-------------------------------------+-------------------------------------
I'm working on a pep561 stubs package for Django here
https://github.com/mkurnikov/django-stubs

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.

Django

unread,
Dec 7, 2018, 1:49:07 PM12/7/18
to django-...@googlegroups.com
#30019: Add __class_getitem__ method to managers and querysets
-------------------------------------+-------------------------------------
Reporter: mkurnikov | Owner: Greg
Type: New feature | Status: assigned
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Greg):

* status: new => assigned
* owner: nobody => Greg


--
Ticket URL: <https://code.djangoproject.com/ticket/30019#comment:1>

Django

unread,
Dec 8, 2018, 12:43:25 AM12/8/18
to django-...@googlegroups.com
#30019: Add __class_getitem__ method to managers and querysets
-------------------------------------+-------------------------------------
Reporter: Maxim Kurnikov | Owner: Greg

Type: New feature | Status: assigned
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------

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>

Django

unread,
Dec 8, 2018, 5:29:13 AM12/8/18
to django-...@googlegroups.com
#30019: Add __class_getitem__ method to managers and querysets
-------------------------------------+-------------------------------------
Reporter: Maxim Kurnikov | Owner: Greg
Type: New feature | Status: assigned
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------

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>

Django

unread,
Dec 10, 2018, 6:11:05 AM12/10/18
to django-...@googlegroups.com
#30019: Add __class_getitem__ method to managers and querysets
-------------------------------------+-------------------------------------
Reporter: Maxim Kurnikov | Owner: Greg
Type: New feature | Status: assigned
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Herbert Fortes):

* cc: Herbert Fortes (added)


--
Ticket URL: <https://code.djangoproject.com/ticket/30019#comment:4>

Django

unread,
Dec 11, 2018, 10:55:59 PM12/11/18
to django-...@googlegroups.com
#30019: Add __class_getitem__ method to managers and querysets
-------------------------------------+-------------------------------------
Reporter: Maxim Kurnikov | Owner: Greg W

Type: New feature | Status: assigned
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------

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>

Django

unread,
Dec 12, 2018, 10:58:15 AM12/12/18
to django-...@googlegroups.com
#30019: Add __class_getitem__ method to managers and querysets
-------------------------------------+-------------------------------------
Reporter: Maxim Kurnikov | Owner: Greg W
Type: New feature | Status: closed

Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution: needsinfo
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Carlton Gibson):

* 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>

Django

unread,
Dec 20, 2018, 8:47:11 PM12/20/18
to django-...@googlegroups.com
#30019: Add __class_getitem__ method to managers and querysets
-------------------------------------+-------------------------------------
Reporter: Maxim Kurnikov | Owner: Greg W
Type: New feature | Status: closed
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution: needsinfo
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------

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>

Reply all
Reply to author
Forward
0 new messages