Skip to content

numpy/read-uninitialized

Pack Severity Fix Versions
NumPy error unsafe, with xpd check --fix --unsafe-fixes All versions

np.empty leaves the values unset, so computing with them uses whatever bytes were in memory. Use np.zeros or fill the array before you read it.

total = np.empty(n) + offset
total = np.zeros(n) + offset

The rule reports np.empty(n).sum() and np.empty_like(x) + 1, which compute with whatever bytes were in memory.

The rule leaves np.empty(n) * np.nan alone, because it gives NaN everywhere.

An explicit zero dimension is also left alone because the array has no values to read.

xpd check --fix --unsafe-fixes applies the fixes of this rule. Check the result, because an unsafe fix can change what the code does.

The unsafe fix writes np.zeros(n) and np.zeros_like(a) with the same arguments, so the code computes on zeros.

$F leaves the values unset, so this computes with whatever bytes were in memory. Write $Z($$$ARGS), which starts from zeros.

See all rules in the NumPy pack.