[issue27794] setattr a read-only property; the AttributeError should show the attribute that failed

Raymond Hettinger report at bugs.python.org
Sun Dec 27 17:52:55 EST 2020


Raymond Hettinger <raymond.hettinger at gmail.com> added the comment:

> In my opinion, it's expected behavior that `name` is 
> overwritten by `__set_name__` method.

It is almost certain that there will be others won't share that expectation.  The StackOverflow questions and bug reports are inevitable.

Most examples of __set_name__() use a private attribute.  I recommend that you go that route and stick to the spirit of the original bug report.  The OP asked for better error messages when possible.  They didn't ask for an API expansion.

> I have added `name` to `property` constructor to support cases
> when a property is added to a class after it was declared.

That rarely occurs in practice.  I wouldn't worry about it.  Also AFAICT the only time the new error message matters is in the context of a setattr() where the attribute isn't already shown in the traceback.  So the case in question is really a rarity inside another rarity.  Let's declare YAGNI unless an actual end-user problem arises in real-world code.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue27794>
_______________________________________


More information about the Python-bugs-list mailing list