python / python/mypy

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

Open
#14,405 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-mypyc
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

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()

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the supplied Python reproducer, especially the Book.assign, compute_1, and compute_2 methods, and run it through mypyc to reproduce the differing runtime behavior. Compare the generated handling of the two equivalent expressions and add a regression test if the relevant test location can be identified. Done means the compiled reproducer no longer raises the numpy.bool_ TypeError.

Written by the indexing model from the issue text.

Assessment

Tech stack
numpy, python
Domain
compilers, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
32/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.