Skip to content
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

Added config file for the Ruff Python linter and fixed additional errors #2473

Open
wants to merge 45 commits into
base: main
Choose a base branch
from

Conversation

tdulcet
Copy link
Contributor

@tdulcet tdulcet commented Jan 8, 2025

  • Fixed additional errors found by Ruff. Continuation of Fixed errors found by the Ruff Python linter #2343
    • Fixed additional bad escape sequences, which are deprecated and produce a SyntaxWarning starting with Python 3.12.
    • Fixed to prevent leaking of the file descriptor for a temporary file.
    • Updated the syntax for better performance and compatibility with future Python versions.
  • Added pyproject.toml config file, to make it easier for contributors to run the linter.
  • Fixed syntax errors in tools/readable_bash.py caused by mixing tab and space indentation.

As requested previously, I only fixed a single rule per commit.

… `break` statement; remove the `else` and dedent its contents
@@ -530,8 +529,7 @@ def check_certificate(domain, ssl_certificate, ssl_private_key, warn_if_expiring
# should work in normal cases).
wildcard_domain = re.sub(r"^[^\.]+", "*", domain)
if domain not in certificate_names and wildcard_domain not in certificate_names:
return ("The certificate is for the wrong domain name. It is for %s."
% ", ".join(sorted(certificate_names)), None)
return ("The certificate is for the wrong domain name. It is for {}.".format(", ".join(sorted(certificate_names))), None)
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a reason why an f-string can't be used here?

return ("The certificate is for the wrong domain name. It is for {', '.join(sorted(certificate_names))}.", None)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, an f-string could of course be used here. This is just a limitation of the autofix for the UP032 rule, as it does not yet support fixes that would require handling of nested quote characters. See astral-sh/ruff#2031. These could be fixed manually, but it is obviously rather tedious, so I was just waiting for the autofix to be improved.


# There is some unknown problem. Return the `openssl verify` raw output.
return ("There is a problem with the certificate.", verifyoutput.strip())

# `openssl verify` returned a zero exit status so the cert is currently
Copy link
Contributor

Choose a reason for hiding this comment

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

From here to the end of the function is difficult to get through because of how github is presenting the unified diff. It appears to be okay, but maybe other eyes here would be good.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I would recommend reviewing each of the relevant commits separately, if needed.

@@ -223,14 +222,14 @@ class EditConf(Grammar):
EOL
)
def value(self):
conffile = self[1]
# conffile = self[1]
Copy link
Contributor

Choose a reason for hiding this comment

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

The conffile var is not used here, so the linter is set to comment out code like this versus deleting it altogether?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The autofix for the F841 rule wanted to remove the assignment, but keep the self[1] expression (Ruff does not know if it has side effects). I elected to comment it out instead, as this file does not actually run and so this variable may actually be needed if/when it is fixed.

Copy link
Contributor

@dms00 dms00 left a comment

Choose a reason for hiding this comment

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

Okay, so I've finally gotten through the entire PR. This was a slog, and it took multiple sessions over multiple days. I left some comments and questions about a few items but overall it looks fine to me.

I think it's difficult for our small brains to digest this size of changeset. This reminds me of a phenomenon I became aware of long ago in my workplace. Basically, I realized if someone posted a 25-line PR everyone had an opinion about it. But if someone posted a 500-line PR... crickets. And ultimately the large change would sail through with little to no dissent.

Because of this I believe it would be better to keep future linter PRs to smaller bite-sized chunks, something like 5 files or 30 lines, whichever comes first. Yes, it means more PRs but I believe they would be more readily reviewed and ultimately would be safer in terms of stability and security of the product. Just my 2 cents.

@tdulcet
Copy link
Contributor Author

tdulcet commented Feb 18, 2025

Thanks for taking the time to review this!

Because of this I believe it would be better to keep future linter PRs to smaller bite-sized chunks, something like 5 files or 30 lines, whichever comes first.

Each commit only fixes a single lint rule and almost all of them are less than 5 files/30 lines. In the future, I would recommend reviewing each of those individual commit separately, as that should make it much easier to see the specific changes. Of course, this PR along with #2343 fixes all of the low hanging fruit Ruff lintier errors, so any future PRs to fix the remaining errors would likely require some refactorings and thus a larger changeset.

Based on the Ruff config included in this PR, there are 520 remaining linter errors:

$ ruff check . | head -n -2 | awk '{ print $2 }' | sort | uniq -c | sort -rn
    122 F405
    100 PLC0415
     85 E701
     25 PLW2901
     24 E401
     20 FURB101
     15 E722
     12 B904
     11 FURB103
      9 ARG001
      8 E741
      8 BLE001
      7 TRY004
      7 TRY002
      5 S404
      4 W391
      4 S603
      4 S110
      4 PERF203
      4 A002
      3 S607
      3 S324
      3 FURB167
      2 UP038
      2 TRY301
      2 SIM108
      2 SIM105
      2 SIM102
      2 S310
      2 PLR6301
      1 TRY003
      1 SIM223
      1 SIM222
      1 SIM118
      1 S605
      1 S112
      1 S108
      1 PLW0603
      1 PLC0206
      1 FURB122
      1 FURB113
      1 F403
      1 EM102
      1 E402
      1 B901
      1 B012
      1 B007
      1 ARG002
      1 A001

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