Allow whitespace alignment of tables of numbers?
See original GitHub issueFor example, np.arange(0, 120).reshape(12, 10)
is formatted by NumPy as
array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[ 10, 11, 12, 13, 14, 15, 16, 17, 18, 19],
[ 20, 21, 22, 23, 24, 25, 26, 27, 28, 29],
[ 30, 31, 32, 33, 34, 35, 36, 37, 38, 39],
[ 40, 41, 42, 43, 44, 45, 46, 47, 48, 49],
[ 50, 51, 52, 53, 54, 55, 56, 57, 58, 59],
[ 60, 61, 62, 63, 64, 65, 66, 67, 68, 69],
[ 70, 71, 72, 73, 74, 75, 76, 77, 78, 79],
[ 80, 81, 82, 83, 84, 85, 86, 87, 88, 89],
[ 90, 91, 92, 93, 94, 95, 96, 97, 98, 99],
[100, 101, 102, 103, 104, 105, 106, 107, 108, 109],
[110, 111, 112, 113, 114, 115, 116, 117, 118, 119]])
but the linters used in unit testing reject tables of numbers like this because of rules like these:
- E201 whitespace after ‘[’
- E202 whitespace before ‘]’
- E221 multiple spaces before operator
- E222 multiple spaces after operator
- E241 multiple spaces after ‘,’
and require changing to the less readable
array([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14, 15, 16, 17, 18, 19],
[20, 21, 22, 23, 24, 25, 26, 27, 28, 29],
[30, 31, 32, 33, 34, 35, 36, 37, 38, 39],
[40, 41, 42, 43, 44, 45, 46, 47, 48, 49],
[50, 51, 52, 53, 54, 55, 56, 57, 58, 59],
[60, 61, 62, 63, 64, 65, 66, 67, 68, 69],
[70, 71, 72, 73, 74, 75, 76, 77, 78, 79],
[80, 81, 82, 83, 84, 85, 86, 87, 88, 89],
[90, 91, 92, 93, 94, 95, 96, 97, 98, 99],
[100, 101, 102, 103, 104, 105, 106, 107, 108, 109],
[110, 111, 112, 113, 114, 115, 116, 117, 118, 119]])
I’ve listed a bunch of other examples here: https://github.com/PyCQA/pycodestyle/issues/289#issuecomment-517937832
I think this is a misapplication of PEP8, and this type of formatting should be allowed, but turning off these error codes completely would be bad, since they are much broader than this, and are correct for one-line lists, etc.
Ideally the rules for tables of data would be changed in PEP8 and pycodestyle, so it trickles down to everything that depends on them, but my attempts at requesting changes to pycodestyle and PEP8 were rejected: https://github.com/PyCQA/pycodestyle/issues/289 https://github.com/python/peps/issues/1428 If you know of a better place to lobby for this, we could try that.
Otherwise, it seems all we could do is implement plugins to flake8 to prevent this from happening? Maybe there are other workarounds.
See also https://github.com/spyder-ide/spyder/issues/9955 and this PR failure and this PR failure.
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (6 by maintainers)
One possible option up at https://github.com/scipy/scipy/pull/12516
I also made a request to pycodestyle in https://github.com/PyCQA/pycodestyle/issues/924, before I saw @endolith’s earlier issue and comments.
I agree that we should be able to create aligned arrays of data without our style checker complaining about whitespace issues. I would have been OK (if not completely happy) with appending
# noqa
at the end of each line that is part of such an array, but that doesn’t disablepycodestyle
’s whitespace checker.With
flake8
, specific rules can be turned off, so one could use, say,# noqa: E201, E241
. I haven’t looked into what is involved in creating aflake8
plugin–maybe a plugin could be written that would require even less inline markup for this. Even without a plugin, I can support switching toflake8
in our CI style checks so we can use the# noqa
markup with arrays of numbers.