{"record":{"id":"eb20a23917a43563","repo":"TheAlgorithms/Python","slug":"unsupported-type-given-for-another-type-another","errorCode":null,"errorMessage":"Unsupported type given for another ({type(another)})","messagePattern":"Unsupported type given for another \\((.+?)\\)","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"matrix/sherman_morrison.py","lineNumber":178,"sourceCode":"        \"\"\"\n\n        if isinstance(another, (int, float)):  # Scalar multiplication\n            result = Matrix(self.row, self.column)\n            for r in range(self.row):\n                for c in range(self.column):\n                    result[r, c] = self[r, c] * another\n            return result\n        elif isinstance(another, Matrix):  # Matrix multiplication\n            assert self.column == another.row\n            result = Matrix(self.row, another.column)\n            for r in range(self.row):\n                for c in range(another.column):\n                    for i in range(self.column):\n                        result[r, c] += self[r, i] * another[i, c]\n            return result\n        else:\n            msg = f\"Unsupported type given for another ({type(another)})\"\n            raise TypeError(msg)\n\n    def transpose(self) -> Matrix:\n        \"\"\"\n        <method Matrix.transpose>\n        Return self^T.\n        Example:\n        >>> a = Matrix(2, 3)\n        >>> for r in range(2):\n        ...     for c in range(3):\n        ...             a[r,c] = r*c\n        ...\n        >>> a.transpose()\n        Matrix consist of 3 rows and 2 columns\n        [0, 0]\n        [0, 1]\n        [0, 2]\n        \"\"\"\n","sourceCodeStart":160,"sourceCodeEnd":196,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/matrix/sherman_morrison.py#L160-L196","documentation":"Raised by the local Matrix class's multiplication method in sherman_morrison when the right operand is neither a number nor a Matrix instance. This standalone Matrix class (used for the Sherman-Morrison inverse-update algorithm) only supports scalar multiplication and matrix multiplication; anything else — strings, nested lists, numpy arrays — is rejected with a TypeError naming the offending type.","triggerScenarios":"sherman_morrison_matrix * [[1, 0], [0, 1]] (raw list), matrix * np.array(...), or matrix * None (a variable that failed to initialize). The f-string in the message tells you exactly which type arrived, e.g. 'Unsupported type given for another (<class \\\"list\\\">)'.","commonSituations":"Interoperating with NumPy arrays; passing unwrapped nested-list test fixtures; None leaking in from a failed lookup of a second matrix; feeding data straight from JSON.","solutions":["Read the type in the error message to identify the culprit operand.","Wrap nested lists in this module's Matrix class before multiplying.","Convert NumPy arrays to plain numbers or to this Matrix type; or move the whole computation into NumPy.","Check for None operands (failed initialization/lookup) before the multiply."],"exampleFix":"# before\nresult = a * [[1, 0], [0, 1]]  # TypeError: got <class 'list'>\n\n# after\nresult = a * Matrix(2, 2, [[1, 0], [0, 1]])  # or however the class constructor takes data","handlingStrategy":"type-guard","validationCode":"def as_sherman_matrix(x, MatrixCls):\n    \"\"\"Wrap nested lists in this module's Matrix; pass numbers and Matrix through.\"\"\"\n    if isinstance(x, (int, float)) or isinstance(x, MatrixCls):\n        return x\n    if isinstance(x, (list, tuple)):\n        return MatrixCls(len(x), len(x[0]), x)\n    raise TypeError(f\"unsupported operand {type(x).__name__}\")\n\nresult = a * as_sherman_matrix(other, Matrix)","typeGuard":"def is_mul_operand(x, MatrixCls) -> bool:\n    \"\"\"Guard: number or this module's Matrix instance.\"\"\"\n    return isinstance(x, (int, float)) or isinstance(x, MatrixCls)","tryCatchPattern":"try:\n    result = a * operand\nexcept TypeError as e:\n    if \"Unsupported type given for another\" in str(e):\n        # message names the actual type; fix the operand at its source\n        raise TypeError(f\"bad operand came from upstream: {operand!r}\") from e\n    raise","preventionTips":["Wrap raw nested-list fixtures in this module's Matrix class in tests.","Keep NumPy arrays out of expressions involving this class, or convert the whole computation to NumPy.","Guard against None (failed lookups) before multiplying — its type shows up in this error message too."],"tags":["matrix","sherman-morrison","typeerror","type-mismatch"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}