qntm / greenery

Regular expression manipulation library
http://qntm.org/greenery
MIT License
331 stars 40 forks source link

introduce strict-ish pylint + minimal fixes #98

Closed rwe closed 1 year ago

rwe commented 1 year ago

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 to StateType/AlphaType. They could be given as aliases, but seemed unlikely for anyone to care).

The most visible changes are:

This isn't motivated by much, just a branch I had following me around.

rwe commented 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).