Nick Coghlan added the comment:
Carl's patch looks good to me, but my one request in relation to the __slots__
situation would be to give it a custom error message better indicating that
lazy initialization of pre-assigned instance slots isn't supported.
Currently that case just lets the underlying AttributeError escape, which is
going to be thoroughly cryptic for folks that try it and may look like an
accidental oversight rather than a deliberate design decision:
>>> class NoDict:
... __slots__ = ()
...
>>> NoDict().__dict__
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'NoDict' object has no attribute '__dict__'
Suggested error message:
TypeError: No '__dict__' attribute on 'objtype' instance to cache
'attrname' property.
Essentially, this line:
val = instance.__dict__[self.func.__name__] = self.func(instance)
would become:
attrname = self.func.__name__
try:
cache = instance.__dict__
except AttributeError:
msg = f"No '__dict__' attribute on {type(instance).__name__!r} instance
to cache {attrname!r} property."
raise TypeError(msg) from None
val = cache[attrname] = self.func(instance)
I believe a future C implementation could potentially be reworked to be
__slots__ compatible, but I'd have to go read the source code to be sure, and I
don't think that's necessary.
Note: the class machinery itself already detects actual name conflicts between
slot and method definitions:
>>> class SlotConflict:
... __slots__ = ("attr")
... @property
... def attr(self):
... return 42
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: 'attr' in __slots__ conflicts with class variable
The requested runtime check for the `__dict__` AttributeError covers the case
where __slots__ is defined, and the cached property *isn't* listed as one of
the instance attributes, but neither is __dict__.
----------
_______________________________________
Python tracker <[email protected]>
<http://bugs.python.org/issue21145>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com