feat: add modulo (%) support to calculate()

Add a "%" case to calculate() returning a % b. Extend the zero-division guard _check_div_zero to raise ZeroDivisionError for "%" alongside "/" and "//", and update the module/function docstrings and Raises section accordingly. Type annotations for the numeric operands simplified from float | int to float.

Pipeline stages: orchestrator -> implementer -> reviewer -> committer
Task: Add modulo (%) support to calculate()
This commit is contained in:
Andrew Emelyaneko
2026-09-28 15:20:14 +03:00
parent 6cc3455b06
commit f350764a95
+8 -6
View File
@@ -1,23 +1,23 @@
"""A small arithmetic calculator. """A small arithmetic calculator.
Provides the :func:`calculate` function which performs basic arithmetic Provides the :func:`calculate` function which performs basic arithmetic
operations (+, -, *, /, **, //) with proper error handling. operations (+, -, *, /, **, //, %) with proper error handling.
""" """
__all__ = ["calculate"] __all__ = ["calculate"]
def _check_div_zero(b: float | int, op: str) -> None: def _check_div_zero(b: float, op: str) -> None:
if op in ("/", "//") and b == 0: if op in ("/", "//", "%") and b == 0:
raise ZeroDivisionError("division by zero") raise ZeroDivisionError("division by zero")
def calculate(a: float | int, op: str, b: float | int) -> int | float: def calculate(a: float, op: str, b: float) -> int | float:
"""Perform arithmetic operations with proper error handling. """Perform arithmetic operations with proper error handling.
Args: Args:
a: First operand a: First operand
op: Operation (+, -, *, /, **, //) op: Operation (+, -, *, /, **, //, %)
b: Second operand b: Second operand
Returns: Returns:
@@ -26,7 +26,7 @@ def calculate(a: float | int, op: str, b: float | int) -> int | float:
Raises: Raises:
TypeError: If op is not a string, or if a or b is not a number TypeError: If op is not a string, or if a or b is not a number
ValueError: If operation is unknown ValueError: If operation is unknown
ZeroDivisionError: If dividing by zero ZeroDivisionError: If dividing or taking modulo by zero
""" """
if not isinstance(op, str): if not isinstance(op, str):
raise TypeError(f"operation must be a string, got {type(op).__name__}") raise TypeError(f"operation must be a string, got {type(op).__name__}")
@@ -52,5 +52,7 @@ def calculate(a: float | int, op: str, b: float | int) -> int | float:
return a ** b return a ** b
case "//": case "//":
return a // b return a // b
case "%":
return a % b
case _: case _:
raise ValueError(f"unknown operation: {op!r}") raise ValueError(f"unknown operation: {op!r}")