Skip to content

numpy/as-strided

Rule as-strided
Pack NumPy
Severity info
Fix none, edit by hand
Library versions every version

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.

xpd prints this message with the issue. A name that starts with $ stands for code from your file.

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.

Browse the other rules of the NumPy pack.