Files
plagin_tokens/pipeline_test.py
T
Andrew Emelyaneko f350764a95 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()
2026-09-28 15:20:14 +03:00

59 lines
1.6 KiB
Python

"""A small arithmetic calculator.
Provides the :func:`calculate` function which performs basic arithmetic
operations (+, -, *, /, **, //, %) with proper error handling.
"""
__all__ = ["calculate"]
def _check_div_zero(b: float, op: str) -> None:
if op in ("/", "//", "%") and b == 0:
raise ZeroDivisionError("division by zero")
def calculate(a: float, op: str, b: float) -> int | float:
"""Perform arithmetic operations with proper error handling.
Args:
a: First operand
op: Operation (+, -, *, /, **, //, %)
b: Second operand
Returns:
Result of the operation
Raises:
TypeError: If op is not a string, or if a or b is not a number
ValueError: If operation is unknown
ZeroDivisionError: If dividing or taking modulo by zero
"""
if not isinstance(op, str):
raise TypeError(f"operation must be a string, got {type(op).__name__}")
if not isinstance(a, (int, float)):
raise TypeError(f"operand a must be a number, got {type(a).__name__}")
if not isinstance(b, (int, float)):
raise TypeError(f"operand b must be a number, got {type(b).__name__}")
_check_div_zero(b, op)
match op:
case "+":
return a + b
case "-":
return a - b
case "*":
return a * b
case "/":
return a / b
case "**":
return a ** b
case "//":
return a // b
case "%":
return a % b
case _:
raise ValueError(f"unknown operation: {op!r}")