add decorator for deprecating function arguments (closes #18)#25
add decorator for deprecating function arguments (closes #18)#25
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #25 +/- ##
==========================================
+ Coverage 97.47% 97.56% +0.09%
==========================================
Files 5 5
Lines 238 288 +50
==========================================
+ Hits 232 281 +49
- Misses 6 7 +1
🚀 New features to boost your workflow:
|
flying-sheep
left a comment
There was a problem hiding this comment.
Good functionality, but I’m concerned about the docstring modifications being too fragile
| warn(warnmsg, category=category, stacklevel=stacklevel) | ||
| else: | ||
| bound = sig.bind(*args, **kwargs) | ||
| if arg in bound.arguments and bound.arguments[arg] != param.default: | ||
| warn(warnmsg, category=category, stacklevel=stacklevel) |
There was a problem hiding this comment.
The default stacklevel=1 should function like in warnings.deprecated
If it is 1 (the default), the warning is emitted at the direct caller of the deprecated object;
I’m 90% sure you need to do warn(..., stacklevel=stacklevel+1) to achieve that here, but please verify.
| def decorate(func: Callable[P, R]) -> Callable[P, R]: | ||
| warnmsg = f"The argument {arg} is deprecated and will be removed in the future." | ||
| doc = inspect.getdoc(func) | ||
| docmsg = f" .. version-deprecated:: {msg.version_deprecated}" |
There was a problem hiding this comment.
The indentation handling looks fragile since you seem to expect exactly 4 spaces here
- Google docstrings are supposed to be indented by 2 or 4 spaces per level, but I assume that
sphinx.ext.napoleonmight be less strict. - reStructuredText and therefore numpy docstring indentation isn’t constrained even in style guides
So really, any amount of indentation should be handled here without breaking. I don’t even know how that would work if someone uses an inline google-style parameter description:
Args:
foo: the descriptionwould then have to become this?
Args:
foo:
the description
.. version-deprecated:: ...or is mixed inline and indented OK?
| if in_arg_header: | ||
| in_arg_header = False | ||
| continue | ||
| elif not in_arg_section and line == "Parameters" and lines[i + 1] == "----------": |
There was a problem hiding this comment.
| assert not lines[i + 3] | ||
| assert lines[i + 4][:prefixlen] == " " * prefixlen | ||
| else: | ||
| assert line == f":param {arg}: .. version-deprecated:: 2.718" |
There was a problem hiding this comment.
does that render as expected?
| in_arg_section = True | ||
| docmsg = indent(docmsg, " ") | ||
| elif in_arg_section: | ||
| if docstring_style == "numpy" and line == arg: |
There was a problem hiding this comment.
in numpy style, there can be a : qualifier after the param name.
| break | ||
| elif ( | ||
| docstring_style == "numpy" | ||
| and set(line.strip()) == {"-"} |
There was a problem hiding this comment.
Could you add a comment about what that singular - is about? I remember handling that too elsewhere, but I forget.
|
@flying-sheep I think perhaps it would make sense at this point to add something like docstring-parser as a dependency. This will alow us to parse a docstring into a DocString object, edit the DocstringParam object, and render an updated docstring. This would also simplify docstring handling throughoug scverse-misc. EDIT: or maybe pydocstring-rs |
|
Before we do that, we should revisit the idea of not doing any docstring manipulation at runtime and doing that in a Sphinx plugin instead. Don’t get me wrong, something like |
|
How would the Sphinx-plugin work? The decorator adds e.g. |
|
yup, but
only question is if that would still integrate so neatly with autosummary-generate … worth an experiment To come back to this PR – maybe we add the A PR that adds usage of |
I think a disadvantage with having it in sphinx might also be that the docs shown in IDEs will not be respecting that? |
|
they don’t show it anyway, they only show static docstrings, not runtime docstrings. |
A warning is emitted if one of the following is true: