mypy icon indicating copy to clipboard operation
mypy copied to clipboard

[mypyc] TypeError: bool object expected; got numpy.bool_

Open matoksoz opened this issue 1 year ago • 3 comments

When I run mypy check, everything seems to be ok. If I compile the code using mypyc and execute the compiled code, at runtime , I get the error " TypeError: bool object expected; got numpy.bool_" when executing the compute_2 function. Note that compute_1 and compute_2 are almost the same. The code is given below:


from __future__ import annotations
import numpy as np
import time
import numpy.typing as npt

class Book:
    def __init__(self)->None:
        self.x: float = 0.1
        self.y: float = 0.2
        self.z: float = 0.3
        self.x_d: float = 0.15
        self.y_d: float = 0.25
        self.z_d: float = 0.35
        self.price: float = 6.5
        self.new_price: np.float64 = np.float64(0.4)
        self.can_sell: bool = True
        self.history: npt.NDArray[np.float64] = np.array([1.2, 1.1, 1.4], dtype=np.float64)


    def compare(self)->None:
        if self.can_sell and self.price > self.new_price:
            print('This is ok: bool and np.bool_ comparison')
   
    def assign(self)->None:
        self.x_d = self.history[0]
  

    def compute_1(self)->None:
        x_d = self.x_d
        y_d = self.y_d
        z_d = self.z_d
        
        err = ((x_d - self.x) ** 2 + (y_d - self.y) ** 2 + (z_d - self.z) ** 2) ** 0.5 

        if err < self.price:
            print("This is also ok")


    def compute_2(self)->None:
        x_d = self.x_d
        y_d = self.y_d
        z_d = self.z_d
        
        err = ((x_d - self.x) ** 2 + (y_d - self.y) ** 2 + (z_d - self.z) ** 2) 
        err = err ** 0.5

        if err < self.price:
            print("This is not ok")

def main()->None:
    b = Book()
    b.compare()
    b.assign()
    b.compute_1()
    b.compute_2()

if __name__ == '__main__':
    main()

matoksoz avatar Jan 06 '23 08:01 matoksoz

Yeah, this is a little tricky. So there are two things at play: 1) why is compute_1 different from compute_2, 2) why the type error

The difference between compute_1 and compute_2 is that float ** float is of type Any, to avoid false positives with complex. So in compute_1 and when compiled you get dynamic code that matches Python semantics. But in compute_2, we have a type for err (from the first assignment), so mypy "knows" that err is float and so mypyc presumably tries to do some more optimal things.

Which brings us to to "why the type error". Which is probably that mypyc has some expectations for what the result of comparisons between floats are, which don't hold with whatever numpy is doing because numpy floats are not actually Liskov compatible with float, although I haven't actually investigated. You can reproduce this like so:

import numpy as np

def foo(x: float, y: float) -> None:
    if x < y:
        print("less")
    else:
        print("not less")

def main():
    x = np.float64(5)
    y = np.float64(6)
    foo(x, y)
λ python -c 'import x; x.main()'                                                                                    
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "x.py", line 12, in main
    foo(x, y)
  File "x.py", line 4, in foo
    if x < y:
TypeError: bool object expected; got numpy.bool_

I guess you have a workaround already in compute_1, but if you're looking for something more explicit, you could give err an explicit Any annotation.

hauntsaninja avatar Jan 06 '23 20:01 hauntsaninja

An import thing to keep in mind is that mypyc also doesn't (at least yet) know about the numpy-specific numeric types such as numpy.float64. They are treated like Any values at runtime, and there can be some weird interactions when coercing the values to a primitive type (there may be others beyond the bool case).

Also, the interactions with float64 etc. will likely change once we've implemented native, unboxed floats (mypyc/mypyc#966). Perhaps mypyc should support transparently converting float64 values to native float values. The only options seem to be that or requiring an explicit float(x) conversion.

JukkaL avatar Jan 09 '23 12:01 JukkaL

In my opinion, mypyc should treat np.float64 as native float and np.bool_ as native bool. In its current form, the compiler does not guarantee safe runtime operations using mixed types. If someone decides to use np.float64 everywhere, which does not support direct assignment such as x: np.float64 = 0.1, then he/she has to make conversion x = np.float64(0.1). This causes three issues: 1-) Your python interpreter will consume extra time during every conversion if you switch to python mode. 2-) Using np.float64 type is costly in both python and mypyc compiled modes. 3-) You have to modify your original python code instead of just doing type annotation and it is difficult to write x: = np.float64(0.1).

matoksoz avatar Jan 09 '23 20:01 matoksoz