On Thu, Feb 10, 2022 at 02:27:42PM -0800, Neil Girdhar wrote:

> AttributeError: can't set attribute 'f'
> 
> This can be a pain to debug when the property is buried in a base class.

> Would it make sense to mention the reason why the attribute can't be set, 
> namely that it's on a property without a setter?

I have no objection to changing the error message, I'm sure it's a small 
enough change that you should just open a ticket on b.p.o. for it. But I 
don't expect that it will be particularly useful either.

If you can't set an attribute on an object, aren't there three obvious 
causes to check?

- the object has no __dict__, and so has no attributes at all;
  e.g. trying to set an attribute on a float;

- the object has slots, but 'f' is not one of them;

- or 'f' is a property with no setter (or a setter that raises 
  AttributeError).

Have I missed any common cases?

The error messages are different in each case, but even if they were the 
same, there are three obvious causes to check, and property is one of 
them.

I suppose that there are exotic failures that could happen in 
__getattribute__ or weird descriptors, or metaclass magic, etc, and 
*those* might be hard to debug, but it doesn't seem likely to me that a 
read-only property will be mysterious.

Especially if you have the object available in the interactive 
interpreter, so you can inspect it with the various tools available:

* dir(obj)
* help(obj)
* type(obj).f  # Will display <property object at 0x...>

etc. Its hard to keep the existence of a property secret in Python.

So once you know that f is a property, it might be hard to work out 
which of the fifty-seven superclasses and mixins it came from *wink* but 
that's neither here nor there :-)

Maybe reporting "can't set property 'f'" is good enough.


-- 
Steve
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/Z2AWWETWB72CKARR4DAHB3A2LFRLQR7X/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to