[mypyc] TypeError: bool object expected; got numpy.bool_
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
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()
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne mit dem mitgelieferten Python-Reproduktionsbeispiel, insbesondere mit den Methoden Book.assign, compute_1 und compute_2, und führe es durch mypyc, um das unterschiedliche Laufzeitverhalten zu reproduzieren. Vergleiche die generierte Verarbeitung der beiden äquivalenten Ausdrücke und füge einen Regressionstest hinzu, wenn sich die relevante Teststelle ermitteln lässt. Erledigt ist die Aufgabe, wenn der kompilierte Reproducer nicht mehr den numpy.bool_ TypeError auslöst.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- numpy, python
- Bereich
- compilers, tooling
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Muss geklärt werden
- Anfängerfreundlichkeit
- 32/100