You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In most circumstances numpy scalar types can be used as drop in replacements to Python's scalar types. PyO3 is almost no exception to this rule as it can successfully extract Rust types from many numpy types such as np.int32, np.float32, etc. However, for some reason it is not possible to extract a Rust boolean from a numpy boolean. Consider this example:
When running python test.py, the first print statement prints 42 as expected. The second print statement however raises an exception:
Traceback (most recent call last):
File "/home/****/****/test.py", line 6, in <module>
print(bool_as_string(a > 0))
TypeError: argument 'a': 'bool_' object cannot be converted to 'PyBool'
There is a simple workaround, as the numpy boolean can be easily converted to a Python boolean before calling the Rust function.
print(bool_as_string(bool(a>0))) # works
However, it would still be nice if this issue could be fixed within PyO3 as it is hard to differentiate between Python booleans and numpy booleans as a developer in a project which heavily relies on numpy.
This was tested with the latest versions of PyO3 (0.20.0) and numpy (1.26.2).
The text was updated successfully, but these errors were encountered:
In most circumstances numpy scalar types can be used as drop in replacements to Python's scalar types. PyO3 is almost no exception to this rule as it can successfully extract Rust types from many numpy types such as np.int32, np.float32, etc. However, for some reason it is not possible to extract a Rust boolean from a numpy boolean. Consider this example:
When running
python test.py
, the first print statement prints42
as expected. The second print statement however raises an exception:There is a simple workaround, as the numpy boolean can be easily converted to a Python boolean before calling the Rust function.
However, it would still be nice if this issue could be fixed within PyO3 as it is hard to differentiate between Python booleans and numpy booleans as a developer in a project which heavily relies on numpy.
This was tested with the latest versions of PyO3 (0.20.0) and numpy (1.26.2).
The text was updated successfully, but these errors were encountered: