Title: setattr a read-only property; the AttributeError should show the attribute that failed
Type: enhancement Stage: needs patch
Components: Interpreter Core Versions: Python 3.7
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Muhammad Alkarouri, apatrushev, ebarry, ncoghlan, r.david.murray, xiang.zhang, xtreak, ztane
Priority: normal Keywords:

Created on 2016-08-18 13:57 by ztane, last changed 2018-07-26 20:49 by apatrushev.

Messages (9)
msg273027 - (view) Author: Antti Haapala (ztane) * Date: 2016-08-18 13:57
Today we had an internal server error in production. I went to see the sentry logs for the error, and was dismayed: the error was `AttributeError: can't set attribute`, and the faulting line was `setattr(obj, attr, value)` that happens in a for-loop that uses computed properties coming from who knows.

Well, I quickly ruled out that it was because the code was trying to set a value to a read-only property descriptor, the only problem was to find out *which of all these read-only properties* was it trying to set:

Python 3.6.0a3+ (default, Aug 11 2016, 11:45:31) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> class Foo:
...     @property
...     def bar(self): pass
>>> setattr(Foo(), 'bar', 42)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: can't set attribute

Could we change this for Python 3.6 so that the message for this could include the name of the property just like `AttributeError: has no attribute 'baz'` does?
msg273033 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2016-08-18 14:39
If someone submits a patch in time, we'd probably accept it.
msg273035 - (view) Author: Emanuel Barry (ebarry) * (Python triager) Date: 2016-08-18 14:51
The approach I'd take would be to change how {get,set,del}attr handle AttributeError; possibly by automatically filling in the information and giving a nicer error message. This would fix this as a side-effect (somewhat; some bits of code would need to change). How does that sound to core devs?
msg273036 - (view) Author: Antti Haapala (ztane) * Date: 2016-08-18 14:57
Unfortunately it seems that it is not that straightforward. The descriptor object doesn't know the name of the property. The error is raised in `property_descr_set`. However the error itself can be propagated from setting another property.
msg273302 - (view) Author: Emanuel Barry (ebarry) * (Python triager) Date: 2016-08-21 16:28
Opened #27823 as a followup to this, and carried nosy list over.
msg273385 - (view) Author: Xiang Zhang (xiang.zhang) * (Python committer) Date: 2016-08-22 17:40
One solution I can think of is alter the constructor of property() and add an optional name attribute to it. If users really care about the exception message, they can set the attribute to the property's name. If they don't care, everything remains the same as now. This is just like the doc parameter of property() right now and the first parameter of type() when it is used to create a class dynamically. This is just a suggestion, I am not sure it's worth to do it.
msg273387 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2016-08-22 17:46
Not worth it.  It would feel like boilerplate, and the situation where you want the information is almost invariably going to be one you didn't anticipate and so didn't "bother" with the name.  Either that or you always have to do it, and the elegance of your python code takes a hit not commensurate with the benefit.
msg273396 - (view) Author: Antti Haapala (ztane) * Date: 2016-08-22 18:45
I've got one idea about how to implement this, but it would require adding a new flag field to PyExc_AttributeError type.

This flag, if set, would tell that the AttributeError in question was raised in C descriptor code or under similar circumstances, and that the attribute name was not known, and thus it is OK for setattr/delattr and attribute lookups to append ": attributename" to the end of the message, then clear the flag; then  all those places that raise AttributeError in __get__, __set__, __del__ would just need to set this flag.
msg301045 - (view) Author: Muhammad Alkarouri (Muhammad Alkarouri) Date: 2017-08-31 10:45
Now that the descriptor protocol has `__set_name__`, does this make any difference to this issue? The property can know its name, subject to a suitable patch.
Date User Action Args
2018-07-26 20:49:21apatrushevsetnosy: + apatrushev
2018-07-25 10:02:25xtreaksetnosy: + xtreak
2017-11-09 14:29:32serhiy.storchakasetnosy: + ncoghlan
stage: needs patch

components: + Interpreter Core
versions: + Python 3.7
2017-11-09 14:20:41r.david.murraylinkissue31989 superseder
2017-08-31 10:45:19Muhammad Alkarourisetnosy: + Muhammad Alkarouri
messages: + msg301045
2016-08-22 18:45:52ztanesetmessages: + msg273396
2016-08-22 17:46:42r.david.murraysetmessages: + msg273387
2016-08-22 17:40:40xiang.zhangsetmessages: + msg273385
2016-08-21 16:28:34ebarrysetmessages: + msg273302
2016-08-19 02:40:21xiang.zhangsetnosy: + xiang.zhang
2016-08-18 14:57:40ztanesetmessages: + msg273036
2016-08-18 14:51:08ebarrysetnosy: + ebarry
messages: + msg273035
2016-08-18 14:39:23r.david.murraysetnosy: + r.david.murray
messages: + msg273033
2016-08-18 13:57:19ztanecreate