Skip to content

numpy/as-strided

Pack Severity Fix Versions
NumPy info none, edit by hand All versions

as_strided reads memory outside the array when a shape or a stride is wrong. Use sliding_window_view for windows, broadcast_to for repeated values, or slicing and reshape for a regular step.

windows = as_strided(x, shape=(len(x) - 2, 3), strides=(x.strides[0], x.strides[0]))
windows = np.lib.stride_tricks.sliding_window_view(x, 3)

The rule reports as_strided(...), because a wrong shape or stride reads memory outside the array. Its docs ask for extreme care and point to sliding_window_view, broadcast_to and reshape.

xpd has no automatic fix. Because the shape and strides decide which safe function fits. Make the change by hand.

as_strided reads memory outside the array when a shape or stride is wrong, and NumPy's docs ask for extreme care with it. Use sliding_window_view for windows, broadcast_to for repeated values, or slicing and reshape for a regular step, which all stay inside the array.

See all rules in the NumPy pack.