Skip to content

Commit

Permalink
fix: bump pc/gha and update for newer Ruff (#65)
Browse files Browse the repository at this point in the history
* fix: bump and add nox job to bump

Signed-off-by: Henry Schreiner <[email protected]>

* fix: modern versions of Ruff updates

Signed-off-by: Henry Schreiner <[email protected]>

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* docs: mention pipx run script

Signed-off-by: Henry Schreiner <[email protected]>

---------

Signed-off-by: Henry Schreiner <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
henryiii and pre-commit-ci[bot] authored Oct 3, 2024
1 parent 74de493 commit f2fc2d5
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 29 deletions.
4 changes: 2 additions & 2 deletions content/week04/cd.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
- name: Build SDist and wheel
run: pipx run build

- uses: actions/upload-artifact@v3
- uses: actions/upload-artifact@v4
with:
path: dist/*

Expand All @@ -30,7 +30,7 @@ jobs:
if: github.event_name == 'release' && github.event.action == 'published'

steps:
- uses: actions/download-artifact@v3
- uses: actions/download-artifact@v4
with:
name: artifact
path: dist
Expand Down
6 changes: 3 additions & 3 deletions content/week04/ci.md
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ configure Pages.
```yaml
- name: Setup Pages
id: pages
uses: actions/configure-pages@v2
uses: actions/configure-pages@v5
```

Notice this action sets an `id:`; this will allow you to use the outputs from
Expand All @@ -434,7 +434,7 @@ this action later; specifically, may want to use

```yaml
- name: Upload artifact
uses: actions/upload-pages-artifact@v1
uses: actions/upload-pages-artifact@v3
```

This actions defaults to uploading `_site`, but you can give any `with: path:`
Expand All @@ -454,7 +454,7 @@ deploy:
steps:
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v1
uses: actions/deploy-pages@v4
```

The deploy-pages job gives a `page_url`, which is the same as `base_url` on the
Expand Down
6 changes: 3 additions & 3 deletions content/week04/docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -298,13 +298,13 @@ jobs:
steps:
- name: Setup Pages
id: pages
uses: actions/configure-pages@v2
uses: actions/configure-pages@v5

- name: build output
run: pipx run nox -s docs

- name: Upload artifact
uses: actions/upload-pages-artifact@v1
uses: actions/upload-pages-artifact@v3
with:
path: docs/build

Expand All @@ -318,5 +318,5 @@ jobs:
steps:
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v1
uses: actions/deploy-pages@v4
```
39 changes: 18 additions & 21 deletions content/week05/precommit.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Here is a minimal `.pre-commit-config.yaml` file with some handy options:
```yaml
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: "v4.4.0"
rev: "v4.6.0"
hooks:
- id: check-added-large-files
- id: check-case-conflict
Expand All @@ -65,7 +65,7 @@ branch here.

## A selection of pre-commit checks

### Black
### Black / Ruff-format

[Black](https://black.readthedocs.io/en/latest/) is a popular auto-formatter
from the Python Software Foundation. One of the main features of Black is that
Expand All @@ -91,7 +91,7 @@ Here is the snippet to add Black to your `.pre-commit-config.yml`:

```yaml
- repo: https://github.com/psf/black-pre-commit-mirror
rev: "23.9.1"
rev: "24.8.0"
hooks:
- id: black
```
Expand All @@ -104,6 +104,9 @@ a way to make the Blacked code look better by rewriting your code; factor out
long unreadable portions into a variable, avoid writing matrices as 1D lists,
etc.

Note that Ruff-format is 99.9% identical and 10-50x faster, so just use that
these days.

#### Jupyter notebook support

If you want Black for Jupyter notebooks _too_, replace `id: black` with
Expand All @@ -112,7 +115,7 @@ Jupyter outputs:

```yaml
- repo: https://github.com/kynan/nbstripout
rev: "0.6.1"
rev: "0.7.1"
hooks:
- id: nbstripout
```
Expand All @@ -126,7 +129,7 @@ The MyPy addition for pre-commit:

```yaml
- repo: https://github.com/pre-commit/mirrors-mypy
rev: "v1.5.1"
rev: "v1.11.2"
hooks:
- id: mypy
files: src
Expand Down Expand Up @@ -186,21 +189,21 @@ failures from plugins updating without updating your pre-commit hook.

```yaml
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: "v0.0.292"
rev: "v0.6.8"
hooks:
- id: ruff
args: ["--fix", "--show-fixes"]
- id: ruff-format
```

The `--fix` argument is optional, but recommended, since you can inspect and
undo changes in git.
undo changes in git. The `ruff-format` hook will run the formatter.

Ruff is configured in your `pyproject.toml`. Here's an example:

```toml
[tool.ruff]
select = [
"E", "F", "W", # flake8
[tool.ruff.lint]
extend-select = [
"B", # flake8-bugbear
"I", # isort
"ARG", # flake8-unused-arguments
Expand All @@ -224,18 +227,11 @@ select = [
"NPY", # NumPy specific rules
"PD", # pandas-vet
]
extend-ignore = [
ignore = [
"PLR", # Design related pylint codes
"E501", # Line too long
"PT004", # Use underscore for non-returning fixture (use usefixture instead)
"ISC001", # Conflicts with formatter
]
typing-modules = ["mypackage._compat.typing"]
src = ["src"]
unfixable = [
"T20", # Removes print statements
"F841", # Removes unused variables
]
exclude = []
flake8-unused-arguments.ignore-variadic-names = true
isort.required-imports = ["from __future__ import annotations"]
Expand Down Expand Up @@ -265,7 +261,8 @@ without this).
Here are some good error codes to enable on most (but not all!) projects:

- `E`, `F`, `W`: These are the standard flake8 checks, classic checks that have
stood the test of time.
stood the test of time. Most of these are enabled by default, except for a few
that clash with formatting.
- `B`: This finds patterns that are very bug-prone.
- `I`: This sorts your includes. There are multiple benefits, such as smaller
diffs, fewer conflicts, a way to auto-inject `__future__` imports, and easier
Expand Down Expand Up @@ -306,7 +303,7 @@ spell checkers, this has a list of mistakes it looks for, rather than a list of

```yaml
- repo: https://github.com/codespell-project/codespell
rev: "v2.2.5"
rev: "v2.3.0"
hooks:
- id: codespell
args: ["-L", "sur,nd"]
Expand Down
27 changes: 27 additions & 0 deletions content/week05/using_packages.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,33 @@ specify extras, etc:
pipx run --spec cibuildwheel==2.9.0 cibuildwheel --platform linux
```

#### Self-contained scripts

You can now make a self-contained script; that is, one that describes it's own
requirements. You could make a `print_blue.py` file that looks like this:

```python
# /// script
# dependencies = ["rich"]
# requires-python = ">=3.11"
# ///

import rich

rich.print("[blue]This worked!")
```

Then run it with almost any tool that understands this:

```bash
pipx run ./print_blue.py
uv run ./print_blue.py
hatch run ./print_blue.py
```

These will make an environment with the specifications you give and run it for
you.

### Environment tools

There are other tools we are about to talk about, like `virtualenv`, `poetry`,
Expand Down
107 changes: 107 additions & 0 deletions noxfile.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import os
import functools
import re
import nox
import urllib.request
import json
from pathlib import Path
from typing import Any

DIR = Path(__file__).parent.resolve()

Expand Down Expand Up @@ -54,3 +60,104 @@ def book(session: nox.Session) -> None:
env=env,
)
session.run("jupyter-book", "build", ".", env=env)


PC_VERS = re.compile(
r"""\
^( *)- repo: (.*?)
* rev: (.*?)$""",
re.MULTILINE,
)

PC_REPL_LINE = """\
{2}- repo: {0}
{2} rev: {3}{1}{3}"""


GHA_VERS = re.compile(r"[\s\-]+uses: (.*?)@([^\s]+)")


@nox.session(reuse_venv=True, tags=["bump"])
def pc_bump(session: nox.Session) -> None:
"""
Bump the pre-commit versions.
"""
session.install("lastversion>=3.4")
versions = {}
pages = Path("content").glob("**/*.md")

for page in pages:
txt = page.read_text()
old_versions = {m[2]: (m[3].strip('"'), m[1]) for m in PC_VERS.finditer(txt)}

for proj, (old_version, space) in old_versions.items():
if proj not in versions:
versions[proj] = session.run(
"lastversion",
"--at=github",
"--format=tag",
"--exclude=~alpha|beta|rc",
proj,
silent=True,
).strip()
new_version = versions[proj]

after = PC_REPL_LINE.format(proj, new_version, space, '"')

session.log(f"Bump {proj}: {old_version} -> {new_version} ({page})")
txt = txt.replace(PC_REPL_LINE.format(proj, old_version, space, '"'), after)
txt = txt.replace(PC_REPL_LINE.format(proj, old_version, space, ""), after)

page.write_text(txt)


@functools.lru_cache(maxsize=None) # noqa: UP033
def get_latest_version_tag(repo: str, old_version: str) -> dict[str, Any] | None:
auth = os.environ.get("GITHUB_TOKEN", os.environ.get("GITHUB_API_TOKEN", ""))
request = urllib.request.Request(
f"https://api.github.com/repos/{repo}/tags?per_page=100"
)
request.add_header("Accept", "application/vnd.github+json")
request.add_header("X-GitHub-Api-Version", "2022-11-28")
if auth:
request.add_header("Authorization", f"Bearer: {auth}")
response = urllib.request.urlopen(request)
results = json.loads(response.read())
if not results:
msg = f"No results for {repo}"
raise RuntimeError(msg)
tags = [
x["name"]
for x in results
if x["name"].count(".") == old_version.count(".")
and x["name"].startswith("v") == old_version.startswith("v")
]
if tags:
return tags[0]
return None


@nox.session(venv_backend="none", tags=["bump"])
def gha_bump(session: nox.Session) -> None:
"""
Bump the GitHub Actions.
"""
pages = list(Path("content").glob("**/*.md"))
full_txt = "\n".join(page.read_text() for page in pages)

# This assumes there is a single version per action
old_versions = {m[1]: m[2] for m in GHA_VERS.finditer(full_txt)}

for repo, old_version in old_versions.items():
session.log(f"{repo}: {old_version}")
new_version = get_latest_version_tag(repo, old_version)
if not new_version:
continue
if new_version != old_version:
session.log(f"Convert {repo}: {old_version} -> {new_version}")
for page in pages:
txt = page.read_text()
txt = txt.replace(
f"uses: {repo}@{old_version}", f"uses: {repo}@{new_version}"
)
page.write_text(txt)

0 comments on commit f2fc2d5

Please sign in to comment.