From 2b42db0b69d3fd7a0708254bbe8161d0d4f12db8 Mon Sep 17 00:00:00 2001 From: Vedant Madane <6527493+VedantMadane@users.noreply.github.com> Date: Tue, 25 Aug 2026 01:01:32 +0530 Subject: [PATCH] fix: Raise on tensor division by zero - raise ZeroDivisionError on tensor division by zero - add unit test Fixes #10 Signed-off-by: Vedant Madane <6527493+VedantMadane@users.noreply.github.com> --- leanpass/tensor.py | 2 ++ tests/test_leanpass.py | 8 ++++++++ 2 files changed, 10 insertions(+) diff --git a/leanpass/tensor.py b/leanpass/tensor.py index 6ea1cfc..64c8c35 100644 --- a/leanpass/tensor.py +++ b/leanpass/tensor.py @@ -108,6 +108,8 @@ def __rmul__(self, other): def __truediv__(self, other): other = other if isinstance(other, Tensor) else Tensor(other) + if np.any(other.data == 0): + raise ZeroDivisionError("Tensor division by zero") out = self._create_child(self.data / other.data, "/", (self, other)) def _backward(): diff --git a/tests/test_leanpass.py b/tests/test_leanpass.py index 2890999..f70bef2 100644 --- a/tests/test_leanpass.py +++ b/tests/test_leanpass.py @@ -97,3 +97,11 @@ def test_tensor_gelu_backward(): assert x.grad.shape == x.data.shape assert np.all(np.isfinite(x.grad)) assert np.all(x.grad != 0) + + +def test_truediv_raises_on_zero_divisor(): + import pytest + x = Tensor([1.0, 2.0], requires_grad=True) + y = Tensor([1.0, 0.0], requires_grad=False) + with pytest.raises(ZeroDivisionError): + _ = x / y