mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-17 22:07:42 +00:00
96 lines
2.2 KiB
Python
96 lines
2.2 KiB
Python
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
|
|
|
|
|
|
# This is fine
|
|
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:
|
|
ZeroDivisionError: If you pass `0` for the time
|
|
TypeError: if you didn't pass a number for both parameters
|
|
"""
|
|
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
|
|
|
|
# DOC502 regression for Sphinx directive after Raises (issue #18959)
|
|
def foo():
|
|
"""First line.
|
|
|
|
Raises:
|
|
ValueError:
|
|
some text
|
|
|
|
.. versionadded:: 0.7.0
|
|
The ``init_kwargs`` argument.
|
|
"""
|
|
raise ValueError
|