-
Notifications
You must be signed in to change notification settings - Fork 15
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
feat: introduce DOC503 for checking specific raised exceptions #161
Conversation
3c223e5
to
ab605d5
Compare
*couple leftover comments, fixed + squashed. |
ab605d5
to
671dd96
Compare
Interestingly, if you have *aaaaaaand, |
This commit compares any raise statements in function bodies to docstring Raises sections and ensures they match. Exceptions specified in a docstring are kept as a sorted list, so duplicate lines will throw DOC503 as well.
671dd96
to
3d62c7d
Compare
try:
pass
except OSError as e:
if e.args[0] == 2 and e.filename:
fp = None
else:
raise |
Introduces a `walk.walk_dfs` for DFS traversal of a node. This allows us to more easily keep track of direct parents of nested children, so that we can find an ExceptHandler that may be containing any Raise statement. Update tests for walk_dfs and some more complex edge cases for try/except blocks.
1c1d395
to
a22d8a1
Compare
Thank you for helping implement this feature! I'm taking a look at it, and it should take me a day or two. |
@@ -33,9 +33,9 @@ def walk(node): | |||
|
|||
|
|||
def walk_dfs(node): | |||
"""Depth-first traversal of AST. Modified from walk.walk, above.""" | |||
"""Depth-first traversal of AST. Modified from `walk()` in this file""" |
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.
Note: I changed the wording "above" because there's no guarantee that the relative positions of these 2 functions won't be changed in the future.
@@ -376,7 +376,7 @@ def func9(d): | |||
raise | |||
|
|||
def func10(): | |||
# no variable resolution is done. this function looks like it throws "GError". | |||
# no variable resolution is done. this function looks like it throws GError. |
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.
Note: I removed quotation marks just to fit the comment within 80 chars
Hi @Amar1729 , sorry for taking so long to get to this. I've been busy with other things last month. Everything looks good now, so I'll merge and publish. |
@jsh9 no worries, I get it. thanks for coming back around to it! |
Closes #158
Compare any raise statements in function bodies to docstring Raises sections and ensures they match. Exceptions specified in a docstring are kept as a sorted list, so duplicate lines will throw DOC503 as well.
Had to introduce a new walker,
walk.walk_dfs
, but I did add tests for it.This feature does NOT do any resolution of errors, so something like the following:
will not identify any specific exceptions thrown. Similarly,
except Exception: raise
will expectException
to be included in the docstring. I think both of those cases are already caught as style violations by certain ruff rules (probably underBLE
orTRY
?), so it might not be much of a problem.While this checks names of exceptions it doesn't check order(?) like your issue title mentions, but i'm not entirely sure what was meant by that.
Completely open to feedback / changes, let me know if there's anything that could be cleaned up. In particular, there's one bit I don't love: in
visitor.py
, i had to do this to parse a common convention when using rest-style docstrings:This bit parses docstring lines like
:raises: Exception:
, which seem to be somewhat common in sphinx docstrings but I don't think are technically allowed by the spec[1][2]?docstring_parser
doesn't currently parse those words intotype_name
. Not sure if this bit of code should be moved intodocstring_parser_fork
, moved into its own function (I wasn't really sure where else to put it), or if you'd prefer to just not support this line format.