tantale / deprecated

Python @deprecated decorator to deprecate old python classes, functions or methods.
MIT License
298 stars 32 forks source link

deprecated_args support #51

Closed mjhajharia closed 2 years ago

mjhajharia commented 2 years ago

This pull request adds support for arguments to be deprecated. I made this specifically because we're planning to use Deprecated in PyMC and we needed support for deprecating parameter for which names have been changed or if they're redundant. I have a draft of some messy regex to add deprecated warning just below a parameter's directive as well, i'll make a PR for that soon. if this is out of scope for this project let me know then we'll rename the fork and use that.

mjhajharia commented 2 years ago

here's an example

image
mjhajharia commented 2 years ago
 def regex_for_deprecated_args(docstring, deprecated_args):
         """
         This function uses regex for positioning deprecation warnings for parameters with their documentation.

         "\\n{1}\\w+:{1}"  - looks for the next parameter(formatted as [line break followed by some string ending with a colon ]) 
         that is defined in the documentation, so we introduce the warning right before that

         "\\n{1}\\w+\\n{1}-+\\n{1}" - looks for the next documentation section like "Parameters", "Examples", "Returns"
         these are followed by a line of dashes (------).

         we look through all of these possible endings to find the "endpoint" of the param definition and insert the deprecation warning there

         """
         for deprecated_arg in deprecated_args:
             doc=docstring.split(f'\n{deprecated_arg}:')[1]
             nextitem = re.search("\\n{1}\\w+:{1}", doc)
             nextsection = re.search("\\n{1}\\w+\\n{1}-+\\n{1}",doc)
             last = len(doc)
             n = min(nextitem.start(), nextsection.start(), last)
             y = len(docstring.split(f'\n{deprecated_arg}:')[0]) + len(f'\n{deprecated_arg}:')
             docstring = docstring[:y+n] + str(sphinxtext) + docstring[y+n:]
         return docstring

here's the [work in progress] code for auto-adding deprecated directive for a kwarg just below its definition. feel free to incorporate this