37 <tr> |
37 <tr> |
38 <td><a href="#_main">_main</a></td> |
38 <td><a href="#_main">_main</a></td> |
39 <td>Parse options and run checks on Python source.</td> |
39 <td>Parse options and run checks on Python source.</td> |
40 </tr><tr> |
40 </tr><tr> |
41 <td><a href="#blank_lines">blank_lines</a></td> |
41 <td><a href="#blank_lines">blank_lines</a></td> |
42 <td></td> |
42 <td>Separate top-level function and class definitions with two blank lines.</td> |
43 </tr><tr> |
43 </tr><tr> |
44 <td><a href="#compound_statements">compound_statements</a></td> |
44 <td><a href="#compound_statements">compound_statements</a></td> |
45 <td></td> |
45 <td>Compound statements (multiple statements on the same line) are generally discouraged.</td> |
46 </tr><tr> |
46 </tr><tr> |
47 <td><a href="#excluded">excluded</a></td> |
47 <td><a href="#excluded">excluded</a></td> |
48 <td>Check if options.exclude contains a pattern that matches filename.</td> |
48 <td>Check if options.exclude contains a pattern that matches filename.</td> |
49 </tr><tr> |
49 </tr><tr> |
50 <td><a href="#expand_indent">expand_indent</a></td> |
50 <td><a href="#expand_indent">expand_indent</a></td> |
73 </tr><tr> |
73 </tr><tr> |
74 <td><a href="#ignore_code">ignore_code</a></td> |
74 <td><a href="#ignore_code">ignore_code</a></td> |
75 <td>Check if options.ignore contains a prefix of the error code.</td> |
75 <td>Check if options.ignore contains a prefix of the error code.</td> |
76 </tr><tr> |
76 </tr><tr> |
77 <td><a href="#imports_on_separate_lines">imports_on_separate_lines</a></td> |
77 <td><a href="#imports_on_separate_lines">imports_on_separate_lines</a></td> |
78 <td></td> |
78 <td>Imports should usually be on separate lines.</td> |
79 </tr><tr> |
79 </tr><tr> |
80 <td><a href="#indentation">indentation</a></td> |
80 <td><a href="#indentation">indentation</a></td> |
81 <td></td> |
81 <td>Use 4 spaces per indentation level.</td> |
82 </tr><tr> |
82 </tr><tr> |
83 <td><a href="#input_dir">input_dir</a></td> |
83 <td><a href="#input_dir">input_dir</a></td> |
84 <td>Check all Python source files in this directory and all subdirectories.</td> |
84 <td>Check all Python source files in this directory and all subdirectories.</td> |
85 </tr><tr> |
85 </tr><tr> |
86 <td><a href="#input_file">input_file</a></td> |
86 <td><a href="#input_file">input_file</a></td> |
97 </tr><tr> |
97 </tr><tr> |
98 <td><a href="#missing_whitespace">missing_whitespace</a></td> |
98 <td><a href="#missing_whitespace">missing_whitespace</a></td> |
99 <td>JCR: Each comma, semicolon or colon should be followed by whitespace.</td> |
99 <td>JCR: Each comma, semicolon or colon should be followed by whitespace.</td> |
100 </tr><tr> |
100 </tr><tr> |
101 <td><a href="#missing_whitespace_around_operator">missing_whitespace_around_operator</a></td> |
101 <td><a href="#missing_whitespace_around_operator">missing_whitespace_around_operator</a></td> |
102 <td></td> |
102 <td>- Always surround these binary operators with a single space on either side: assignment (=), augmented assignment (+=, -= etc.), comparisons (==, <, >, !=, <>, <=, >=, in, not in, is, is not), Booleans (and, or, not).</td> |
103 </tr><tr> |
103 </tr><tr> |
104 <td><a href="#mute_string">mute_string</a></td> |
104 <td><a href="#mute_string">mute_string</a></td> |
105 <td>Replace contents with 'xxx' to prevent syntax matching.</td> |
105 <td>Replace contents with 'xxx' to prevent syntax matching.</td> |
106 </tr><tr> |
106 </tr><tr> |
107 <td><a href="#print_benchmark">print_benchmark</a></td> |
107 <td><a href="#print_benchmark">print_benchmark</a></td> |
139 </tr><tr> |
139 </tr><tr> |
140 <td><a href="#selftest">selftest</a></td> |
140 <td><a href="#selftest">selftest</a></td> |
141 <td>Test all check functions with test cases in docstrings.</td> |
141 <td>Test all check functions with test cases in docstrings.</td> |
142 </tr><tr> |
142 </tr><tr> |
143 <td><a href="#tabs_obsolete">tabs_obsolete</a></td> |
143 <td><a href="#tabs_obsolete">tabs_obsolete</a></td> |
144 <td></td> |
144 <td>For new projects, spaces-only are strongly recommended over tabs.</td> |
145 </tr><tr> |
145 </tr><tr> |
146 <td><a href="#tabs_or_spaces">tabs_or_spaces</a></td> |
146 <td><a href="#tabs_or_spaces">tabs_or_spaces</a></td> |
147 <td></td> |
147 <td>Never mix tabs and spaces.</td> |
148 </tr><tr> |
148 </tr><tr> |
149 <td><a href="#trailing_blank_lines">trailing_blank_lines</a></td> |
149 <td><a href="#trailing_blank_lines">trailing_blank_lines</a></td> |
150 <td></td> |
150 <td>JCR: Trailing blank lines are superfluous.</td> |
151 </tr><tr> |
151 </tr><tr> |
152 <td><a href="#trailing_whitespace">trailing_whitespace</a></td> |
152 <td><a href="#trailing_whitespace">trailing_whitespace</a></td> |
153 <td></td> |
153 <td>JCR: Trailing whitespace is superfluous.</td> |
154 </tr><tr> |
154 </tr><tr> |
155 <td><a href="#whitespace_around_comma">whitespace_around_comma</a></td> |
155 <td><a href="#whitespace_around_comma">whitespace_around_comma</a></td> |
156 <td>Avoid extraneous whitespace in the following situations:</td> |
156 <td>Avoid extraneous whitespace in the following situations:</td> |
157 </tr><tr> |
157 </tr><tr> |
158 <td><a href="#whitespace_around_named_parameter_equals">whitespace_around_named_parameter_equals</a></td> |
158 <td><a href="#whitespace_around_named_parameter_equals">whitespace_around_named_parameter_equals</a></td> |
275 <div align="right"><a href="#top">Up</a></div> |
275 <div align="right"><a href="#top">Up</a></div> |
276 <hr /><hr /> |
276 <hr /><hr /> |
277 <a NAME="blank_lines" ID="blank_lines"></a> |
277 <a NAME="blank_lines" ID="blank_lines"></a> |
278 <h2>blank_lines</h2> |
278 <h2>blank_lines</h2> |
279 <b>blank_lines</b>(<i>logical_line, blank_lines, indent_level, line_number, previous_logical, previous_indent_level, blank_lines_before_comment</i>) |
279 <b>blank_lines</b>(<i>logical_line, blank_lines, indent_level, line_number, previous_logical, previous_indent_level, blank_lines_before_comment</i>) |
280 |
280 <p> |
|
281 Separate top-level function and class definitions with two blank lines. |
|
282 </p><p> |
|
283 Method definitions inside a class are separated by a single blank line. |
|
284 </p><p> |
|
285 Extra blank lines may be used (sparingly) to separate groups of related |
|
286 functions. Blank lines may be omitted between a bunch of related |
|
287 one-liners (e.g. a set of dummy implementations). |
|
288 </p><p> |
|
289 Use blank lines in functions, sparingly, to indicate logical sections. |
|
290 </p><p> |
|
291 Okay: def a():\n pass\n\n\ndef b():\n pass |
|
292 Okay: def a():\n pass\n\n\n# Foo\n# Bar\n\ndef b():\n pass |
|
293 </p><p> |
|
294 E301: class Foo:\n b = 0\n def bar():\n pass |
|
295 E302: def a():\n pass\n\ndef b(n):\n pass |
|
296 E303: def a():\n pass\n\n\n\ndef b(n):\n pass |
|
297 E303: def a():\n\n\n\n pass |
|
298 E304: @decorator\n\ndef a():\n pass |
|
299 </p> |
281 <div align="right"><a href="#top">Up</a></div> |
300 <div align="right"><a href="#top">Up</a></div> |
282 <hr /><hr /> |
301 <hr /><hr /> |
283 <a NAME="compound_statements" ID="compound_statements"></a> |
302 <a NAME="compound_statements" ID="compound_statements"></a> |
284 <h2>compound_statements</h2> |
303 <h2>compound_statements</h2> |
285 <b>compound_statements</b>(<i>logical_line</i>) |
304 <b>compound_statements</b>(<i>logical_line</i>) |
286 |
305 <p> |
|
306 Compound statements (multiple statements on the same line) are |
|
307 generally discouraged. |
|
308 </p><p> |
|
309 While sometimes it's okay to put an if/for/while with a small body |
|
310 on the same line, never do this for multi-clause statements. Also |
|
311 avoid folding such long lines! |
|
312 </p><p> |
|
313 Okay: if foo == 'blah':\n do_blah_thing() |
|
314 Okay: do_one() |
|
315 Okay: do_two() |
|
316 Okay: do_three() |
|
317 </p><p> |
|
318 E701: if foo == 'blah': do_blah_thing() |
|
319 E701: for x in lst: total += x |
|
320 E701: while t < 10: t = delay() |
|
321 E701: if foo == 'blah': do_blah_thing() |
|
322 E701: else: do_non_blah_thing() |
|
323 E701: try: something() |
|
324 E701: finally: cleanup() |
|
325 E701: if foo == 'blah': one(); two(); three() |
|
326 </p><p> |
|
327 E702: do_one(); do_two(); do_three() |
|
328 </p> |
287 <div align="right"><a href="#top">Up</a></div> |
329 <div align="right"><a href="#top">Up</a></div> |
288 <hr /><hr /> |
330 <hr /><hr /> |
289 <a NAME="excluded" ID="excluded"></a> |
331 <a NAME="excluded" ID="excluded"></a> |
290 <h2>excluded</h2> |
332 <h2>excluded</h2> |
291 <b>excluded</b>(<i>filename</i>) |
333 <b>excluded</b>(<i>filename</i>) |
403 <div align="right"><a href="#top">Up</a></div> |
445 <div align="right"><a href="#top">Up</a></div> |
404 <hr /><hr /> |
446 <hr /><hr /> |
405 <a NAME="imports_on_separate_lines" ID="imports_on_separate_lines"></a> |
447 <a NAME="imports_on_separate_lines" ID="imports_on_separate_lines"></a> |
406 <h2>imports_on_separate_lines</h2> |
448 <h2>imports_on_separate_lines</h2> |
407 <b>imports_on_separate_lines</b>(<i>logical_line</i>) |
449 <b>imports_on_separate_lines</b>(<i>logical_line</i>) |
408 |
450 <p> |
|
451 Imports should usually be on separate lines. |
|
452 </p><p> |
|
453 Okay: import os\nimport sys |
|
454 E401: import sys, os |
|
455 </p><p> |
|
456 Okay: from subprocess import Popen, PIPE |
|
457 Okay: from myclas import MyClass |
|
458 Okay: from foo.bar.yourclass import YourClass |
|
459 Okay: import myclass |
|
460 Okay: import foo.bar.yourclass |
|
461 </p> |
409 <div align="right"><a href="#top">Up</a></div> |
462 <div align="right"><a href="#top">Up</a></div> |
410 <hr /><hr /> |
463 <hr /><hr /> |
411 <a NAME="indentation" ID="indentation"></a> |
464 <a NAME="indentation" ID="indentation"></a> |
412 <h2>indentation</h2> |
465 <h2>indentation</h2> |
413 <b>indentation</b>(<i>logical_line, previous_logical, indent_char, indent_level, previous_indent_level</i>) |
466 <b>indentation</b>(<i>logical_line, previous_logical, indent_char, indent_level, previous_indent_level</i>) |
414 |
467 <p> |
|
468 Use 4 spaces per indentation level. |
|
469 </p><p> |
|
470 For really old code that you don't want to mess up, you can continue to |
|
471 use 8-space tabs. |
|
472 </p><p> |
|
473 Okay: a = 1 |
|
474 Okay: if a == 0:\n a = 1 |
|
475 E111: a = 1 |
|
476 </p><p> |
|
477 Okay: for item in items:\n pass |
|
478 E112: for item in items:\npass |
|
479 </p><p> |
|
480 Okay: a = 1\nb = 2 |
|
481 E113: a = 1\n b = 2 |
|
482 </p> |
415 <div align="right"><a href="#top">Up</a></div> |
483 <div align="right"><a href="#top">Up</a></div> |
416 <hr /><hr /> |
484 <hr /><hr /> |
417 <a NAME="input_dir" ID="input_dir"></a> |
485 <a NAME="input_dir" ID="input_dir"></a> |
418 <h2>input_dir</h2> |
486 <h2>input_dir</h2> |
419 <b>input_dir</b>(<i>dirname, runner=None</i>) |
487 <b>input_dir</b>(<i>dirname, runner=None</i>) |
479 <div align="right"><a href="#top">Up</a></div> |
547 <div align="right"><a href="#top">Up</a></div> |
480 <hr /><hr /> |
548 <hr /><hr /> |
481 <a NAME="missing_whitespace_around_operator" ID="missing_whitespace_around_operator"></a> |
549 <a NAME="missing_whitespace_around_operator" ID="missing_whitespace_around_operator"></a> |
482 <h2>missing_whitespace_around_operator</h2> |
550 <h2>missing_whitespace_around_operator</h2> |
483 <b>missing_whitespace_around_operator</b>(<i>logical_line, tokens</i>) |
551 <b>missing_whitespace_around_operator</b>(<i>logical_line, tokens</i>) |
484 |
552 <p> |
|
553 - Always surround these binary operators with a single space on |
|
554 either side: assignment (=), augmented assignment (+=, -= etc.), |
|
555 comparisons (==, <, >, !=, <>, <=, >=, in, not in, is, is not), |
|
556 Booleans (and, or, not). |
|
557 </p><p> |
|
558 - Use spaces around arithmetic operators. |
|
559 </p><p> |
|
560 Okay: i = i + 1 |
|
561 Okay: submitted += 1 |
|
562 Okay: x = x * 2 - 1 |
|
563 Okay: hypot2 = x * x + y * y |
|
564 Okay: c = (a + b) * (a - b) |
|
565 Okay: foo(bar, key='word', *args, **kwargs) |
|
566 Okay: baz(**kwargs) |
|
567 Okay: negative = -1 |
|
568 Okay: spam(-1) |
|
569 Okay: alpha[:-i] |
|
570 Okay: if not -5 < x < +5:\n pass |
|
571 Okay: lambda *args, **kw: (args, kw) |
|
572 </p><p> |
|
573 E225: i=i+1 |
|
574 E225: submitted +=1 |
|
575 E225: x = x*2 - 1 |
|
576 E225: hypot2 = x*x + y*y |
|
577 E225: c = (a+b) * (a-b) |
|
578 E225: c = alpha -4 |
|
579 E225: z = x **y |
|
580 </p> |
485 <div align="right"><a href="#top">Up</a></div> |
581 <div align="right"><a href="#top">Up</a></div> |
486 <hr /><hr /> |
582 <hr /><hr /> |
487 <a NAME="mute_string" ID="mute_string"></a> |
583 <a NAME="mute_string" ID="mute_string"></a> |
488 <h2>mute_string</h2> |
584 <h2>mute_string</h2> |
489 <b>mute_string</b>(<i>text</i>) |
585 <b>mute_string</b>(<i>text</i>) |
617 <div align="right"><a href="#top">Up</a></div> |
713 <div align="right"><a href="#top">Up</a></div> |
618 <hr /><hr /> |
714 <hr /><hr /> |
619 <a NAME="tabs_obsolete" ID="tabs_obsolete"></a> |
715 <a NAME="tabs_obsolete" ID="tabs_obsolete"></a> |
620 <h2>tabs_obsolete</h2> |
716 <h2>tabs_obsolete</h2> |
621 <b>tabs_obsolete</b>(<i>physical_line</i>) |
717 <b>tabs_obsolete</b>(<i>physical_line</i>) |
622 |
718 <p> |
|
719 For new projects, spaces-only are strongly recommended over tabs. Most |
|
720 editors have features that make this easy to do. |
|
721 </p><p> |
|
722 Okay: if True:\n return |
|
723 W191: if True:\n\treturn |
|
724 </p> |
623 <div align="right"><a href="#top">Up</a></div> |
725 <div align="right"><a href="#top">Up</a></div> |
624 <hr /><hr /> |
726 <hr /><hr /> |
625 <a NAME="tabs_or_spaces" ID="tabs_or_spaces"></a> |
727 <a NAME="tabs_or_spaces" ID="tabs_or_spaces"></a> |
626 <h2>tabs_or_spaces</h2> |
728 <h2>tabs_or_spaces</h2> |
627 <b>tabs_or_spaces</b>(<i>physical_line, indent_char</i>) |
729 <b>tabs_or_spaces</b>(<i>physical_line, indent_char</i>) |
628 |
730 <p> |
|
731 Never mix tabs and spaces. |
|
732 </p><p> |
|
733 The most popular way of indenting Python is with spaces only. The |
|
734 second-most popular way is with tabs only. Code indented with a mixture |
|
735 of tabs and spaces should be converted to using spaces exclusively. When |
|
736 invoking the Python command line interpreter with the -t option, it issues |
|
737 warnings about code that illegally mixes tabs and spaces. When using -tt |
|
738 these warnings become errors. These options are highly recommended! |
|
739 </p><p> |
|
740 Okay: if a == 0:\n a = 1\n b = 1 |
|
741 E101: if a == 0:\n a = 1\n\tb = 1 |
|
742 </p> |
629 <div align="right"><a href="#top">Up</a></div> |
743 <div align="right"><a href="#top">Up</a></div> |
630 <hr /><hr /> |
744 <hr /><hr /> |
631 <a NAME="trailing_blank_lines" ID="trailing_blank_lines"></a> |
745 <a NAME="trailing_blank_lines" ID="trailing_blank_lines"></a> |
632 <h2>trailing_blank_lines</h2> |
746 <h2>trailing_blank_lines</h2> |
633 <b>trailing_blank_lines</b>(<i>physical_line, lines, line_number</i>) |
747 <b>trailing_blank_lines</b>(<i>physical_line, lines, line_number</i>) |
634 |
748 <p> |
|
749 JCR: Trailing blank lines are superfluous. |
|
750 </p><p> |
|
751 Okay: spam(1) |
|
752 W391: spam(1)\n |
|
753 </p> |
635 <div align="right"><a href="#top">Up</a></div> |
754 <div align="right"><a href="#top">Up</a></div> |
636 <hr /><hr /> |
755 <hr /><hr /> |
637 <a NAME="trailing_whitespace" ID="trailing_whitespace"></a> |
756 <a NAME="trailing_whitespace" ID="trailing_whitespace"></a> |
638 <h2>trailing_whitespace</h2> |
757 <h2>trailing_whitespace</h2> |
639 <b>trailing_whitespace</b>(<i>physical_line</i>) |
758 <b>trailing_whitespace</b>(<i>physical_line</i>) |
640 |
759 <p> |
|
760 JCR: Trailing whitespace is superfluous. |
|
761 FBM: Except when it occurs as part of a blank line (i.e. the line is |
|
762 nothing but whitespace). According to Python docs[1] a line with only |
|
763 whitespace is considered a blank line, and is to be ignored. However, |
|
764 matching a blank line to its indentation level avoids mistakenly |
|
765 terminating a multi-line statement (e.g. class declaration) when |
|
766 pasting code into the standard Python interpreter. |
|
767 </p><p> |
|
768 [1] http://docs.python.org/reference/lexical_analysis.html#blank-lines |
|
769 </p><p> |
|
770 The warning returned varies on whether the line itself is blank, for easier |
|
771 filtering for those who want to indent their blank lines. |
|
772 </p><p> |
|
773 Okay: spam(1) |
|
774 W291: spam(1)\s |
|
775 W293: class Foo(object):\n \n bang = 12 |
|
776 </p> |
641 <div align="right"><a href="#top">Up</a></div> |
777 <div align="right"><a href="#top">Up</a></div> |
642 <hr /><hr /> |
778 <hr /><hr /> |
643 <a NAME="whitespace_around_comma" ID="whitespace_around_comma"></a> |
779 <a NAME="whitespace_around_comma" ID="whitespace_around_comma"></a> |
644 <h2>whitespace_around_comma</h2> |
780 <h2>whitespace_around_comma</h2> |
645 <b>whitespace_around_comma</b>(<i>logical_line</i>) |
781 <b>whitespace_around_comma</b>(<i>logical_line</i>) |