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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

GitHub Action to lint Python code with ruff #2056

Closed
wants to merge 2 commits into from

Conversation

cclauss
Copy link

@cclauss cclauss commented Apr 11, 2023

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


馃摎 Documentation preview 馃摎: https://datasette--2056.org.readthedocs.build/en/2056/

@simonw
Copy link
Owner

simonw commented Apr 13, 2023

https://github.com/simonw/datasette/actions/runs/4664796647/jobs/8300596121?pr=2056 it's pretty fast - that finished in 9s.

@simonw
Copy link
Owner

simonw commented Apr 13, 2023

@simonw
Copy link
Owner

simonw commented Apr 13, 2023

Cool - and now https://github.com/simonw/datasette/pull/2056/files is showing me those inline annotations:

image

@simonw
Copy link
Owner

simonw commented Apr 13, 2023

OK, I'm sold - this is a really neat improvement.

One thing to change in the PR: right now it runs pip install --user ruff on every commit, which hits PyPI to install the package.

I prefer to avoid hitting PyPI every time, so I like to use the GitHub Actions cache. My usual pattern for that looks like this:

- name: Set up Python 3.10
uses: actions/setup-python@v3
with:
python-version: "3.10"
cache: 'pip'

Then a separate command that runs pip install ... will benefit from that cache.

Are you OK to make that change?

[Ruff](https://beta.ruff.rs/) supports [over 500 lint rules](https://beta.ruff.rs/docs/rules) and can be used to replace [Flake8](https://pypi.org/project/flake8/) (plus dozens of plugins), [isort](https://pypi.org/project/isort/), [pydocstyle](https://pypi.org/project/pydocstyle/), [yesqa](https://github.com/asottile/yesqa), [eradicate](https://pypi.org/project/eradicate/), [pyupgrade](https://pypi.org/project/pyupgrade/), and [autoflake](https://pypi.org/project/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](https://user-images.githubusercontent.com/3709715/223758136-afc386d2-70aa-4eff-953a-2c2d82ceea23.png)
@cclauss
Copy link
Author

cclauss commented Apr 13, 2023

Ruff (written in Rust, not Python) is a 23MB executable so the time to download and pip install it dwarfs its runtime.

Let's run ruff with and without GitHub Actions pip cache side-by-side to see the relative performance.

Once you approve the workflows below, ruff_with_cache should echo cache-hit = false but if you rerun that job hopefully it should echo cache-hit = true. That will be the execution time that we are interested to compare.

There are two great problems in computer science: ;-)

  1. Naming things
  2. Cache invalidation
  3. Off-by-one errors

For 2., https://github.com/actions/setup-python#caching-packages-dependencies is vital reading.
Only exactly pinned requirements can be cached. Currently in setup.py the only pinned dependencies are:

  1. Sphinx==6.1.3
  2. furo==2023.3.27
  3. black==23.3.0
  4. blacken-docs==1.13.0 # but unpinned elsewhere in setup.py

This means that there will be very few cache hits in the current actions. See the link below to print out cache hits:
https://github.com/actions/setup-python/blob/main/docs/advanced-usage.md#cache-hit

PyPI uses Fastly's CDN to quickly serve content to end-users, allowing us to minimize our hosting infrastructure and obscure possible downtime. -- https://pypi.org/sponsors

I would be shocked if Fastly does not have beefy CDN nodes in the same datacenters where GitHub Actions run so GHA requests to download ruff probably never hit a PyPI server.

@cclauss
Copy link
Author

cclauss commented Apr 15, 2023

Status?

@cclauss cclauss closed this Feb 10, 2024
@cclauss cclauss deleted the patch-1 branch February 10, 2024 10:08
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.

None yet

2 participants