Skip to content

Commit a7f6e28

Browse files
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.
1 parent 6e03a48 commit a7f6e28

1 file changed

Lines changed: 4 additions & 2 deletions

File tree

‎matrix/matrix_operation.py‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,10 @@ def multiply(matrix_a: list[list[int]], matrix_b: list[list[int]]) -> list[list[
6666
>>> multiply([[1, 2, 3]], [[2], [3], [4]])
6767
[[20]]
6868
"""
69-
if _check_not_integer(matrix_a) and _check_not_integer(matrix_b):
70-
rows, cols = _verify_matrix_sizes(matrix_a, matrix_b)
69+
if not (_check_not_integer(matrix_a) and _check_not_integer(matrix_b)):
70+
raise TypeError("Expected a matrix, got int/list instead")
71+
72+
rows, cols = _verify_matrix_sizes(matrix_a, matrix_b)
7173

7274
if cols[0] != rows[1]:
7375
msg = (

0 commit comments

Comments
 (0)