Skip to content

Rework our linting setup #11522

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 12 commits into from
Mar 3, 2024
Merged

Rework our linting setup #11522

merged 12 commits into from
Mar 3, 2024

Conversation

AlexWaygood
Copy link
Member

  • Do all our linting via pre-commit, rather than doing ruff and flake8 via GitHub Actions. This has several advantages:
    • It's less code/configuration for us to worry about
    • We only have to have our ruff version pin in 2 places, rather than 3
    • pre-commit.ci is faster due to better caching
    • It's easier to do multiple runs of ruff with slightly different CLI arguments, which is useful if we only want to use --unsafe-fixes for one specific rule
    • It means we can simplify and reduce duplication in our protobuf-generation scripts
  • Bump ruff to the latest version
  • Add a new pre-commit hook check-hooks-apply. This will error out if a regex passed to the files parameter for any individual pre-commit hook matches 0 files
  • Move ruff before black in our pre-commit config file. It's reasonably common for ruff to produce code in its autofixes that will cause black violations.

I didn't succeed in running the protobuf-stub-generation scripts locally on my MacBook (something about realpath not accepting an -L1 argument on the MacOS version?), so please review the changes to those files carefully :)

Co-authored-by: Avasam <samuel.06@hotmail.com>
Copy link
Member Author

Choose a reason for hiding this comment

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

The changes to this file were necessary because the test currently assumes that all repos listed in .pre-commit-config.yaml will have a rev key in them, and that's not true for the "meta" repo that I'm adding as part of this PR

@AlexWaygood AlexWaygood requested a review from Avasam March 2, 2024 17:43
Copy link
Collaborator

@Avasam Avasam left a comment

Choose a reason for hiding this comment

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

A suggestion and a question. Everything else looks good to me

@@ -203,7 +194,7 @@ def main() -> None:
print(colored("\n\n--- TEST SUMMARY: One or more tests failed. See above for details. ---\n", "red"))
else:
print(colored("\n\n--- TEST SUMMARY: All tests passed! ---\n", "green"))
print("Flake8:", _SUCCESS if flake8_result.returncode == 0 else _FAILED)
print("pre-commit:", _SUCCESS if pre_commit_result.returncode == 0 else _FAILED)
Copy link
Collaborator

@Avasam Avasam Mar 2, 2024

Choose a reason for hiding this comment

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

If black formats a file, or Ruff autofixes something with no issues left, this shows up as failed. It's not major, but it is a change of behaviour.

Would it matter for the CI if black only fails on unformattable / syntax errors, and Ruff on leftover issues ? Since a change (format/autofix) retriggers the CI anyway iirc.

Also from CONTRIBUTING.md

Anything that can't be autofixed will show up as a CI failure

idk how this should interact with git pre-commit hooks if pre-commit is installed as one, given I don't use 'em.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good point. I think it's useful for black and ruff to give nonzero exit codes when they do autofixes, because it's sometimes important to manually review the output of the autofixes before making the PR. What do you think of 6f079ee?

idk how this should interact with git pre-commit hooks if pre-commit is installed as one, given I don't use 'em.

¯\_(ツ)_/¯ (I don't use them either 😄)

Copy link
Member Author

Choose a reason for hiding this comment

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

Example output from runtests.py if pre-commit fails, following 6f079ee:

image

Copy link
Collaborator

@Avasam Avasam Mar 3, 2024

Choose a reason for hiding this comment

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

I think it's useful for black and ruff to give nonzero exit codes when they do autofixes, because it's sometimes important to manually review the output of the autofixes before making the PR. What do you think of 6f079ee?

I think that's valid and something we already could've done by showing a warning instead of passing, for example.
Now with pre-commit, the next best thing is probably failing with a message like you did.

One tiny nitpick is that Check the output of pre-commit for more details. might be overly verbose given One or more tests failed. See above for details. is written two lines above. But that won't block my approval

Edit: Sorry but I find it really funny to see the "how to screenshot on mac" page in the background. Started my day with a good laugh 😄

Copy link
Member

Choose a reason for hiding this comment

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

Alex finding out the hard way that Terminal is transparent on Macs...

Copy link
Member Author

Choose a reason for hiding this comment

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

WHY CAN'T THERE BE A BUTTON ON THE KEYBOARD FOR PRINT-SCREENING LIKE ON WINDOWS

Copy link
Collaborator

@Avasam Avasam left a comment

Choose a reason for hiding this comment

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

This all LGTM. I've used pre-commit as the CLI command for linting/formatting in some project, and I don't see an issue using it more that way in typeshed.

All my concerns have been addressed though I may want to hear from another maintainer. Otherwise happy to merge.

@AlexWaygood AlexWaygood merged commit 35b74bc into python:main Mar 3, 2024
@AlexWaygood AlexWaygood deleted the pre-commit branch March 3, 2024 23:11
@AlexWaygood
Copy link
Member Author

Thanks, both!

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