Skip to content

Commit ac5c34e

Browse files
authored
Add more CI (#18)
1 parent 29c1ab6 commit ac5c34e

File tree

3 files changed

+146
-4
lines changed

3 files changed

+146
-4
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
"""Script that attempts to invoke docstring-adder on all third-party typeshed stubs."""
2+
3+
import argparse
4+
import subprocess
5+
import tempfile
6+
from pathlib import Path
7+
8+
9+
def add_docstrings(typeshed_dir: Path, docstring_adder_dir: Path) -> None:
10+
"""Add docstrings to third-party typeshed stubs."""
11+
12+
for path in (typeshed_dir / "stubs").iterdir():
13+
with tempfile.TemporaryDirectory() as td:
14+
venv_dir = f"{td}-venv"
15+
subprocess.run(["uv", "venv", "--python", "3.13", venv_dir], check=True)
16+
17+
# seems to fail with an internal assertion when you try to import it...?
18+
if path.name == "keyboard":
19+
continue
20+
21+
try:
22+
subprocess.run(
23+
[
24+
"uvx",
25+
"--python",
26+
f"{venv_dir}/bin/python",
27+
"--from",
28+
docstring_adder_dir,
29+
"add-docstrings",
30+
"--typeshed-packages",
31+
path,
32+
],
33+
check=True,
34+
)
35+
except subprocess.CalledProcessError as e:
36+
if e.returncode == 10:
37+
print(f"\nFailed to install runtime package for `{path.name}\n`")
38+
continue
39+
else:
40+
raise
41+
42+
43+
if __name__ == "__main__":
44+
45+
def absolute_path(path: str) -> Path:
46+
"""Convert a string path to an absolute Path."""
47+
return Path(path).absolute()
48+
49+
parser = argparse.ArgumentParser(__doc__)
50+
parser.add_argument(
51+
"--typeshed-dir",
52+
type=absolute_path,
53+
help="Path to the typeshed directory.",
54+
required=True,
55+
)
56+
parser.add_argument(
57+
"--docstring-adder-dir",
58+
type=absolute_path,
59+
help="Path to docstring-adder.",
60+
required=True,
61+
)
62+
args = parser.parse_args()
63+
add_docstrings(
64+
typeshed_dir=args.typeshed_dir, docstring_adder_dir=args.docstring_adder_dir
65+
)

.github/workflows/check.yaml

Lines changed: 78 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ on:
66
- main
77
pull_request:
88
workflow_dispatch:
9+
schedule:
10+
- cron: "0 12 * * *"
911

1012
permissions:
1113
contents: read
@@ -14,14 +16,15 @@ env:
1416
FORCE_COLOR: 1 # Request colored output from CLI tools supporting it
1517
CLICOLOR_FORCE: 1 # recognized by uv
1618
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
19+
DEFAULT_PYTHON_VERSION: "3.13" # Default Python version for uvx
1720

1821
concurrency:
1922
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
2023
cancel-in-progress: true
2124

2225
jobs:
23-
check:
24-
name: Check
26+
stdlib:
27+
name: Check typeshed stdlib
2528
runs-on: ${{ matrix.platform }}
2629
strategy:
2730
fail-fast: false
@@ -39,7 +42,7 @@ jobs:
3942
- name: Check a basic run doesn't fail
4043
run: uvx --python=${{ matrix.python-version }} --force-reinstall --from=./docstring-adder add-docstrings --stdlib-path ./typeshed/stdlib
4144
- name: Check typeshed's mypy_test.py passes
42-
run: uv run --directory=typeshed --no-project --python=3.13 --with-requirements=requirements-tests.txt python tests/mypy_test.py stdlib -p "${{ matrix.python-version }}"
45+
run: uv run --directory=typeshed --no-project --python="${DEFAULT_PYTHON_VERSION}" --with-requirements=requirements-tests.txt python tests/mypy_test.py stdlib -p "${{ matrix.python-version }}"
4346
- name: Check running it again doesn't produce any changes
4447
# Some weird thing with distutils means that it does actually add docstrings
4548
# when run the second time that it didn't add when run the first time.
@@ -51,10 +54,57 @@ jobs:
5154
cd typeshed
5255
git config --global user.email "you@example.com"
5356
git config --global user.name "Your Name"
57+
test -n "$(git status --porcelain)"
5458
git commit -am "."
5559
uvx --python=${{ matrix.python-version }} --force-reinstall --from=../docstring-adder add-docstrings --stdlib-path ./stdlib
5660
test -z "$(git status --porcelain)"
5761
62+
typeshed-third-party:
63+
name: Check typeshed third-party stubs
64+
runs-on: ubuntu-latest
65+
# Too slow to run on every PR
66+
if: ${{ github.event_name != 'pull_request' }}
67+
steps:
68+
- uses: actions/checkout@v4
69+
with:
70+
path: docstring-adder
71+
persist-credentials: false
72+
- name: Checkout typeshed
73+
run: git clone --depth=1 https://github.com/python/typeshed.git || git clone --depth=1 https://github.com/python/typeshed.git
74+
- uses: astral-sh/setup-uv@v6
75+
- name: Check a basic run doesn't fail
76+
run: uvx --python="${DEFAULT_PYTHON_VERSION}" python ./docstring-adder/.github/scripts/typeshed_third_party.py --typeshed-dir ./typeshed --docstring-adder-dir ./docstring-adder
77+
- name: Check typeshed's mypy_test.py passes
78+
run: uv run --directory=typeshed --no-project --python="${DEFAULT_PYTHON_VERSION}" --with-requirements=requirements-tests.txt python tests/mypy_test.py stubs -p "${DEFAULT_PYTHON_VERSION}"
79+
# Ideally we'd now check that running it again doesn't produce any changes, but it fails for some third-party packages.
80+
# I believe this is because some badly-behaved packages make persistent changes to the Python environment when they're imported.
81+
# Stubs packages for which this fails are: `Authlib`, `aws-xray-sdk`, `beautifulsoup4`, `html5lib`, `python-jose`, `qrcode`
82+
83+
# A fairly arbitrary check, but it's good to enforce that it works on a non-typeshed stubs package.
84+
pandas-stubs:
85+
name: Check running on pandas-stubs
86+
runs-on: ubuntu-latest
87+
steps:
88+
- uses: actions/checkout@v4
89+
with:
90+
path: docstring-adder
91+
persist-credentials: false
92+
- name: Checkout typeshed
93+
run: git clone --depth=1 https://github.com/pandas-dev/pandas-stubs.git || git clone --depth=1 https://github.com/pandas-dev/pandas-stubs.git
94+
- uses: astral-sh/setup-uv@v6
95+
- name: Check a basic run doesn't fail
96+
run: uvx --python="${DEFAULT_PYTHON_VERSION}" --force-reinstall --with=pandas --from=./docstring-adder add-docstrings --packages ./pandas-stubs
97+
- name: Check running it again doesn't produce any changes
98+
shell: bash
99+
run: |
100+
git config --global user.email "you@example.com"
101+
git config --global user.name "Your Name"
102+
cd pandas-stubs
103+
test -n "$(git status --porcelain)"
104+
git commit -am "."
105+
uvx --python="${DEFAULT_PYTHON_VERSION}" --force-reinstall --with=pandas --from=../docstring-adder add-docstrings --packages .
106+
test -z "$(git status --porcelain)"
107+
58108
mypy:
59109
name: Run mypy
60110
runs-on: ubuntu-latest
@@ -73,3 +123,28 @@ jobs:
73123
persist-credentials: false
74124
- uses: actions/setup-python@v5
75125
- uses: pre-commit/action@v3.0.1
126+
127+
create-issue-on-failure:
128+
name: Create an issue if the daily test run failed
129+
runs-on: ubuntu-latest
130+
needs:
131+
- stdlib
132+
- typeshed-third-party
133+
- mypy
134+
- pre-commit
135+
- pandas-stubs
136+
if: ${{ github.repository == 'astral-sh/docstring-adder' && always() && github.event_name == 'schedule' && ((needs.stdlib.result == 'failure') || (needs.typeshed-third-party.result == 'failure') || (needs.mypy.result == 'failure') || (needs.pre-commit.result == 'failure') || (needs.pandas-stubs.result == 'failure')) }}
137+
permissions:
138+
issues: write
139+
steps:
140+
- uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
141+
with:
142+
github-token: ${{ secrets.GITHUB_TOKEN }}
143+
script: |
144+
await github.rest.issues.create({
145+
owner: "astral-sh",
146+
repo: "docstring-adder",
147+
title: `Daily test run failed on ${new Date().toDateString()}`,
148+
body: "Run listed here: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}",
149+
labels: ["bug"],
150+
})

add_docstrings.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
Some miscellaneous details:
3333
- The tool should be idempotent. It should add docstrings where possible, but it should
3434
never remove or alter docstrings that were already present in the stub file.
35+
Idempotency cannot be guaranteed, however, if importing the package at runtime causes
36+
persistent changes to be made to your Python environment.
3537
- Because it is a libcst-based codemod, it should not make spurious changes to formatting
3638
or comments in the stub file. `type: ignore` comments should be preserved; mypy and
3739
other type checkers should still be able to type-check the stub file after
@@ -910,7 +912,7 @@ def _main() -> None:
910912
add_docstrings_to_stub(module, context, stdlib_blacklist)
911913
elif any(path.is_relative_to(p) for p in package_paths):
912914
add_docstrings_to_stub(module, context, combined_blacklist)
913-
m = "\n--- Successfully codemodded typeshed ---"
915+
m = "\n--- Successfully completed the codemod ---"
914916
print(colored(m, "green"))
915917
sys.exit(0)
916918

0 commit comments

Comments
 (0)