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)What the rule reports
Section titled “What the rule reports”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.
How to fix it
Section titled “How to fix it”xpd has no automatic fix. Because the shape and strides decide which safe function fits. Make the change by hand.
Message
Section titled “Message”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.