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) + offsettotal = np.zeros(n) + offsetWhat the rule reports
Section titled “What the rule reports”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.
How to fix it
Section titled “How to fix it”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.
Message
Section titled “Message”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.