Skip to content

all: Lint Python code with ruff. #651

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 24, 2023
Merged

Conversation

cclauss
Copy link
Contributor

@cclauss cclauss commented May 2, 2023

As hinted at micropython/micropython#10977 (comment)

Ruff supports over 500 lint rules and can be used to replace Flake8 (plus dozens of plugins), isort, pydocstyle, yesqa, eradicate, pyupgrade, and autoflake, all while executing (in Rust) tens or hundreds of times faster than any individual tool.

The ruff Action uses minimal steps to run in ~5 seconds, rapidly providing intuitive GitHub Annotations to contributors.

image

@cclauss cclauss force-pushed the ruff branch 2 times, most recently from 68a761f to 99806b5 Compare May 2, 2023 22:49
@dpgeorge
Copy link
Member

dpgeorge commented May 9, 2023

Thanks for working on this!

@cclauss cclauss force-pushed the ruff branch 2 times, most recently from de6a771 to 8d4b1ba Compare May 11, 2023 20:34
@cclauss cclauss requested review from dlech and dpgeorge May 15, 2023 15:50
@dpgeorge
Copy link
Member

Thanks for the reminder.

I rebased and squashed the changes. But looks like ruff was updated in the meantime and now has more warnings?

Note that some of the suggestions go against MicroPython best practice, eg because they allocate memory.

@cclauss cclauss force-pushed the ruff branch 2 times, most recently from 5e09c3d to 0ea0a9b Compare July 23, 2023 19:17
@cclauss
Copy link
Contributor Author

cclauss commented Jul 23, 2023

OK... Rebased. I do not see any memory allocations but please let me know if you do.

@@ -149,7 +149,7 @@ def send(self, sensor_data, adjust_output_power=True):
delta = time.ticks_diff(maybe_ack.ticks_ms, sent_at)
print(
f"ACKed with RSSI {rssi}, {delta}ms after sent "
+ f"(skew {delta-ACK_DELAY_MS-ack_packet_ms}ms)"
f"(skew {delta-ACK_DELAY_MS-ack_packet_ms}ms)"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MicroPython doesn't support adjacent f-strings, it needs the "+".

(This file is not tested by the CI here, because it's an example.)

@@ -141,7 +141,7 @@ async def send(self, sensor_data, adjust_output_power=True):
delta = time.ticks_diff(maybe_ack.ticks_ms, sent_at)
print(
f"ACKed with RSSI {rssi}, {delta}ms after sent "
+ f"(skew {delta-ACK_DELAY_MS-ack_packet_ms}ms)"
f"(skew {delta-ACK_DELAY_MS-ack_packet_ms}ms)"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The "+" is needed.

@@ -183,7 +183,7 @@ def main():
(sf, bw_khz, coding_rate, low_datarate_optimize) = modem_settings
print(
f"Modem config sf={sf} bw={bw_khz}kHz coding_rate=4/{coding_rate} "
+ f"low_datarate_optimize={low_datarate_optimize}"
f"low_datarate_optimize={low_datarate_optimize}"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The "+" is needed.

Signed-off-by: Christian Clauss <cclauss@me.com>
@cclauss
Copy link
Contributor Author

cclauss commented Jul 24, 2023

Ignore ruff rule ISC003 # micropython does not support implicit concatenation of f-strings

explicit-string-concatenation (ISC003)

Derived from the flake8-implicit-str-concat linter.

What it does

Checks for string literals that are explicitly concatenated (using the + operator).

Why is this bad?

For string literals that wrap across multiple lines, implicit string concatenation within parentheses is preferred over explicit concatenation using the + operator, as the former is more readable.

Example

z = (
    "The quick brown fox jumps over the lazy "
    + "dog"
)

Use instead:

z = (
    "The quick brown fox jumps over the lazy "
    "dog"
)

@dpgeorge dpgeorge merged commit 4da6e6f into micropython:master Jul 24, 2023
@dpgeorge
Copy link
Member

Thank you, looks pretty good now!

@cclauss cclauss deleted the ruff branch July 24, 2023 07:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants