Skip to content

Commit ce941cb

Browse files
committed
Add example of bad code and checking it with ruff
1 parent 6277713 commit ce941cb

File tree

1 file changed

+65
-1
lines changed

1 file changed

+65
-1
lines changed

episodes/linting.md

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,68 @@ A code linter tool is a static code analysis program that automatically checks s
1010

1111
## Linting examples
1212

13-
here
13+
Here is some poorly written Python code:
14+
15+
```Python
16+
# bad_code.py
17+
18+
import os, sys # multiple imports on one line
19+
20+
def add(x,y): # missing whitespace after comma
21+
return x+ y # inconsistent spacing
22+
23+
def unused_function():
24+
pass # unused function
25+
26+
value = 42
27+
print("The answer is: {}". format(value)) # space before format call
28+
```
29+
30+
Next, run Ruff to check the code:
31+
32+
```bash
33+
$ ruff check bad_code.py
34+
```
35+
36+
Ruff will output the following errors and warnings about the code in the file:
37+
38+
```output
39+
E401 [*] Multiple imports on one line
40+
--> bad_code.py:3:1
41+
|
42+
1 | # bad_code.py
43+
2 |
44+
3 | import os, sys # multiple imports on one line
45+
| ^^^^^^^^^^^^^^
46+
4 |
47+
5 | def add(x,y): # missing whitespace after comma
48+
|
49+
help: Split imports
50+
51+
F401 [*] `os` imported but unused
52+
--> bad_code.py:3:8
53+
|
54+
1 | # bad_code.py
55+
2 |
56+
3 | import os, sys # multiple imports on one line
57+
| ^^
58+
4 |
59+
5 | def add(x,y): # missing whitespace after comma
60+
|
61+
help: Remove unused import
62+
63+
F401 [*] `sys` imported but unused
64+
--> bad_code.py:3:12
65+
|
66+
1 | # bad_code.py
67+
2 |
68+
3 | import os, sys # multiple imports on one line
69+
| ^^^
70+
4 |
71+
5 | def add(x,y): # missing whitespace after comma
72+
|
73+
help: Remove unused import
74+
75+
Found 3 errors.
76+
[*] 3 fixable with the `--fix` option.
77+
```

0 commit comments

Comments
 (0)