Skip to content

Commit a75c791

Browse files
authored
Update spellcheck to run only on changed files (#3233)
.rst, .md, and .py file changes are included for testing purposes only and will not be merged. Mering despite of the lint errors, could be fixed in followup PRs I guess
1 parent b76570d commit a75c791

File tree

5 files changed

+191
-16
lines changed

5 files changed

+191
-16
lines changed

.github/workflows/spelling.yml

+137-4
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,149 @@ on:
55
push:
66
branches:
77
- main
8+
89
jobs:
910
pyspelling:
1011
runs-on: ubuntu-20.04
1112
steps:
12-
- uses: actions/checkout@v3
13+
- name: Check for skip label and get changed files
14+
id: check-files
15+
uses: actions/github-script@v6
16+
with:
17+
script: |
18+
let skipCheck = false;
19+
let changedFiles = [];
20+
21+
if (context.eventName === 'pull_request') {
22+
// Check for skip label
23+
const { data: labels } = await github.rest.issues.listLabelsOnIssue({
24+
owner: context.repo.owner,
25+
repo: context.repo.repo,
26+
issue_number: context.issue.number
27+
});
28+
skipCheck = labels.some(label => label.name === 'skip-spell-check');
29+
30+
if (!skipCheck) {
31+
// Get changed files in PR
32+
const { data: files } = await github.rest.pulls.listFiles({
33+
owner: context.repo.owner,
34+
repo: context.repo.repo,
35+
pull_number: context.issue.number
36+
});
37+
38+
changedFiles = files
39+
.filter(file => file.filename.match(/\.(py|rst|md)$/))
40+
.map(file => file.filename);
41+
}
42+
} else {
43+
// For push events, we'll still need to use git diff
44+
// We'll handle this after checkout
45+
}
46+
47+
core.setOutput('skip', skipCheck.toString());
48+
core.setOutput('files', changedFiles.join('\n'));
49+
core.setOutput('is-pr', (context.eventName === 'pull_request').toString());
50+
51+
- uses: actions/checkout@v4
52+
if: steps.check-files.outputs.skip != 'true'
53+
with:
54+
fetch-depth: 0
55+
56+
- name: Get changed files for push event
57+
if: |
58+
steps.check-files.outputs.skip != 'true' &&
59+
steps.check-files.outputs.is-pr != 'true'
60+
id: push-files
61+
run: |
62+
CHANGED_FILES=$(git diff --name-only HEAD^..HEAD -- '*.py' '*.rst' '*.md')
63+
echo "files<<EOF" >> $GITHUB_OUTPUT
64+
echo "$CHANGED_FILES" >> $GITHUB_OUTPUT
65+
echo "EOF" >> $GITHUB_OUTPUT
66+
67+
- name: Check if relevant files changed
68+
if: steps.check-files.outputs.skip != 'true'
69+
id: check
70+
run: |
71+
if [ "${{ steps.check-files.outputs.is-pr }}" == "true" ]; then
72+
FILES="${{ steps.check-files.outputs.files }}"
73+
else
74+
FILES="${{ steps.push-files.outputs.files }}"
75+
fi
76+
77+
if [ -z "$FILES" ]; then
78+
echo "skip=true" >> $GITHUB_OUTPUT
79+
echo "No relevant files changed (*.py, *.rst, *.md), skipping spell check"
80+
else
81+
echo "skip=false" >> $GITHUB_OUTPUT
82+
echo "Found changed files to check:"
83+
echo "$FILES"
84+
fi
85+
1386
- uses: actions/setup-python@v4
87+
if: |
88+
steps.check-files.outputs.skip != 'true' &&
89+
steps.check.outputs.skip != 'true'
1490
with:
1591
python-version: '3.9'
1692
cache: 'pip'
17-
- run: pip install pyspelling
18-
- run: sudo apt-get install aspell aspell-en
19-
- run: pyspelling
2093

94+
- name: Install dependencies
95+
if: |
96+
steps.check-files.outputs.skip != 'true' &&
97+
steps.check.outputs.skip != 'true'
98+
run: |
99+
pip install pyspelling
100+
sudo apt-get install aspell aspell-en
101+
102+
- name: Run spell check on each file
103+
id: spellcheck
104+
if: |
105+
steps.check-files.outputs.skip != 'true' &&
106+
steps.check.outputs.skip != 'true'
107+
run: |
108+
if [ "${{ steps.check-files.outputs.is-pr }}" == "true" ]; then
109+
mapfile -t FILES <<< "${{ steps.check-files.outputs.files }}"
110+
else
111+
mapfile -t FILES <<< "${{ steps.push-files.outputs.files }}"
112+
fi
113+
114+
# Check each file individually
115+
FINAL_EXIT_CODE=0
116+
SPELLCHECK_LOG=""
117+
for file in "${FILES[@]}"; do
118+
if [ -n "$file" ]; then
119+
echo "Checking spelling in $file"
120+
python3 -c "import yaml; config = yaml.safe_load(open('.pyspelling.yml')); new_matrix = [matrix.copy() for matrix in config['matrix'] if (('python' in matrix['name'].lower() and '$file'.endswith('.py')) or ('rest' in matrix['name'].lower() and '$file'.endswith('.rst')) or ('markdown' in matrix['name'].lower() and '$file'.endswith('.md'))) and not matrix.update({'sources': ['$file']})]; config['matrix'] = new_matrix; yaml.dump(config, open('temp_config.yml', 'w'))"
121+
122+
if OUTPUT=$(pyspelling -c temp_config.yml 2>&1); then
123+
echo "No spelling errors found in $file"
124+
else
125+
FINAL_EXIT_CODE=1
126+
echo "Spelling errors found in $file:"
127+
echo "$OUTPUT"
128+
SPELLCHECK_LOG+="### $file\n$OUTPUT\n\n"
129+
fi
130+
fi
131+
done
132+
133+
# Save the results to GITHUB_OUTPUT
134+
echo "spell_failed=$FINAL_EXIT_CODE" >> $GITHUB_OUTPUT
135+
echo "spell_log<<SPELLEOF" >> $GITHUB_OUTPUT
136+
echo "$SPELLCHECK_LOG" >> $GITHUB_OUTPUT
137+
echo "SPELLEOF" >> $GITHUB_OUTPUT
138+
139+
if [ $FINAL_EXIT_CODE -ne 0 ]; then
140+
echo "Spell check failed! See above for details."
141+
echo
142+
echo "Here are a few tips:"
143+
echo "- All PyTorch API objects must be in double backticks or use an intersphinx directive."
144+
echo " Example: ``torch.nn``, :func:"
145+
echo "- Consult en-wordlist.txt for spellings of some of the words."
146+
echo " You can add a word to en-wordlist.txt if:"
147+
echo " 1) It's a common abbreviation, like RNN."
148+
echo " 2) It's a word widely accepted in the industry."
149+
echo "- Please do not add words like 'dtype', 'torch.nn.Transformer' to pass spellcheck."
150+
echo " Instead wrap it in double backticks or use an intersphinx directive."
151+
echo
152+
exit 1
153+
fi

.pyspelling.yml

+47-5
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@ spellchecker: aspell
22
matrix:
33
- name: python
44
sources:
5-
- beginner_source/*.py
6-
- intermediate_source/*.py
7-
- advanced_source/*.py
8-
- recipes_source/*/*.py
5+
- "**/*.py"
96
dictionary:
107
wordlists:
118
- en-wordlist.txt
@@ -56,7 +53,7 @@ matrix:
5653
- pyspelling.filters.url:
5754
- name: reST
5855
sources:
59-
- beginner_source/*.rst
56+
- "**/*.rst"
6057
dictionary:
6158
wordlists:
6259
- en-wordlist.txt
@@ -119,3 +116,48 @@ matrix:
119116
- open: '\.\.\s+(image|include|only)::'
120117
close: '$'
121118
- pyspelling.filters.url:
119+
- name: markdown
120+
sources:
121+
- '**/*.md'
122+
dictionary:
123+
wordlists:
124+
- en-wordlist.txt
125+
pipeline:
126+
- pyspelling.filters.markdown:
127+
markdown_extensions:
128+
- markdown.extensions.extra:
129+
- markdown.extensions.admonition:
130+
- markdown.extensions.codehilite:
131+
- markdown.extensions.meta:
132+
- markdown.extensions.tables:
133+
- markdown.extensions.toc:
134+
- pyspelling.filters.html:
135+
comments: false
136+
ignores:
137+
- code
138+
- pre
139+
- tt
140+
- img
141+
- a
142+
- table
143+
- thead
144+
- tbody
145+
- th
146+
- tr
147+
- td
148+
- pyspelling.filters.context:
149+
context_visible_first: true
150+
delimiters:
151+
# Ignore code blocks
152+
- open: '```[a-z]*\n'
153+
close: '```\n'
154+
# Ignore inline code
155+
- open: '`'
156+
close: '`'
157+
# Ignore links
158+
- open: '\[([^]]*)\]'
159+
close: '\([^)]*\)'
160+
# Ignore HTML comments
161+
- open: '<!--'
162+
close: '-->'
163+
- pyspelling.filters.url:

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ All the tutorials are now presented as sphinx style documentation at:
77

88
# Asking a question
99

10-
If you have a question about a tutorial, post in https://dev-discuss.pytorch.org/ rather than creating an issue in this repo. Your question will be answered much faster on the dev-discuss forum.
10+
If you hve a qestion about a tutorial, post in https://dev-discuss.pytorch.org/ rather than creating an issue in this repo. Your question will be answered much faster on the dev-discuss forum.
1111

1212
# Submitting an issue
1313

@@ -20,7 +20,7 @@ You can submit the following types of issues:
2020

2121
We use sphinx-gallery's [notebook styled examples](https://sphinx-gallery.github.io/stable/tutorials/index.html) to create the tutorials. Syntax is very simple. In essence, you write a slightly well formatted Python file and it shows up as an HTML page. In addition, a Jupyter notebook is autogenerated and available to run in Google Colab.
2222

23-
Here is how you can create a new tutorial (for a detailed description, see [CONTRIBUTING.md](./CONTRIBUTING.md)):
23+
Here is how you can ceate a new tutorial (for a detailed description, see [CONTRIBUTING.md](./CONTRIBUTING.md)):
2424

2525
NOTE: Before submitting a new tutorial, read [PyTorch Tutorial Submission Policy](./tutorial_submission_policy.md).
2626

beginner_source/colab.rst

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ run PyTorch tutorials in Google Colab.
1010
PyTorch Version in Google Colab
1111
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1212

13-
When you are running a tutorial that requires a version of PyTorch that has
14-
just been released, that version might not be yet available in Google Colab.
13+
Wen you are running a tutorial that requires a version of PyTorch that has
14+
jst been released, that version might not be yet available in Google Colab.
1515
To check that you have the required ``torch`` and compatible domain libraries
1616
installed, run ``!pip list``.
1717

@@ -27,7 +27,7 @@ Using Tutorial Data from Google Drive in Colab
2727
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2828

2929
We've added a new feature to tutorials that allows users to open the
30-
notebook associated with a tutorial in Google Colab. You may need to
30+
ntebook associated with a tutorial in Google Colab. You may need to
3131
copy data to your Google drive account to get the more complex tutorials
3232
to work.
3333

beginner_source/introyt/tensors_deeper_tutorial.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444

4545

4646
##########################################################################
47-
# Let’s unpack what we just did:
47+
# Let’s upack what we just did:
4848
#
4949
# - We created a tensor using one of the numerous factory methods
5050
# attached to the ``torch`` module.
@@ -85,7 +85,7 @@
8585

8686

8787
#########################################################################
88-
# The factory methods all do just what you’d expect - we have a tensor
88+
# The fctory methods all do just what you’d expect - we have a tensor
8989
# full of zeros, another full of ones, and another with random values
9090
# between 0 and 1.
9191
#

0 commit comments

Comments
 (0)