<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On 26 December 2016 at 21:23, Zahari Dim <span dir="ltr"><<a href="mailto:zaharid@gmail.com" target="_blank">zaharid@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><span class="gmail-">> There are a lot of entirely valid properties that look something like this:<br>
><br>
><br>
> @property<br>
> def attr(self):<br>
> try:<br>
> return data_store[lookup_key]<br>
> except KeyError:<br>
> raise AttributeError("attr")<br>
<br>
</span>But wouldn't something like this be implemented more commonly with<br>
__getattr__ instead (likely there is more than one such property in a<br>
real example)? Even though __getattr__ has a similar problem (a bad<br>
AttributeError inside can cause many bugs), I'd agree it would<br>
probably be too difficult to change that without breaking a lot of<br>
code. For __get__, the errors are arguably more confusing (e.g. when<br>
used with @property) and the legitimate use case, while existing,<br>
seems more infrequent to me: I did a github search and there was a<br>
small number of cases, but most were for code written in python 2<br>
anyway.</blockquote><br></div><div class="gmail_quote">Aye, I agree this pattern is far more common in __getattr__ than it is in descriptor __get__ implementations or in property getter implementations.<br><br></div><div class="gmail_quote">Rather than changing the descriptor protocol in general, I'd personally be more amenable to the idea of *property* catching AttributeError from the functions it calls and turning it into RuntimeError (after a suitable deprecation period). That way folks that really wanted the old behaviour could define their own descriptor that works the same way property does today, whereas if the descriptor protocol itself were to change, there's very little people could do to work around it if it wasn't what they wanted.<br><br></div><div class="gmail_quote">Exploring that possible approach a bit further:<br><br></div><div class="gmail_quote"><div class="gmail_quote">- after a deprecation period, the "property" builtin would change to convert any AttributeError raised by the methods it calls into RuntimeError<br></div>- the current "property" could be renamed "optionalproperty": the methods may raise AttributeError to indicate the attribute isn't *really* present, even though the property is defined at the class level<br></div>- the deprecation warning would indicate that the affected properties should switch to using optionalproperty instead<br></div><div class="gmail_extra"><div class="gmail_quote"><br></div><div class="gmail_quote">Cheers,<br></div><div class="gmail_quote">Nick.<br></div><br>-- <br><div class="gmail_signature">Nick Coghlan | <a href="mailto:ncoghlan@gmail.com" target="_blank">ncoghlan@gmail.com</a> | Brisbane, Australia</div>
</div></div>