micropython / micropython-lib

Core Python libraries ported to MicroPython
Other
2.3k stars 980 forks source link

all: Enable ruff E401,E402 import lints. #858

Closed projectgus closed 1 month ago

projectgus commented 1 month ago

Mostly small cleanups, and explicit disable for examples/tests which insert the current directory into the path before importing.

dpgeorge commented 1 month ago

This is a bit of a change, making each import go on a separate line. But I guess we have to do that because we have committed to using ruff check for our style.

Luckily there is no difference in bytecode between import a, b and import a; import b.

projectgus commented 1 month ago

This is a bit of a change, making each import go on a separate line. But I guess we have to do that because we have committed to using ruff check for our style.

I think we could reasonably commit to ignoring these, especially E402 (imports not at top ) because we need to add noqa for legitimate use case in quite a few places.

However, the current usage is pretty arbitrary - sometimes imports on the same line, sometimes imports on different lines, sometimes the same source file has both approaches for no apparent reason. So it is also nice to be consistent, IMO.

stinos commented 1 month ago

Each import on its own line (or even going further: isort style sorted and grouped by type) just seems cleaner overall: more readable, nicer diffs, consistency.

projectgus commented 1 month ago

All imports on one line (or even going further: isort style sorted and grouped) just seems cleaner overall: more readable, nicer diffs, consistency.

I don't exactly disagree, but I do want to point out that these individual small differences in style preference is why many projects just pick a formatter (like ruff) and have it auto-format the code. Instant consistency, minimal time overhead. No one gets their preferred individual style, but the code base all has the same style.

dpgeorge commented 1 month ago

or even going further: isort style sorted and grouped

AFAIK, isort keeps separate modules on separate import lines??

nicer diffs

Aren't diffs smaller if imports are on separate lines?

stinos commented 1 month ago

AFAIK, isort keeps separate modules on separate import lines??

Depends, it has a bit too much configuration options actually. Though I think ruff's isort implementation is betrer in that regard.

Aren't diffs smaller if imports are on separate lines?

Oops. I actually did mean to say 'all imports on their own line' ! Will edit original comment.

dpgeorge commented 1 month ago

OK, we're all on the same page now :)

Merged.