[issue39443] Inhomogeneous behaviour for descriptors in between the class-instance and metaclass-class pairs

2020-01-24 Thread Hugo Ricateau


New submission from Hugo Ricateau :

Assume one has defined the following descriptor:
```
class Descriptor:
def __set__(self, instance, value):
print('SET')
```

On the one hand, for the class-instance pair, the behaviour is as follows:
```
class FirstClass:
descriptor = Descriptor()

def __init__(self):
self.descriptor = None

FirstClass().descriptor = None
```
results in "SET" being displayed twice; i.e. both assignations triggered the 
__set__ method of the descriptor.

On the other hand, for the metaclass-class pair, the behaviour is the following:
```
class SecondClassMeta(type):
descriptor = Descriptor()

class SecondClass(metaclass=SecondClassMeta):
descriptor = None

SecondClass.descriptor = None
```
results in "SET" being displayed only once: the first assignation (the one in 
the class definition) did not triggered __set__.

It looks to me like an undesirable asymmetry between the descriptors behaviour 
when in classes vs when in metaclasses. Is that intended? If it is, I think it 
should be highlighted in the descriptors documentation.

Best

--
components: Interpreter Core
messages: 360623
nosy: Hugo Ricateau
priority: normal
severity: normal
status: open
title: Inhomogeneous behaviour for descriptors in between the class-instance 
and metaclass-class pairs
type: behavior
versions: Python 3.6, Python 3.7, Python 3.8

___
Python tracker 
<https://bugs.python.org/issue39443>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue39443] Inhomogeneous behaviour for descriptors in between the class-instance and metaclass-class pairs

2020-01-25 Thread Hugo Ricateau


Hugo Ricateau  added the comment:

Thanks for this detailed answer; very instructive :)

> the descriptor protocol is only triggered by "dotted access"

Indeed; this is what I was missing... despite it is indirectly mentioned in the 
documentation. Nonetheless, it could be worth the overload to explicitly add in 
the language reference that 'the descriptor protocol is only triggered by 
"dotted access"' (looks like it is not the case for now).

>+ a list enumerating places where descriptors are *not* invoked
> [...]
> Which of those do you think would have helped you the most?

Could be really helpful as well, by clearly exhibiting the limitations of the 
descriptors; I think the best location for this could be the 'descriptors 
howto' page despite the other option is perfectly suitable as well.

Best,
Hugo

--

___
Python tracker 
<https://bugs.python.org/issue39443>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com