Skip to content

Commit 1322333

Browse files
hjmjohnsonwyli
andauthored
Simplify code formatting tests (Project-MONAI#431)
* ENH: Update to new flake8 conformance tool Allow for flake8 with newer pycodestyle and pyflakes versions Forcing installation of flake8 3.7.9 was incompatiable with newer pycodestyle and pyflakes versions ERROR: flake8 3.7.9 has requirement pycodestyle<2.6.0,>=2.5.0, but you'll have pycodestyle 2.6.0 which is incompatible. ERROR: flake8 3.7.9 has requirement pyflakes<2.2.0,>=2.1.0, but you'll have pyflakes 2.2.0 which is incompatible. * ENH: Add code format checking to test script Use the test script to also perform code format testing that must pass for CI to pass. * Define common requirements-dev.txt for dev packages Use a commond defintion file to define the devlopment package dependancies. * Update pythonapp.yml Co-authored-by: Wenqi Li <wenqi.li@ucl.ac.uk>
1 parent ab0e671 commit 1322333

File tree

7 files changed

+81
-13
lines changed

7 files changed

+81
-13
lines changed

.github/workflows/pythonapp.yml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@ jobs:
1919
- name: Install dependencies
2020
run: |
2121
python -m pip install --upgrade pip wheel
22-
pip install -r requirements.txt
22+
pip install -r requirements-dev.txt
2323
- name: Lint with flake8
2424
run: |
25-
pip install flake8==3.7.9 flake8-mypy flake8-bugbear flake8-comprehensions flake8-executable flake8-pyi mccabe pep8-naming
25+
python -m pip install --upgrade pip wheel
26+
pip install -r requirements-dev.txt
2627
flake8 --version
2728
python -c 'import monai; monai.config.print_config()'
2829
MYPYPATH=$(pwd)/monai flake8 $(pwd) --count --statistics
@@ -51,7 +52,7 @@ jobs:
5152
- name: Install the dependencies
5253
run: |
5354
python -m pip install torch==1.4
54-
python -m pip install -r requirements.txt
55+
python -m pip install -r requirements-dev.txt
5556
python -m pip list
5657
- name: Run quick tests (CPU ${{ runner.os }})
5758
run: |
@@ -73,7 +74,7 @@ jobs:
7374
python -m pip install --upgrade pip wheel
7475
python -m pip uninstall -y torch torchvision
7576
python -m pip install torch==1.4
76-
python -m pip install -r requirements.txt
77+
python -m pip install -r requirements-dev.txt
7778
- name: Run quick tests (GPU)
7879
run: |
7980
python -m pip list

.github/workflows/setupapp.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jobs:
2020
python -m pip install --upgrade pip wheel
2121
python -m pip uninstall -y torch torchvision
2222
python -m pip install torch==1.4
23-
python -m pip install -r requirements.txt
23+
python -m pip install -r requirements-dev.txt
2424
python -m pip list
2525
- name: Run unit tests report coverage
2626
run: |

CONTRIBUTING.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,17 @@ To run a particular test, for example `tests/test_dice_loss.py`:
126126
python -m tests.test_dice_loss
127127
```
128128

129+
## Linting and code style testing
130+
131+
```bash
132+
# Install the various static analysis tools for development
133+
pip install -r requirements-dev.txt
134+
```
135+
129136
Before submitting a pull request, we recommend that all linting and unit tests
130-
should pass, by running the following commands locally:
137+
should pass, by running the following command locally:
131138
```
132-
pip install flake8==3.7.9 flake8-mypy flake8-bugbear flake8-comprehensions flake8-executable flake8-pyi mccabe pycodestyle pyflakes
133-
flake8 . --count --statistics
134-
./runtests.sh --coverage
139+
./runtests.sh --codeformat --coverage
135140
```
136141

137142
_If it's not tested, it's broken_

monai/losses/focal_loss.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def forward(self, input, target):
6464

6565
if target.shape[1] != 1:
6666
raise ValueError(
67-
f"target must have one channel, and should be a class index in the range [0, C-1] "
67+
"target must have one channel, and should be a class index in the range [0, C-1] "
6868
+ f"where C is the number of classes inferred from 'input': C={i.shape[1]}."
6969
)
7070
# Change the shape of input and target to

requirements-dev.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Requirements for testing
2+
-r requirements.txt
3+
flake8>=3.8.1
4+
flake8-mypy
5+
flake8-bugbear
6+
flake8-comprehensions
7+
flake8-executable
8+
flake8-pyi
9+
mccabe
10+
pep8-naming
11+
pycodestyle
12+
pyflakes
13+
coverage
14+
parameterized
15+
black

requirements.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,3 @@ tensorboard
66
pillow
77
scipy
88
scikit-image>=0.14.2
9-
coverage
10-
parameterized

runtests.sh

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ doQuickTests=false
1515
doNetTests=false
1616
doDryRun=false
1717
doZooTests=false
18+
doCodeFormat=false
1819

1920
# testing command to run
2021
cmd="python3"
@@ -40,13 +41,61 @@ do
4041
--zoo)
4142
doZooTests=true
4243
;;
44+
--codeformat)
45+
doCodeFormat=true
46+
47+
;;
4348
*)
44-
echo "runtests.sh [--coverage] [--quick] [--net] [--dryrun] [--zoo]"
49+
echo "runtests.sh [--codeformat] [--coverage] [--quick] [--net] [--dryrun] [--zoo]"
4550
exit 1
4651
;;
4752
esac
4853
done
4954

55+
# report on code format
56+
if [ "$doCodeFormat" = 'true' ]
57+
then
58+
# Ensure that the necessary packages for code format testing are installed
59+
if [[ ! -f "$(which flake8)" ]] || [[ ! -f "$(which black)" ]]; then
60+
pip install -r requirements-dev.txt
61+
fi
62+
63+
set +e # Disable exit on failure so that diagnostics can be given on failure
64+
echo "----------------------------------"
65+
echo "Verifying black formatting checks."
66+
black --check "$(pwd)"
67+
black_status=$?
68+
echo "----------------------------------"
69+
if [ ${black_status} -ne 0 ];
70+
then
71+
echo "----------------------------------"
72+
echo "black code formatting test failed!"
73+
echo "::: Run"
74+
echo "::: black \"$(pwd)\""
75+
echo "::: to auto fixing formatting errors"
76+
echo "----------------------------------"
77+
exit ${black_status}
78+
else
79+
echo "*** black code format tests passed. ***"
80+
fi
81+
82+
echo "-----------------------------------"
83+
echo "Verifying flake8 formatting checks."
84+
MYPYPATH="$(pwd)/monai" flake8 "$(pwd)" --count --statistics
85+
flake8_status=$?
86+
echo "-----------------------------------"
87+
if [ ${flake8_status} -ne 0 ];
88+
then
89+
echo "----------------------------------"
90+
echo "Formatting test failed!"
91+
echo "Manually review and fix listed formatting errors"
92+
exit ${flake8_status}
93+
else
94+
echo "*** flake8 code format tests passed. ***"
95+
fi
96+
set -e # Re-enable exit on failure
97+
fi
98+
5099
# When running --quick, require doCoverage as well and set QUICKTEST environmental
51100
# variable to disable slow unit tests from running.
52101
if [ "$doQuickTests" = 'true' ]

0 commit comments

Comments
 (0)