#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
---------------------------------+----------------------------------------
Comment (by Fabian Braun):

 Python (since 3.9) resolves
 [https://docs.python.org/3/reference/datamodel.html#object.__class_getitem__
 MyClass["something"]] this way (taken from the docs linked - adapted for a
 class):
 {{{
 def subscribe(cls, x):
     """Return the result of the expression 'cls[x] if cls is a class'"""

     metaclass = type(cls)

     # If the metaclass of cls defines __getitem__, call
 metaclass.__getitem__(cls, x)
     if hasattr(metaclass, '__getitem__'):  # This is also true for python
 pre-3.9
         return metaclass.__getitem__(cls, x)

     # New in Python 3.9:
     # Else, if obj is a class and defines __class_getitem__, call
 obj.__class_getitem__(x)
     elif hasattr(cls, '__class_getitem__'):
         # Instead of TypeError the __class_getitem__ class method returns
 a GenericAlias object
         return cls.__class_getitem__(x)

     # Else, raise an exception - this will let Django's template system
 try a property next
     else:
         raise TypeError(
             f"'{cls.__name__}' object is not subscriptable"
         )
 }}}
-- 
Ticket URL: <https://code.djangoproject.com/ticket/35735#comment:10>
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/01070191c8f85827-99709967-13b0-4ced-b76f-72353d71eb99-000000%40eu-central-1.amazonses.com.

Reply via email to