From a7f6e2863bd3e653a07fe85d0918c750fc33e120 Mon Sep 17 00:00:00 2001 From: ShoaibAbbasDev Date: Fri, 23 Oct 2026 13:17:23 +0500 Subject: [PATCH] fix(matrix): raise TypeError for non-matrix input in multiply multiply() only assigned rows and cols inside an if that had no else, so multiply([3], [4, 5]) crashed with UnboundLocalError. Raise the same TypeError("Expected a matrix, got int/list instead") that add, subtract and transpose already raise for invalid input. --- matrix/matrix_operation.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/matrix/matrix_operation.py b/matrix/matrix_operation.py index 89aab0552975..6104905d2919 100644 --- a/matrix/matrix_operation.py +++ b/matrix/matrix_operation.py @@ -66,8 +66,10 @@ def multiply(matrix_a: list[list[int]], matrix_b: list[list[int]]) -> list[list[ >>> multiply([[1, 2, 3]], [[2], [3], [4]]) [[20]] """ - if _check_not_integer(matrix_a) and _check_not_integer(matrix_b): - rows, cols = _verify_matrix_sizes(matrix_a, matrix_b) + if not (_check_not_integer(matrix_a) and _check_not_integer(matrix_b)): + raise TypeError("Expected a matrix, got int/list instead") + + rows, cols = _verify_matrix_sizes(matrix_a, matrix_b) if cols[0] != rows[1]: msg = (