<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Tue, Nov 14, 2017 at 12:24 AM, Serhiy Storchaka <span dir="ltr"><<a href="mailto:storchaka@gmail.com" target="_blank">storchaka@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">10.09.17 21:48, Ivan Levkivskyi пише:<span class=""><br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
# lib.py<br>
<br>
from warnings import warn<br>
<br>
deprecated_names = ["old_function", ...]<br>
<br>
def _deprecated_old_function(arg, other):<br>
...<br>
<br>
def __getattr__(name):<br>
if name in deprecated_names:<br>
warn(f"{name} is deprecated", DeprecationWarning)<br>
return globals()[f"_deprecated_{name}<wbr>"]<br>
raise AttributeError(f"module {__name__} has no attribute {name}")<br>
<br>
# main.py<br>
<br>
from lib import old_function # Works, but emits the warning<br>
</blockquote>
<br></span>
I think the PEP should provide better examples, because they will be copied in third-party code.<br>
<br>
For keeping all functionality (in particularly be pickleable) the deprecated function should preserve its name.<br>
<br>
def old_function(arg, other):<br>
...<br>
_deprecated_old_function = old_function<br>
del old_function<br>
<br>
or<br>
<br>
def _deprecated_old_function(arg, other):<br>
...<br>
_deprecated_old_function.__na<wbr>me__ = 'old_function'<br>
_deprecated_old_function.__qu<wbr>alname__ = 'old_function'<br>
<br>
(I prefer the former variant.)<br></blockquote><div><br></div><div>However the latter could be done by a decorator.<br></div></div><div class="gmail_quote"><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
I'm wondering if it is worth to provide a special helper that will rename the deprecated function and create the corresponding __getattr__ function.<br>
<br>
It could be possible to create helpers that implement module-level properties too.<br clear="all"></blockquote><div><br></div><div>OTOH not everyone cares about whether their functions are picklable. Maybe a discussion of how to make deprecated functions picklable could be in an Appendix? And another example of how to implement more functional lazy loading. Or even more complete examples could be maintained as a separate GitHub repo (like Ethan did for PEP 561).<br></div></div><br>-- <br><div class="gmail_signature" data-smartmail="gmail_signature">--Guido van Rossum (<a href="http://python.org/~guido" target="_blank">python.org/~guido</a>)</div>
</div></div>