#35735: For python 3.9+ class property may not be accessible by Django's
template
system
---------------------------------+----------------------------------------
Reporter: Fabian Braun | Owner: Fabian Braun
Type: Bug | Status: new
Component: Template system | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+----------------------------------------
Changes (by Fabian Braun):
* resolution: needsinfo =>
* status: closed => new
Comment:
Hi Natalia!
Thanks for taking time to look into this.
Sorry, indeed the example needs the `list` parent class. Sorry, I missed
that on the ticket. [https://github.com/django/django/pull/18547/files The
fix] has three tests that work with python 3.8 and not with python 3.9+. I
wanted the test code to be as short as possible and that took me more
iterations than anticipated.
While these tests subclass `list`, **any class that implements some sort
of type hinting using `__class_get_item__` will have its properties or
methods shadowed**. `list` is just a built-in example. Having said this,
you will have to assume that projects subclass Django classes, shadowing
for example a model's manager:
{{{
{% for obj in MyModel.objects.all %}
<li>{{ obj }}</li>
{% endfor %}
}}}
will not work, if the custom `MyModel` for some reason implements
`__class_get_item__` - a thing one might do to annotate, for example, what
model a generic foreign key might refer to, or what sort of data is stored
in a JSON field, or ....
At the time the template system was designed, classes were never
subscriptable. Python has changed, and that's not Django's fault. But I
believe it is time that Django changes with python. The fix I propose
avoids the issue because it does not run `MyClass["property_name"]` in the
first place. This restores the original template variable resolution
design and order of how template references were resolved before python
3.9.
With type hinting getting more and more popular, I expect this issue to
become more and more important. That's what I mean with "class properties
may not be accessible". Sorry, if it felt like I was overstating the
issue.
I guess, already now, the issue might be important: The latest version of
django-modeltranslation [https://github.com/deschler/django-
modeltranslation/blob/0cafcee3f273ac9d01c3321bae2fcc96cc0a83c4/modeltranslation/_typing.py#L26-L40
adds `__class_getitem__` to all admin classes]. This has unforeseen side
effects on all admin classes of all projects using django-
modeltranslation. (And to prevent the discussion if django-
modeltranslation should fix this: (a) they've worked their way around it
and (b) it is not a specific issue for django-modeltranslation but for
**all classes that make their way into Django's template system**.)
I hope I could clarify your questions. Please keep asking if there is need
for more information. Since I am still convinced this should be fixed,
hence I reopen the ticket.
--
Ticket URL: <https://code.djangoproject.com/ticket/35735#comment:8>
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/01070191c3d2772f-d1cf1712-8344-4404-9bb4-7ebc2602632b-000000%40eu-central-1.amazonses.com.