|
| 1 | +# Linter |
| 2 | +A code linter is there to highlight issues with the code that do not conform with a style guide, and that may lead to errors when interpreted or compiled. For python, a code linter makes sure that the code conforms with PEP 8 standards. |
| 3 | + |
| 4 | + |
| 5 | +## Flake8 |
| 6 | +We will be using [flake8](https://flake8.pycqa.org/en/latest/), a popular linter for Python code. |
| 7 | + |
| 8 | +First, you need to install flake8. You can do so by typing |
| 9 | +``` |
| 10 | +pip install flake8 |
| 11 | +``` |
| 12 | +in your terminal. All of this will be demonstrated in the course. You can then invoke the linter using |
| 13 | +``` |
| 14 | +flake8 path/to/code/to/check.py |
| 15 | +# or |
| 16 | +flake8 path/to/code/ |
| 17 | +``` |
| 18 | +in your terminal. |
| 19 | + |
| 20 | +## Flake8 messages |
| 21 | +Flake8 will return error messages if your code does not adhere to the styling recommendation. You can find a list of all error codes [here](https://flake8.pycqa.org/en/latest/user/error-codes.html). |
| 22 | + |
| 23 | +If you run flake8 on `example1.py`, you will get a list of error messages such as |
| 24 | +``` |
| 25 | +Material_Part2_Linter/example1.py:6:1: E303 too many blank lines (3) |
| 26 | +Material_Part2_Linter/example1.py:7:1: E302 expected 2 blank lines, found 3 |
| 27 | +Material_Part2_Linter/example1.py:7:65: E251 unexpected spaces around keyword / parameter equals |
| 28 | +Material_Part2_Linter/example1.py:7:67: E251 unexpected spaces around keyword / parameter equals |
| 29 | +Material_Part2_Linter/example1.py:11:80: E501 line too long (166 > 79 characters) |
| 30 | +Material_Part2_Linter/example1.py:27:11: E225 missing whitespace around operator |
| 31 | +Material_Part2_Linter/example1.py:32:18: W291 trailing whitespace |
| 32 | +Material_Part2_Linter/example1.py:34:1: E305 expected 2 blank lines after class or function definition, found 1 |
| 33 | +Material_Part2_Linter/example1.py:34:12: E225 missing whitespace around operator |
| 34 | +Material_Part2_Linter/example1.py:36:41: W292 no newline at end of file |
| 35 | +``` |
| 36 | +Flake8 tells you which line the error occurs (for example, line 6) and at which position in that line (for example, position 1), and then report the error code (E303) and the error type (too many blank lines - in the example it also tells that it found (3) blank lines). |
| 37 | + |
| 38 | +Armed with this information, you can then navigate to the position in your code and correct the issues. |
| 39 | + |
| 40 | +You can also select to report specific errors only: |
| 41 | +``` |
| 42 | +flake8 --select E305 Material_Part2_Linter/example1.py |
| 43 | +``` |
| 44 | +- this will return |
| 45 | +``` |
| 46 | +Material_Part2_Linter/example1.py:32:1: E305 expected 2 blank lines after class or function definition, found 1 |
| 47 | +``` |
| 48 | +Or you can ask flake8 to ignore specific errors: |
| 49 | +``` |
| 50 | +flake8 --ignore E305 Material_Part2_Linter/example1.py |
| 51 | +``` |
| 52 | +This will report all errors other than E305. More than one error code is ignored using |
| 53 | +``` |
| 54 | +flake8 --ignore E303,E305 Material_Part2_Linter/example1.py |
| 55 | +``` |
| 56 | +Try out all the commands and familiarize yourself with the output. |
| 57 | + |
| 58 | +## Configuring flake8 |
| 59 | +In some cases, you may want to ignore flake8 errors or set limits differently, for example if you want to increase your line length to 120. You can reconfiger flake8 in a [number of different ways](https://flake8.pycqa.org/en/latest/user/configuration.html), and we will be using a flake8 configuration file. |
| 60 | + |
| 61 | +In order to do so, place a `.flake8` file in the root of your folder. The top of the file contains the line `[flake8]` - see [the example](../.flake8). A list of ignored errors can be included following the `ignore` option. |
| 62 | + |
| 63 | +If you want to ignore additional errors to the ones specified in your flake8 configuration file, use `extend-ignore` (instead of `ignore`) when invoking flake8 (just using `ignore` will overwrite the specifications in your .flake8 configuration). |
| 64 | +``` |
| 65 | +flake8 --extend-ignore E305 Material_Part2_Linter/example1.py |
| 66 | +``` |
| 67 | + |
| 68 | +A good `.flake8` file will look like this |
| 69 | +``` |
| 70 | +[flake8] |
| 71 | +extend-ignore = E203 |
| 72 | +exclude = |
| 73 | + .git, |
| 74 | + __pycache__, |
| 75 | + docs/source/conf.py, |
| 76 | + old, |
| 77 | + build, |
| 78 | + dist |
| 79 | +max-complexity = 10 |
| 80 | +``` |
| 81 | +using `extend-ignore` not to overwrite previously defined options, and excluding directories for the linter to parse - so you can invoke flake8 on your complete package / source folder instead of selecting each file manually: |
| 82 | +``` |
| 83 | +flake8 Material_Part1_PEP |
| 84 | +``` |
| 85 | +**Task: Run the linter on your previously formatted code to see if you have missed any deviations in code style or obvious bugs.** |
| 86 | + |
| 87 | +## Excurse: Flake8 for notebooks |
| 88 | + |
| 89 | + |
| 90 | + |
0 commit comments