-
Notifications
You must be signed in to change notification settings - Fork 18
FIX: ignore numpy-style default values in docstrings #210
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Thanks! Let me take a look. |
Shoot -- are those CI errors from my changes? The tests run clean on my local python 3.12 install (macOS ventura). |
pydoclint/utils/doc.py
Outdated
# bar: int = 10 # noqa: E800 | ||
for k, metadata in enumerate(self.parsed.meta): | ||
if metadata.args[0] == 'param': | ||
# use of `in` can be replaced with a pre-compiled `re`, but |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @mazer-ai , could you double check your comment here? Because I don't see the use of in
here. Thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry -- originally used in
and then replaced with .find
so I could get the substring position in one go.. Both seem faster than trying to use an regex here.. Will update comments.
Hi @mazer-ai , could you add some test cases to check your code changes in this PR? Thank you! |
# supports a couple different specs: | ||
# Parameters | ||
# ---------- | ||
# foo: int, default 10 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we support all the 3 styles mentioned here?

And could you also add a note in the documentation (at least in docs/notes_for_users.md
, and preferably also in Section 2.7 of README)?
Thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These are all supported. I added some examples to the new test to confirm.
Hi @mazer-ai , after you make changes, you can run |
@jsh9 When I run tox locally, I get those same errors from tests/utils/test_arg.py
<unknown>:19: SyntaxWarning: invalid escape sequence '\_'
<unknown>:208: SyntaxWarning: invalid escape sequence '\_'
<unknown>:209: SyntaxWarning: invalid escape sequence '\_'
<unknown>:322: SyntaxWarning: invalid escape sequence '\_'
<unknown>:323: SyntaxWarning: invalid escape sequence '\_' Not sure why these escape sequences are present in test_args.py, but they seem to be illegal escape sequences: mazer@bridger $ python
Python 3.12.4 (main, Jun 6 2024, 18:26:44) [Clang 15.0.0 (clang-1500.1.0.2.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 'arg1\_\_'
<stdin>:1: SyntaxWarning: invalid escape sequence '\_'
'arg1\\_\\_'
>>> Any idea if I'm missing something obvious here? Or is this something strange about my dev env? Sorry about turning something simple into something complicated... |
Hi, don't worry about those warnings. Those existed before your PR. |
@jsh9 I think this should address your comments. Let me know if you see anything else. |
```python | ||
Parameters | ||
---------- | ||
arg1 : int, optional |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @mazer-ai , I think we probably need more work to specify the behavior here.
Because I think int, optional
is a valid way in the docstring for arg1: int | None = None
in the function signature, so we should add the logic to check this in the code.
```` | ||
|
||
The portion following the type hints are ignored and not checked for | ||
congruence with the function signature. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think if we want to allow annotating default values in the docstring, we need to check the congruence (between default value in the docstring and the default value in the function signature). Otherwise the behavior is surprising and implicit (i.e., we selectively check something, but not others).
Could you think of a way to also check the congruence of the default values as well?
Reminder: We need to update this section in README: Lines 187 to 193 in 2e8af22
|
Hi @mazer-ai , in some earlier code changes (5b850f1, and #258), I added this feature for both numpy style and google style. Newer versions (such as 0.7.3) supports this feature. Users need to set
Styles like "default is 0", "default = 0", "default: 0", etc. are not recognized. I think this restriction is a sensible trade-off to ensure uniform style within the same project. Therefore, I'm closing this PR. Thank you for your efforts! If you have other comments/suggestions, please feel free to open a new issue. |
Problem: The numpy format parser from
docstring_parser
doesn't remove the default value information fromtype_name
, resulting in a mismatch between the name in the function signature and the docstring for any parameters specifying a default value in the docstring.The numpy style doc is vague about "correct" way to specify default values, this patch handles two common patterns:
Both of which appear in numpy code and numpy-related projects.
Note that this could also be handled in
docstring_parser
, but since currentpydoclint
depends on.a forked version of the package, I opted to handle here it here, where it seemed simpler.Also, this could be done using a regex to search for and exclude the default info, but poking around, the general consensus seems to be that use of
in
or.find()
for short, static strings is faster than usingre.match
forsomething like this.
Finally, this problem doesn't occur for ReST formatted docstrings, and the specification for default values in google style docstrings is even more poorly defined than for numpy, but from the examples I found on-line, the current parser works fine. So this seems to really just be a numpy issue.