# Cast numpy integer into Mojo integer

**URL:** <https://forum.modular.com/t/cast-numpy-integer-into-mojo-integer/670>\
**Category:** Mojo\
**Tags:** debugging, 25\_1\
**Created:** [March 3, 2025, 11:14pm UTC](https://forum.modular.com/t/cast-numpy-integer-into-mojo-integer/670 "2025-03-03T23:14:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ferdinand](https://sea1.discourse-cdn.com/flex001/user_avatar/forum.modular.com/ferdinand/32/120_2.png) [@ferdinand](https://forum.modular.com/u/ferdinand)\
**Post date:** [March 3, 2025, 11:14pm UTC](https://forum.modular.com/t/cast-numpy-integer-into-mojo-integer/670/1 "2025-03-03T23:14:46Z")

</div>

I’m updating this little [library](https://github.com/fnands/mimage) I have going to 25.1, and am having a bit of trouble with casting from numpy types to Mojo types.

Essentially I used to be able to cast `numpy.uint8` to a Mojo Int as:  
`py_array[rows][columns][channels]. __int__ ()`, where the elements of py\_array are `numpy.uint8`.

But now I get:  
`Unhandled exception caught during execution: an integer is required`

`. __as_int__ ()` has the same behaviour, as well as `Int(py_array[rows][columns][channels])`. This worked at least up to 24.5, so I am trying to see in the Changelog if anything about typing changed in the meantime, but nothing stands out to me.

In the [function](https://github.com/fnands/mimage/blob/main/tests/testing_utils.mojo#L6) I am trying to fix, I call I am comparing the values in a numpy array to the values in a Mojo tensor.

The numpy array is initialized as `var py_array = np.array(PillowImage.open(image_path))`

So is there a new/better way of casting numpy types to Mojo?

---

<div class="post-metadata">

**Author:** ![ferdinand](https://sea1.discourse-cdn.com/flex001/user_avatar/forum.modular.com/ferdinand/32/120_2.png) [@ferdinand](https://forum.modular.com/u/ferdinand)\
**Post date:** [March 7, 2025, 12:16pm UTC](https://forum.modular.com/t/cast-numpy-integer-into-mojo-integer/670/2 "2025-03-07T12:16:19Z")

</div>

Opened a bug report: [[BUG] Numpy ints no longer cast to Mojo · Issue #4119 · modular/max · GitHub](https://github.com/modular/max/issues/4119)

---

<div class="post-metadata">

**Author:** ![jack.clayton](https://sea1.discourse-cdn.com/flex001/user_avatar/forum.modular.com/jack.clayton/32/57_2.png) [@jack.clayton](https://forum.modular.com/u/jack.clayton)\
**Post date:** [March 7, 2025, 7:10pm UTC](https://forum.modular.com/t/cast-numpy-integer-into-mojo-integer/670/3 "2025-03-07T19:10:12Z")

</div>

Thanks @ferdinand have a fix landed that passes your tests PR: [[stdlib] Add tests for python and numpy casting by fnands · Pull Request #4120 · modular/max · GitHub](https://github.com/modular/max/pull/4120)

Will land those tests when the fix lands in the `max` repo, thanks for adding tests much appreciated.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex001/uploads/modular/original/1X/2751e0fbdc595a99718b216730957e9db4448cfd.jpeg) [@system](https://forum.modular.com/u/system)\
**Post date:** [March 14, 2025, 7:10pm UTC](https://forum.modular.com/t/cast-numpy-integer-into-mojo-integer/670/4 "2025-03-14T19:10:30Z")

</div>

Closed on March 14, 2025, 7:10pm UTC

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
