When I do a matrix multiplication with numpy
(Python 3.12.1, numpy 1.26.4, PyCharm 2024.3.1 (Professional Edition)), I get this warning which I believe is wrong:
Unresolved attribute reference 'all' for class 'bool'
Minimial Reproducible Example
import numpy as np
a_matrix = np.array([[1, 2], [3, 4]])
b_matrix = np.array([[10], [20]])
a = [5.0]
b = [2.0]
if ((a_matrix @ np.array([[round(a[0], 0)], [round(b[0], 0)]])) == b_matrix).all():
print("Success")
The issue is with the ==
operator. It appears the numpy
class definition in the numpy
library specifies that it returns a typing.Any
with a type hint, so pycharm assumes it was the usual return type of builtins.bool
.
Here is the line from the numpy
library that pycharm is using, copied for context and posterity and abbreviated to the key components for brevity:
class _ArrayOrScalarCommon:
...
def __eq__(self, other: Any, /) -> Any: ...
...
class ndarray(_ArrayOrScalarCommon, Generic[_ShapeT_co, _DType_co]):
...
I confirmed that the typing.Any
hint causes this issue by creating a custom class and seeing what the return type hint suggested and it matched exactly. This code runs without errors for the same reason yours does, it returns a numpy.ndarray
which supports the .all
method.
from typing import Any
import numpy
class A:
def __eq__(self, other) -> Any:
return numpy.array([])
a = A()
(a == a).all()
Rearranging doesn't remove the issue, but makes the issue more obvious for the next person to come along and check your code with a static type checker by very clearly indicating what you expect the return type of that operation to be.
from typing import Any
import numpy
class A:
def __eq__(self, other) -> Any:
return numpy.array([])
a = A()
equal_result: numpy.ndarray = a == a
equal_result.all()
Whether you consider this to be a pycharm error by assuming that a return type of typing.Any
is a builtins.bool
or if it is a numpy
error by type hinting typing.Any
on a ==
between two numpy.ndarray
error is up to you.
This has been discussed in the numpy
community before, here is what appears to be the main discussion on the topic.