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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jupyter notebook support #44

Merged
merged 6 commits into from Feb 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 20 additions & 0 deletions .github/workflows/test.yml
Expand Up @@ -25,6 +25,26 @@ jobs:
echo "Changes detected!"
exit 1
fi
- name: Check black would format the expected files
run: |
expected_files=(num_guess.py num_guess.ipynb subfolder/queen_problem.py subfolder/queen_problem.ipynb)
checked_files=(${{ env.BLACK_CHECK_FILE_PATHS }})
# Check whether the expected files are checked
for file in ${expected_files[@]}; do
if ! [[ " ${checked_files[*]} " == *"${file}"* ]]; then
echo "Black forgot to check file ${file}!"
exit 1
fi
done

# Check whether black checks files that should not be checked
for file in ${checked_files[@]}; do
if ! [[ " ${expected_files[*]} " == *"${file}"* ]]; then
echo "Unexpected file ${file} got checked!"
exit 1
fi
done
echo "Black checked the expected files: [${expected_files[@]}] successfully!"

test-pr-check:
if: github.event_name == 'pull_request'
Expand Down
30 changes: 25 additions & 5 deletions entrypoint.sh
Expand Up @@ -17,18 +17,20 @@ wget -O - -q https://raw.githubusercontent.com/reviewdog/reviewdog/master/instal

if [[ "$(which black)" == "" ]]; then
echo "[action-black] Installing black package..."
python -m pip install --upgrade black
python -m pip install --upgrade black[jupyter]
fi

# Run black with reviewdog
black_exit_val="0"
reviewdog_exit_val="0"
if [[ "${INPUT_REPORTER}" = 'github-pr-review' ]]; then
echo "[action-black] Checking python code with the black formatter and reviewdog..."
# shellcheck disable=SC2086
black_check_output="$(black --diff --quiet --check . ${INPUT_BLACK_ARGS})" ||
black_exit_val="$?"

# Intput black formatter output to reviewdog
# Input black formatter output to reviewdog
# shellcheck disable=SC2086
echo "${black_check_output}" | /tmp/reviewdog -f="diff" \
-f.diff.strip=0 \
-name="${INPUT_TOOL_NAME}" \
Expand All @@ -40,10 +42,12 @@ if [[ "${INPUT_REPORTER}" = 'github-pr-review' ]]; then
else

echo "[action-black] Checking python code with the black formatter and reviewdog..."
# shellcheck disable=SC2086
black_check_output="$(black --check . ${INPUT_BLACK_ARGS} 2>&1)" ||
black_exit_val="$?"

# Intput black formatter output to reviewdog
# Input black formatter output to reviewdog
# shellcheck disable=SC2086
echo "${black_check_output}" | /tmp/reviewdog -f="black" \
-name="${INPUT_TOOL_NAME}" \
-reporter="${INPUT_REPORTER}" \
Expand All @@ -53,8 +57,24 @@ else
${INPUT_REVIEWDOG_FLAGS} || reviewdog_exit_val="$?"
fi

# Output the checked file paths that would be formatted
black_check_file_paths=()
while read -r line; do
black_check_file_paths+=("$line")
done <<<"${black_check_output//"would reformat "/}"

# remove last two lines of black output, since they are irrelevant
unset "black_check_file_paths[-1]"
unset "black_check_file_paths[-1]"

# append the array elements to BLACK_CHECK_FILE_PATHS in github env
# shellcheck disable=SC2129
echo "BLACK_CHECK_FILE_PATHS<<EOF" >>"$GITHUB_ENV"
echo "${black_check_file_paths[@]}" >>"$GITHUB_ENV"
echo "EOF" >>"$GITHUB_ENV"

# Throw error if an error occurred and fail_on_error is true
if [[ "${INPUT_FAIL_ON_ERROR}" = 'true' && ("${black_exit_val}" -ne '0' || \
if [[ "${INPUT_FAIL_ON_ERROR}" = 'true' && ("${black_exit_val}" -ne '0' ||
"${reviewdog_exit_val}" -eq "1") ]]; then
if [[ "${black_exit_val}" -eq "123" ]]; then
# NOTE: Done since syntax errors are already handled by reviewdog (see
Expand All @@ -70,4 +90,4 @@ if [[ "${INPUT_FAIL_ON_ERROR}" = 'true' && ("${black_exit_val}" -ne '0' || \
fi

echo "[action-black] Clean up reviewdog..."
rm /tmp/reviewdog
rm /tmp/reviewdog
22 changes: 22 additions & 0 deletions testdata/hello.ipynb
@@ -0,0 +1,22 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# This file doesn't need formatting, so Black should skip it\n",
"print(\"hello world\")"
]
}
],
"metadata": {
"language_info": {
"name": "python"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}
2 changes: 2 additions & 0 deletions testdata/hello.py
@@ -0,0 +1,2 @@
# This file doesn't need formatting, so Black should skip it
print("hello world")
70 changes: 70 additions & 0 deletions testdata/num_guess.ipynb
@@ -0,0 +1,70 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"\"\"\"Small test script taken from https://wiki.python.org/moin/SimplePrograms\"\"\"\n",
"\n",
"import random\n",
"import sys # F401 'os' imported but unused\n",
"import os # F401 'os' imported but unused\n",
"\n",
"### E265 block comment should start with '# '\n",
"print(\"Hello from reviewdog!\")\n",
"print(\"Let's play a small number guessing game to test the flake8 github action.\")\n",
"print(\"This game is taken from https://wiki.python.org/moin/SimplePrograms.\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"guesses_made = 0\n",
"\n",
"name = input(\"Hello! What is your name?\\n\")\n",
"\n",
"number = random.randint(1, 20)\n",
"print(\"Well, {0}, I am thinking of a number between 1 and 20.\".format(name)) # E501 line too long (80 > 79 characters)\n",
"\n",
"while guesses_made < 6:\n",
"\n",
" guess = int(input(\"Take a guess: \"))\n",
"\n",
" guesses_made += 1\n",
"\n",
" if guess < number:\n",
" print(\"Your guess is too low.\")\n",
"\n",
" if guess > number:\n",
" print(\"Your guess is too high.\")\n",
"\n",
" if guess == number:\n",
" break\n",
"\n",
"if guess == number:\n",
" print(\n",
" \"Good job, {0}! You guessed my number in {1} guesses!\".format(\n",
" name, guesses_made\n",
" )\n",
" )\n",
"else:\n",
" print(\"Nope. The number I was thinking of was {0}\".format(number))\n",
"\n",
"import itertools # E402 module level import not at top of file"
]
}
],
"metadata": {
"language_info": {
"name": "python"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}
67 changes: 67 additions & 0 deletions testdata/subfolder/queen_problem.ipynb
@@ -0,0 +1,67 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"\"\"\"Small test script taken from https://wiki.python.org/moin/SimplePrograms\"\"\"\n",
"\n",
"import pwd # F401 'os' imported but unused\n",
"import grp # F401 'os' imported but unused\n",
"\n",
"BOARD_SIZE = 8\n",
"\n",
"### E265 block comment should start with '# '\n",
"print(\"Hello from reviewdog!\")\n",
"print(\"Let's play a small queen problem game to test the flake8 github action.\")\n",
"print(\"This game is taken from https://wiki.python.org/moin/SimplePrograms.\")\n",
"\n",
"class BailOut(Exception):\n",
" pass\n",
"\n",
"def validate(queens):\n",
" left = right = col = queens[-1] # E501 line too long (80 > 79 characters). Long description text\n",
" for r in reversed(queens[:-1]):\n",
" left, right = left-1, right+1\n",
" if r in (left, col, right):\n",
" raise BailOut\n",
"\n",
"def add_queen(queens):\n",
" for i in range(BOARD_SIZE):\n",
" test_queens = queens + [i]\n",
" try:\n",
" validate(test_queens)\n",
" if len(test_queens) == BOARD_SIZE:\n",
" return test_queens\n",
" else:\n",
" return add_queen(test_queens)\n",
" except BailOut:\n",
" pass\n",
" raise BailOut"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"queens = add_queen([])\n",
"print (queens)\n",
"print (\"\\n\".join(\". \"*q + \"Q \" + \". \"*(BOARD_SIZE-q-1) for q in queens))\n",
"\n",
"import dis # E402 module level import not at top of file"
]
}
],
"metadata": {
"language_info": {
"name": "python"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}