10.09.17 21:48, Ivan Levkivskyi пише:
   # lib.py

   from warnings import warn

   deprecated_names = ["old_function", ...]

   def _deprecated_old_function(arg, other):
       ...

   def __getattr__(name):
       if name in deprecated_names:
           warn(f"{name} is deprecated", DeprecationWarning)
           return globals()[f"_deprecated_{name}"]
       raise AttributeError(f"module {__name__} has no attribute {name}")

   # main.py

   from lib import old_function  # Works, but emits the warning

I think the PEP should provide better examples, because they will be copied in third-party code.

For keeping all functionality (in particularly be pickleable) the deprecated function should preserve its name.

   def old_function(arg, other):
       ...
   _deprecated_old_function = old_function
   del old_function

or

   def _deprecated_old_function(arg, other):
       ...
   _deprecated_old_function.__name__ = 'old_function'
   _deprecated_old_function.__qualname__ = 'old_function'

(I prefer the former variant.)

I'm wondering if it is worth to provide a special helper that will rename the deprecated function and create the corresponding __getattr__ function.

It could be possible to create helpers that implement module-level properties too.

_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to