-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[
pydoclint
] Implement docstring-missing-exception
and `docstring-…
…extraneous-exception` (`DOC501`, `DOC502`) (#11471) ## Summary These are the first rules implemented as part of #458, but I plan to implement more. Specifically, this implements `docstring-missing-exception` which checks for raised exceptions not documented in the docstring, and `docstring-extraneous-exception` which checks for exceptions in the docstring not present in the body. ## Test Plan Test fixtures added for both google and numpy style.
- Loading branch information
1 parent
53b84ab
commit 4bc73dd
Showing
21 changed files
with
1,161 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
192 changes: 192 additions & 0 deletions
192
crates/ruff_linter/resources/test/fixtures/pydoclint/DOC501_google.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,192 @@ | ||
import something | ||
from somewhere import AnotherError | ||
|
||
|
||
class FasterThanLightError(Exception): | ||
... | ||
|
||
|
||
_some_error = Exception | ||
|
||
|
||
# OK | ||
def calculate_speed(distance: float, time: float) -> float: | ||
"""Calculate speed as distance divided by time. | ||
Args: | ||
distance: Distance traveled. | ||
time: Time spent traveling. | ||
Returns: | ||
Speed as distance divided by time. | ||
Raises: | ||
FasterThanLightError: If speed is greater than the speed of light. | ||
""" | ||
try: | ||
return distance / time | ||
except ZeroDivisionError as exc: | ||
raise FasterThanLightError from exc | ||
|
||
|
||
# DOC501 | ||
def calculate_speed(distance: float, time: float) -> float: | ||
"""Calculate speed as distance divided by time. | ||
Args: | ||
distance: Distance traveled. | ||
time: Time spent traveling. | ||
Returns: | ||
Speed as distance divided by time. | ||
""" | ||
try: | ||
return distance / time | ||
except ZeroDivisionError as exc: | ||
raise FasterThanLightError from exc | ||
|
||
|
||
# DOC501 | ||
def calculate_speed(distance: float, time: float) -> float: | ||
"""Calculate speed as distance divided by time. | ||
Args: | ||
distance: Distance traveled. | ||
time: Time spent traveling. | ||
Returns: | ||
Speed as distance divided by time. | ||
""" | ||
try: | ||
return distance / time | ||
except ZeroDivisionError as exc: | ||
raise FasterThanLightError from exc | ||
except: | ||
raise ValueError | ||
|
||
|
||
# DOC501 | ||
def calculate_speed(distance: float, time: float) -> float: | ||
"""Calculate speed as distance divided by time. | ||
Args: | ||
distance: Distance traveled. | ||
time: Time spent traveling. | ||
Returns: | ||
Speed as distance divided by time. | ||
""" | ||
try: | ||
return distance / time | ||
except ZeroDivisionError as exc: | ||
print('oops') | ||
raise exc | ||
|
||
|
||
# DOC501 | ||
def calculate_speed(distance: float, time: float) -> float: | ||
"""Calculate speed as distance divided by time. | ||
Args: | ||
distance: Distance traveled. | ||
time: Time spent traveling. | ||
Returns: | ||
Speed as distance divided by time. | ||
""" | ||
try: | ||
return distance / time | ||
except (ZeroDivisionError, ValueError) as exc: | ||
print('oops') | ||
raise exc | ||
|
||
|
||
# DOC501 | ||
def calculate_speed(distance: float, time: float) -> float: | ||
"""Calculate speed as distance divided by time. | ||
Args: | ||
distance: Distance traveled. | ||
time: Time spent traveling. | ||
Returns: | ||
Speed as distance divided by time. | ||
""" | ||
raise AnotherError | ||
|
||
|
||
# DOC501 | ||
def calculate_speed(distance: float, time: float) -> float: | ||
"""Calculate speed as distance divided by time. | ||
Args: | ||
distance: Distance traveled. | ||
time: Time spent traveling. | ||
Returns: | ||
Speed as distance divided by time. | ||
""" | ||
raise AnotherError() | ||
|
||
|
||
# DOC501 | ||
def foo(bar: int): | ||
"""Foo. | ||
Args: | ||
bar: Bar. | ||
""" | ||
raise something.SomeError | ||
|
||
|
||
# DOC501, but can't resolve the error | ||
def calculate_speed(distance: float, time: float) -> float: | ||
"""Calculate speed as distance divided by time. | ||
Args: | ||
distance: Distance traveled. | ||
time: Time spent traveling. | ||
Returns: | ||
Speed as distance divided by time. | ||
""" | ||
raise _some_error | ||
|
||
|
||
# OK | ||
def calculate_speed(distance: float, time: float) -> float: | ||
try: | ||
return distance / time | ||
except ZeroDivisionError as exc: | ||
raise FasterThanLightError from exc | ||
|
||
|
||
# OK | ||
def calculate_speed(distance: float, time: float) -> float: | ||
raise NotImplementedError | ||
|
||
|
||
# OK | ||
def foo(bar: int): | ||
"""Foo. | ||
Args: | ||
bar: Bar. | ||
Raises: | ||
SomeError: Wow. | ||
""" | ||
raise something.SomeError | ||
|
||
|
||
# OK | ||
def foo(bar: int): | ||
"""Foo. | ||
Args: | ||
bar: Bar. | ||
Raises: | ||
something.SomeError: Wow. | ||
""" | ||
raise something.SomeError |
78 changes: 78 additions & 0 deletions
78
crates/ruff_linter/resources/test/fixtures/pydoclint/DOC501_numpy.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
class FasterThanLightError(Exception): | ||
... | ||
|
||
|
||
# OK | ||
def calculate_speed(distance: float, time: float) -> float: | ||
""" | ||
Calculate speed as distance divided by time. | ||
Parameters | ||
---------- | ||
distance : float | ||
Distance traveled. | ||
time : float | ||
Time spent traveling. | ||
Returns | ||
------- | ||
float | ||
Speed as distance divided by time. | ||
Raises | ||
------ | ||
FasterThanLightError | ||
If speed is greater than the speed of light. | ||
""" | ||
try: | ||
return distance / time | ||
except ZeroDivisionError as exc: | ||
raise FasterThanLightError from exc | ||
|
||
|
||
# DOC501 | ||
def calculate_speed(distance: float, time: float) -> float: | ||
""" | ||
Calculate speed as distance divided by time. | ||
Parameters | ||
---------- | ||
distance : float | ||
Distance traveled. | ||
time : float | ||
Time spent traveling. | ||
Returns | ||
------- | ||
float | ||
Speed as distance divided by time. | ||
""" | ||
try: | ||
return distance / time | ||
except ZeroDivisionError as exc: | ||
raise FasterThanLightError from exc | ||
|
||
|
||
# DOC501 | ||
def calculate_speed(distance: float, time: float) -> float: | ||
""" | ||
Calculate speed as distance divided by time. | ||
Parameters | ||
---------- | ||
distance : float | ||
Distance traveled. | ||
time : float | ||
Time spent traveling. | ||
Returns | ||
------- | ||
float | ||
Speed as distance divided by time. | ||
""" | ||
try: | ||
return distance / time | ||
except ZeroDivisionError as exc: | ||
raise FasterThanLightError from exc | ||
except: | ||
raise ValueError |
58 changes: 58 additions & 0 deletions
58
crates/ruff_linter/resources/test/fixtures/pydoclint/DOC502_google.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
class FasterThanLightError(Exception): | ||
... | ||
|
||
|
||
# DOC502 | ||
def calculate_speed(distance: float, time: float) -> float: | ||
"""Calculate speed as distance divided by time. | ||
Args: | ||
distance: Distance traveled. | ||
time: Time spent traveling. | ||
Returns: | ||
Speed as distance divided by time. | ||
Raises: | ||
FasterThanLightError: If speed is greater than the speed of light. | ||
""" | ||
return distance / time | ||
|
||
|
||
# DOC502 | ||
def calculate_speed(distance: float, time: float) -> float: | ||
"""Calculate speed as distance divided by time. | ||
Args: | ||
distance: Distance traveled. | ||
time: Time spent traveling. | ||
Returns: | ||
Speed as distance divided by time. | ||
Raises: | ||
FasterThanLightError: If speed is greater than the speed of light. | ||
DivisionByZero: Divide by zero. | ||
""" | ||
return distance / time | ||
|
||
|
||
# DOC502 | ||
def calculate_speed(distance: float, time: float) -> float: | ||
"""Calculate speed as distance divided by time. | ||
Args: | ||
distance: Distance traveled. | ||
time: Time spent traveling. | ||
Returns: | ||
Speed as distance divided by time. | ||
Raises: | ||
FasterThanLightError: If speed is greater than the speed of light. | ||
DivisionByZero: Divide by zero. | ||
""" | ||
try: | ||
return distance / time | ||
except ZeroDivisionError as exc: | ||
raise FasterThanLightError from exc |
Oops, something went wrong.