mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-29 13:25:17 +00:00

## Summary Fixes #12630. DOC501 and DOC502 now understand functions with constructs like this to be explicitly raising `TypeError` (which should be documented in a function's docstring): ```py try: foo(): except TypeError: ... raise ``` I made an exception for `Exception` and `BaseException`, however. Constructs like this are reasonably common, and I don't think anybody would say that it's worth putting in the docstring that it raises "some kind of generic exception": ```py try: foo() except BaseException: do_some_logging() raise ``` ## Test Plan `cargo test -p ruff_linter --lib`
119 lines
2.4 KiB
Python
119 lines
2.4 KiB
Python
class FasterThanLightError(Exception):
|
|
...
|
|
|
|
|
|
# DOC502
|
|
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.
|
|
"""
|
|
return distance / time
|
|
|
|
|
|
# DOC502
|
|
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.
|
|
DivisionByZero
|
|
If attempting to divide by zero.
|
|
"""
|
|
return distance / time
|
|
|
|
|
|
# DOC502
|
|
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.
|
|
DivisionByZero
|
|
If attempting to divide by zero.
|
|
"""
|
|
try:
|
|
return distance / time
|
|
except ZeroDivisionError as exc:
|
|
raise FasterThanLightError from exc
|
|
|
|
|
|
# This is fine
|
|
def calculate_speed(distance: float, time: float) -> float:
|
|
"""Calculate speed as distance divided by time.
|
|
|
|
ACalculate 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
|
|
------
|
|
TypeError
|
|
If one or both of the parameters is not a number.
|
|
ZeroDivisionError
|
|
If attempting to divide by zero.
|
|
"""
|
|
try:
|
|
return distance / time
|
|
except ZeroDivisionError:
|
|
print("Oh no, why would you divide something by zero?")
|
|
raise
|
|
except TypeError:
|
|
print("Not a number? Shame on you!")
|
|
raise
|