Skip to content

numpy/read-uninitialized

Rule read-uninitialized
Pack NumPy
Severity error
Fix unsafe, with xpd check --fix --unsafe-fixes
Library versions every version

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.

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.

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

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

Browse the other rules of the NumPy pack.