Closed rwe closed 1 year ago
What exactly is this linter's problem with comparing things to
0
?
I think it's just trying to suggest consistency about something that's sometimes beneficial and basically never harmful.
The general recommendation is to use the least-powerful test. An expression like if len(x) > 0:
is asking for more information than it needs ("compute the length") and then throwing most of it away.
In most cases, this doesn't matter, since built-in collection types maintain their length eagerly, and __len__
or __bool__
both result in an equivalent field lookup. But it's not always implemented that way. Lazy collections, proxies, etc. can special-case __bool__
or can at least stop counting past one element to determine non-emptiness.
As a pattern, if x:
is briefer, arguably clearer, is sometimes more performant, and realistically never less performant.
(Historically, in Python 2, __bool__
was named __nonzero__
, same functionality).
This adds a default
pylintrc
, updates some project-specific fields, enables most of the built-in plugins, and suppresses all of the currently-triggering checkers.Following that, some low-hanging fruit is addressed, suppressing pretty liberally, with care to limit logical impact and interface changes. (Exception: renaming
state_type
/alpha_type
toStateType
/AlphaType
. They could be given as aliases, but seemed unlikely for anyone to care).The most visible changes are:
ValueError
orArithmeticError
exceptions vs.Exception
.Exception
includes things likeSyntaxError
andTypeError
and is just way too broad.next()
andmin()
/max()
not x
vslen(x) == 0
(generally good practice, since some collections can determine non-empty faster than they can count the entire collection) or using.items()
orenumerate(…)
to avoid repeated lookups in indexed collections.This isn't motivated by much, just a branch I had following me around.