Plugins/CheckerPlugins/CodeStyleChecker/pycodestyle.py

changeset 6732
1a4758379c45
parent 6645
ad476851d7e0
child 6733
519422827ace
equal deleted inserted replaced
6726:bf2ec89b1975 6732:1a4758379c45
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*- 2 # -*- coding: utf-8 -*-
3 3
4 # pycodestyle.py - Check Python source code formatting, according to PEP 8 4 # pycodestyle.py - Check Python source code formatting, according to
5 # PEP 8
5 # 6 #
6 # Copyright (C) 2006-2009 Johann C. Rocholl <johann@rocholl.net> 7 # Copyright (C) 2006-2009 Johann C. Rocholl <johann@rocholl.net>
7 # Copyright (C) 2009-2014 Florent Xicluna <florent.xicluna@gmail.com> 8 # Copyright (C) 2009-2014 Florent Xicluna <florent.xicluna@gmail.com>
8 # Copyright (C) 2014-2016 Ian Lee <ianlee1521@gmail.com> 9 # Copyright (C) 2014-2016 Ian Lee <ianlee1521@gmail.com>
9 # 10 #
56 # follows: 57 # follows:
57 # 58 #
58 # - made messages translatable via Qt 59 # - made messages translatable via Qt
59 # - added code for eric6 integration 60 # - added code for eric6 integration
60 # 61 #
61 # Copyright (c) 2011 - 2019 Detlev Offenbach <detlev@die-offenbachs.de> 62 # Copyright (c) 2011 - 2018 Detlev Offenbach <detlev@die-offenbachs.de>
62 # 63 #
63 64
64 import inspect 65 import inspect
65 import keyword 66 import keyword
66 import os 67 import os
73 74
74 try: 75 try:
75 from functools import lru_cache 76 from functools import lru_cache
76 except ImportError: 77 except ImportError:
77 def lru_cache(maxsize=128): # noqa as it's a fake implementation. 78 def lru_cache(maxsize=128): # noqa as it's a fake implementation.
78 """Does not really need a real a lru_cache, it's just optimization, so 79 """Does not really need a real a lru_cache, it's just
79 let's just do nothing here. Python 3.2+ will just get better 80 optimization, so let's just do nothing here. Python 3.2+ will
80 performances, time to upgrade? 81 just get better performances, time to upgrade?
81 """ 82 """
82 return lambda function: function 83 return lambda function: function
83 84
84 from fnmatch import fnmatch 85 from fnmatch import fnmatch
85 from optparse import OptionParser 86 from optparse import OptionParser
88 from configparser import RawConfigParser 89 from configparser import RawConfigParser
89 from io import TextIOWrapper 90 from io import TextIOWrapper
90 except ImportError: 91 except ImportError:
91 from ConfigParser import RawConfigParser # __IGNORE_WARNING__ 92 from ConfigParser import RawConfigParser # __IGNORE_WARNING__
92 93
93 __version__ = '2.4.0-eric' 94 __version__ = '2.5.0-eric'
94 95
95 DEFAULT_EXCLUDE = '.svn,CVS,.bzr,.hg,.git,__pycache__,.tox' 96 DEFAULT_EXCLUDE = '.svn,CVS,.bzr,.hg,.git,__pycache__,.tox'
96 DEFAULT_IGNORE = 'E121,E123,E126,E226,E24,E704,W503,W504' 97 DEFAULT_IGNORE = 'E121,E123,E126,E226,E24,E704,W503,W504'
97 try: 98 try:
98 if sys.platform == 'win32': 99 if sys.platform == 'win32':
113 # Top level class and function. 114 # Top level class and function.
114 'top_level': 2, 115 'top_level': 2,
115 # Methods and nested class and function. 116 # Methods and nested class and function.
116 'method': 1, 117 'method': 1,
117 } 118 }
119 MAX_DOC_LENGTH = 72
118 REPORT_FORMAT = { 120 REPORT_FORMAT = {
119 'default': '%(path)s:%(row)d:%(col)d: %(code)s %(text)s', 121 'default': '%(path)s:%(row)d:%(col)d: %(code)s %(text)s',
120 'pylint': '%(path)s:%(row)d: [%(code)s] %(text)s', 122 'pylint': '%(path)s:%(row)d: [%(code)s] %(text)s',
121 } 123 }
122 124
124 SINGLETONS = frozenset(['False', 'None', 'True']) 126 SINGLETONS = frozenset(['False', 'None', 'True'])
125 KEYWORDS = frozenset(keyword.kwlist + ['print', 'async']) - SINGLETONS 127 KEYWORDS = frozenset(keyword.kwlist + ['print', 'async']) - SINGLETONS
126 UNARY_OPERATORS = frozenset(['>>', '**', '*', '+', '-']) 128 UNARY_OPERATORS = frozenset(['>>', '**', '*', '+', '-'])
127 ARITHMETIC_OP = frozenset(['**', '*', '/', '//', '+', '-']) 129 ARITHMETIC_OP = frozenset(['**', '*', '/', '//', '+', '-'])
128 WS_OPTIONAL_OPERATORS = ARITHMETIC_OP.union(['^', '&', '|', '<<', '>>', '%']) 130 WS_OPTIONAL_OPERATORS = ARITHMETIC_OP.union(['^', '&', '|', '<<', '>>', '%'])
131 # Warn for -> function annotation operator in py3.5+ (issue 803)
132 FUNCTION_RETURN_ANNOTATION_OP = ['->'] if sys.version_info >= (3, 5) else []
129 WS_NEEDED_OPERATORS = frozenset([ 133 WS_NEEDED_OPERATORS = frozenset([
130 '**=', '*=', '/=', '//=', '+=', '-=', '!=', '<>', '<', '>', 134 '**=', '*=', '/=', '//=', '+=', '-=', '!=', '<>', '<', '>',
131 '%=', '^=', '&=', '|=', '==', '<=', '>=', '<<=', '>>=', '=']) 135 '%=', '^=', '&=', '|=', '==', '<=', '>=', '<<=', '>>=', '='] +
136 FUNCTION_RETURN_ANNOTATION_OP)
132 WHITESPACE = frozenset(' \t') 137 WHITESPACE = frozenset(' \t')
133 NEWLINE = frozenset([tokenize.NL, tokenize.NEWLINE]) 138 NEWLINE = frozenset([tokenize.NL, tokenize.NEWLINE])
134 SKIP_TOKENS = NEWLINE.union([tokenize.INDENT, tokenize.DEDENT]) 139 SKIP_TOKENS = NEWLINE.union([tokenize.INDENT, tokenize.DEDENT])
135 # ERRORTOKEN is triggered by backticks in Python 3 140 # ERRORTOKEN is triggered by backticks in Python 3
136 SKIP_COMMENTS = SKIP_TOKENS.union([tokenize.COMMENT, tokenize.ERRORTOKEN]) 141 SKIP_COMMENTS = SKIP_TOKENS.union([tokenize.COMMENT, tokenize.ERRORTOKEN])
165 'while', 170 'while',
166 ))) 171 )))
167 ) 172 )
168 DUNDER_REGEX = re.compile(r'^__([^\s]+)__ = ') 173 DUNDER_REGEX = re.compile(r'^__([^\s]+)__ = ')
169 174
170 # Work around Python < 2.6 behaviour, which does not generate NL after
171 # a comment which is on a line by itself.
172 COMMENT_WITH_NL = tokenize.generate_tokens(['#\n'].pop).send(None)[1] == '#\n'
173
174
175 _checks = {'physical_line': {}, 'logical_line': {}, 'tree': {}} 175 _checks = {'physical_line': {}, 'logical_line': {}, 'tree': {}}
176 176
177 177
178 def _get_parameters(function): 178 def _get_parameters(function):
179 if sys.version_info >= (3, 3): 179 if sys.version_info >= (3, 3):
181 for parameter 181 for parameter
182 in inspect.signature(function).parameters.values() 182 in inspect.signature(function).parameters.values()
183 if parameter.kind == parameter.POSITIONAL_OR_KEYWORD] 183 if parameter.kind == parameter.POSITIONAL_OR_KEYWORD]
184 else: 184 else:
185 return inspect.getargspec(function)[0] 185 return inspect.getargspec(function)[0]
186
186 187
187 def register_check(check, codes=None): 188 def register_check(check, codes=None):
188 """Register a new check object.""" 189 """Register a new check object."""
189 def _add_check(check, kind, codes, args): 190 def _add_check(check, kind, codes, args):
190 if check in _checks[kind]: 191 if check in _checks[kind]:
201 if _get_parameters(check.__init__)[:2] == ['self', 'tree']: 202 if _get_parameters(check.__init__)[:2] == ['self', 'tree']:
202 _add_check(check, 'tree', codes, None) 203 _add_check(check, 'tree', codes, None)
203 return check 204 return check
204 205
205 206
206 ############################################################################## 207 ########################################################################
207 # Plugins (check functions) for physical lines 208 # Plugins (check functions) for physical lines
208 ############################################################################## 209 ########################################################################
209 210
210 @register_check 211 @register_check
211 def tabs_or_spaces(physical_line, indent_char): 212 def tabs_or_spaces(physical_line, indent_char):
212 r"""Never mix tabs and spaces. 213 r"""Never mix tabs and spaces.
213 214
214 The most popular way of indenting Python is with spaces only. The 215 The most popular way of indenting Python is with spaces only. The
215 second-most popular way is with tabs only. Code indented with a mixture 216 second-most popular way is with tabs only. Code indented with a
216 of tabs and spaces should be converted to using spaces exclusively. When 217 mixture of tabs and spaces should be converted to using spaces
217 invoking the Python command line interpreter with the -t option, it issues 218 exclusively. When invoking the Python command line interpreter with
218 warnings about code that illegally mixes tabs and spaces. When using -tt 219 the -t option, it issues warnings about code that illegally mixes
219 these warnings become errors. These options are highly recommended! 220 tabs and spaces. When using -tt these warnings become errors.
220 221 These options are highly recommended!
221 Okay: if a == 0:\n a = 1\n b = 1 222
223 Okay: if a == 0:\n a = 1\n b = 1
222 E101: if a == 0:\n a = 1\n\tb = 1 224 E101: if a == 0:\n a = 1\n\tb = 1
223 """ 225 """
224 indent = INDENT_REGEX.match(physical_line).group(1) 226 indent = INDENT_REGEX.match(physical_line).group(1)
225 for offset, char in enumerate(indent): 227 for offset, char in enumerate(indent):
226 if char != indent_char: 228 if char != indent_char:
227 return offset, "E101 indentation contains mixed spaces and tabs" 229 return offset, "E101 indentation contains mixed spaces and tabs"
228 230
229 231
230 @register_check 232 @register_check
231 def tabs_obsolete(physical_line): 233 def tabs_obsolete(physical_line):
232 r"""For new projects, spaces-only are strongly recommended over tabs. 234 r"""On new projects, spaces-only are strongly recommended over tabs.
233 235
234 Okay: if True:\n return 236 Okay: if True:\n return
235 W191: if True:\n\treturn 237 W191: if True:\n\treturn
236 """ 238 """
237 indent = INDENT_REGEX.match(physical_line).group(1) 239 indent = INDENT_REGEX.match(physical_line).group(1)
241 243
242 @register_check 244 @register_check
243 def trailing_whitespace(physical_line): 245 def trailing_whitespace(physical_line):
244 r"""Trailing whitespace is superfluous. 246 r"""Trailing whitespace is superfluous.
245 247
246 The warning returned varies on whether the line itself is blank, for easier 248 The warning returned varies on whether the line itself is blank,
247 filtering for those who want to indent their blank lines. 249 for easier filtering for those who want to indent their blank lines.
248 250
249 Okay: spam(1)\n# 251 Okay: spam(1)\n#
250 W291: spam(1) \n# 252 W291: spam(1) \n#
251 W293: class Foo(object):\n \n bang = 12 253 W293: class Foo(object):\n \n bang = 12
252 """ 254 """
270 272
271 However the last line should end with a new line (warning W292). 273 However the last line should end with a new line (warning W292).
272 """ 274 """
273 if line_number == total_lines: 275 if line_number == total_lines:
274 stripped_last_line = physical_line.rstrip() 276 stripped_last_line = physical_line.rstrip()
275 if not stripped_last_line: 277 if physical_line and not stripped_last_line:
276 return 0, "W391 blank line at end of file" 278 return 0, "W391 blank line at end of file"
277 if stripped_last_line == physical_line: 279 if stripped_last_line == physical_line:
278 return len(physical_line), "W292 no newline at end of file" 280 return len(lines[-1]), "W292 no newline at end of file"
279 281
280 282
281 @register_check 283 @register_check
282 def maximum_line_length(physical_line, max_line_length, multiline, 284 def maximum_line_length(physical_line, max_line_length, multiline,
283 line_number, noqa): 285 line_number, noqa):
284 r"""Limit all lines to a maximum of 79 characters. 286 r"""Limit all lines to a maximum of 79 characters.
285 287
286 There are still many devices around that are limited to 80 character 288 There are still many devices around that are limited to 80 character
287 lines; plus, limiting windows to 80 characters makes it possible to have 289 lines; plus, limiting windows to 80 characters makes it possible to
288 several windows side-by-side. The default wrapping on such devices looks 290 have several windows side-by-side. The default wrapping on such
289 ugly. Therefore, please limit all lines to a maximum of 79 characters. 291 devices looks ugly. Therefore, please limit all lines to a maximum
290 For flowing long blocks of text (docstrings or comments), limiting the 292 of 79 characters. For flowing long blocks of text (docstrings or
291 length to 72 characters is recommended. 293 comments), limiting the length to 72 characters is recommended.
292 294
293 Reports error E501. 295 Reports error E501.
294 """ 296 """
295 line = physical_line.rstrip() 297 line = physical_line.rstrip()
296 length = len(line) 298 length = len(line)
297 if length > max_line_length and not noqa: 299 if length > max_line_length and not noqa:
298 # Special case: ignore long shebang lines. 300 # Special case: ignore long shebang lines.
299 if line_number == 1 and line.startswith('#!'): 301 if line_number == 1 and line.startswith('#!'):
300 return 302 return
301 # Special case for long URLs in multi-line docstrings or comments, 303 # Special case for long URLs in multi-line docstrings or
302 # but still report the error when the 72 first chars are whitespaces. 304 # comments, but still report the error when the 72 first chars
305 # are whitespaces.
303 chunks = line.split() 306 chunks = line.split()
304 if ((len(chunks) == 1 and multiline) or 307 if ((len(chunks) == 1 and multiline) or
305 (len(chunks) == 2 and chunks[0] == '#')) and \ 308 (len(chunks) == 2 and chunks[0] == '#')) and \
306 len(line) - len(chunks[-1]) < max_line_length - 7: 309 len(line) - len(chunks[-1]) < max_line_length - 7:
307 return 310 return
315 return (max_line_length, "E501 line too long " 318 return (max_line_length, "E501 line too long "
316 "(%d > %d characters)" % (length, max_line_length), 319 "(%d > %d characters)" % (length, max_line_length),
317 length, max_line_length) 320 length, max_line_length)
318 321
319 322
320 ############################################################################## 323 ########################################################################
321 # Plugins (check functions) for logical lines 324 # Plugins (check functions) for logical lines
322 ############################################################################## 325 ########################################################################
323 326
324 327
325 @register_check 328 @register_check
326 def blank_lines(logical_line, blank_lines, indent_level, line_number, 329 def blank_lines(logical_line, blank_lines, indent_level, line_number,
327 blank_before, previous_logical, 330 blank_before, previous_logical,
328 previous_unindented_logical_line, previous_indent_level, 331 previous_unindented_logical_line, previous_indent_level,
329 lines): 332 lines):
330 r"""Separate top-level function and class definitions with two blank lines. 333 r"""Separate top-level function and class definitions with two blank
331 334 lines.
332 Method definitions inside a class are separated by a single blank line. 335
333 336 Method definitions inside a class are separated by a single blank
334 Extra blank lines may be used (sparingly) to separate groups of related 337 line.
335 functions. Blank lines may be omitted between a bunch of related 338
336 one-liners (e.g. a set of dummy implementations). 339 Extra blank lines may be used (sparingly) to separate groups of
337 340 related functions. Blank lines may be omitted between a bunch of
338 Use blank lines in functions, sparingly, to indicate logical sections. 341 related one-liners (e.g. a set of dummy implementations).
342
343 Use blank lines in functions, sparingly, to indicate logical
344 sections.
339 345
340 Okay: def a():\n pass\n\n\ndef b():\n pass 346 Okay: def a():\n pass\n\n\ndef b():\n pass
341 Okay: def a():\n pass\n\n\nasync def b():\n pass 347 Okay: def a():\n pass\n\n\nasync def b():\n pass
342 Okay: def a():\n pass\n\n\n# Foo\n# Bar\n\ndef b():\n pass 348 Okay: def a():\n pass\n\n\n# Foo\n# Bar\n\ndef b():\n pass
343 Okay: default = 1\nfoo = 1 349 Okay: default = 1\nfoo = 1
372 "scope, expected %d", blank_lines, method_lines) 378 "scope, expected %d", blank_lines, method_lines)
373 else: 379 else:
374 yield (0, "E303 too many blank lines (%d), expected %d", 380 yield (0, "E303 too many blank lines (%d), expected %d",
375 blank_lines, top_level_lines) 381 blank_lines, top_level_lines)
376 elif STARTSWITH_TOP_LEVEL_REGEX.match(logical_line): 382 elif STARTSWITH_TOP_LEVEL_REGEX.match(logical_line):
383 # If this is a one-liner (i.e. the next line is not more
384 # indented), and the previous line is also not deeper
385 # (it would be better to check if the previous line is part
386 # of another def/class at the same level), don't require blank
387 # lines around this.
388 prev_line = lines[line_number - 2] if line_number >= 2 else ''
389 next_line = lines[line_number] if line_number < len(lines) else ''
390 if (expand_indent(prev_line) <= indent_level and
391 expand_indent(next_line) <= indent_level):
392 return
377 if indent_level: 393 if indent_level:
378 if not (blank_before == method_lines or 394 if not (blank_before == method_lines or
379 previous_indent_level < indent_level or 395 previous_indent_level < indent_level or
380 DOCSTRING_REGEX.match(previous_logical) 396 DOCSTRING_REGEX.match(previous_logical)
381 ): 397 ):
382 ancestor_level = indent_level 398 ancestor_level = indent_level
383 nested = False 399 nested = False
384 # Search backwards for a def ancestor or tree root (top level). 400 # Search backwards for a def ancestor or tree root
401 # (top level).
385 for line in lines[line_number - top_level_lines::-1]: 402 for line in lines[line_number - top_level_lines::-1]:
386 if line.strip() and expand_indent(line) < ancestor_level: 403 if line.strip() and expand_indent(line) < ancestor_level:
387 ancestor_level = expand_indent(line) 404 ancestor_level = expand_indent(line)
388 nested = line.lstrip().startswith('def ') 405 nested = line.lstrip().startswith('def ')
389 if nested or ancestor_level == 0: 406 if nested or ancestor_level == 0:
465 yield match.start(2), "E271 multiple spaces after keyword" 482 yield match.start(2), "E271 multiple spaces after keyword"
466 483
467 484
468 @register_check 485 @register_check
469 def missing_whitespace_after_import_keyword(logical_line): 486 def missing_whitespace_after_import_keyword(logical_line):
470 r"""Multiple imports in form from x import (a, b, c) should have space 487 r"""Multiple imports in form from x import (a, b, c) should have
471 between import statement and parenthesised name list. 488 space between import statement and parenthesised name list.
472 489
473 Okay: from foo import (bar, baz) 490 Okay: from foo import (bar, baz)
474 E275: from foo import(bar, baz) 491 E275: from foo import(bar, baz)
475 E275: from importable.module import(bar, baz) 492 E275: from importable.module import(bar, baz)
476 """ 493 """
513 @register_check 530 @register_check
514 def indentation(logical_line, previous_logical, indent_char, 531 def indentation(logical_line, previous_logical, indent_char,
515 indent_level, previous_indent_level): 532 indent_level, previous_indent_level):
516 r"""Use 4 spaces per indentation level. 533 r"""Use 4 spaces per indentation level.
517 534
518 For really old code that you don't want to mess up, you can continue to 535 For really old code that you don't want to mess up, you can continue
519 use 8-space tabs. 536 to use 8-space tabs.
520 537
521 Okay: a = 1 538 Okay: a = 1
522 Okay: if a == 0:\n a = 1 539 Okay: if a == 0:\n a = 1
523 E111: a = 1 540 E111: a = 1
524 E114: # a = 1 541 E114: # a = 1
539 if indent_expect and indent_level <= previous_indent_level: 556 if indent_expect and indent_level <= previous_indent_level:
540 yield 0, tmpl % (2 + c, "expected an indented block") 557 yield 0, tmpl % (2 + c, "expected an indented block")
541 elif not indent_expect and indent_level > previous_indent_level: 558 elif not indent_expect and indent_level > previous_indent_level:
542 yield 0, tmpl % (3 + c, "unexpected indentation") 559 yield 0, tmpl % (3 + c, "unexpected indentation")
543 560
561 expected_indent_level = previous_indent_level + 4
562 if indent_expect and indent_level > expected_indent_level:
563 yield 0, tmpl % (7, 'over-indented')
564
544 565
545 @register_check 566 @register_check
546 def continued_indentation(logical_line, tokens, indent_level, hang_closing, 567 def continued_indentation(logical_line, tokens, indent_level, hang_closing,
547 indent_char, noqa, verbose): 568 indent_char, noqa, verbose):
548 r"""Continuation lines indentation. 569 r"""Continuation lines indentation.
551 using Python's implicit line joining inside parentheses, brackets 572 using Python's implicit line joining inside parentheses, brackets
552 and braces, or using a hanging indent. 573 and braces, or using a hanging indent.
553 574
554 When using a hanging indent these considerations should be applied: 575 When using a hanging indent these considerations should be applied:
555 - there should be no arguments on the first line, and 576 - there should be no arguments on the first line, and
556 - further indentation should be used to clearly distinguish itself as a 577 - further indentation should be used to clearly distinguish itself
557 continuation line. 578 as a continuation line.
558 579
559 Okay: a = (\n) 580 Okay: a = (\n)
560 E123: a = (\n ) 581 E123: a = (\n )
561 582
562 Okay: a = (\n 42) 583 Okay: a = (\n 42)
637 # closing bracket for visual indent 658 # closing bracket for visual indent
638 if start[1] != indent[depth]: 659 if start[1] != indent[depth]:
639 yield (start, "E124 closing bracket does not match " 660 yield (start, "E124 closing bracket does not match "
640 "visual indentation") 661 "visual indentation")
641 elif close_bracket and not hang: 662 elif close_bracket and not hang:
642 # closing bracket matches indentation of opening bracket's line 663 # closing bracket matches indentation of opening
664 # bracket's line
643 if hang_closing: 665 if hang_closing:
644 yield start, "E133 closing bracket is missing indentation" 666 yield start, "E133 closing bracket is missing indentation"
645 elif indent[depth] and start[1] < indent[depth]: 667 elif indent[depth] and start[1] < indent[depth]:
646 if visual_indent is not True: 668 if visual_indent is not True:
647 # visual indent is broken 669 # visual indent is broken
655 hangs[depth] = hang 677 hangs[depth] = hang
656 elif visual_indent is True: 678 elif visual_indent is True:
657 # visual indent is verified 679 # visual indent is verified
658 indent[depth] = start[1] 680 indent[depth] = start[1]
659 elif visual_indent in (text, str): 681 elif visual_indent in (text, str):
660 # ignore token lined up with matching one from a previous line 682 # ignore token lined up with matching one from a
683 # previous line
661 pass 684 pass
662 else: 685 else:
663 # indent is broken 686 # indent is broken
664 if hang <= 0: 687 if hang <= 0:
665 error = "E122", "missing indentation or outdented" 688 error = "E122", "missing indentation or outdented"
868 # Allow keyword args or defaults: foo(bar=None). 891 # Allow keyword args or defaults: foo(bar=None).
869 pass 892 pass
870 elif text in WS_NEEDED_OPERATORS: 893 elif text in WS_NEEDED_OPERATORS:
871 need_space = True 894 need_space = True
872 elif text in UNARY_OPERATORS: 895 elif text in UNARY_OPERATORS:
873 # Check if the operator is being used as a binary operator 896 # Check if the operator is used as a binary operator
874 # Allow unary operators: -123, -x, +1. 897 # Allow unary operators: -123, -x, +1.
875 # Allow argument unpacking: foo(*args, **kwargs). 898 # Allow argument unpacking: foo(*args, **kwargs).
876 if (prev_text in '}])' if prev_type == tokenize.OP 899 if (prev_text in '}])' if prev_type == tokenize.OP
877 else prev_text not in KEYWORDS): 900 else prev_text not in KEYWORDS):
878 need_space = None 901 need_space = None
914 @register_check 937 @register_check
915 def whitespace_around_named_parameter_equals(logical_line, tokens): 938 def whitespace_around_named_parameter_equals(logical_line, tokens):
916 r"""Don't use spaces around the '=' sign in function arguments. 939 r"""Don't use spaces around the '=' sign in function arguments.
917 940
918 Don't use spaces around the '=' sign when used to indicate a 941 Don't use spaces around the '=' sign when used to indicate a
919 keyword argument or a default parameter value, except when using a type 942 keyword argument or a default parameter value, except when
920 annotation. 943 using a type annotation.
921 944
922 Okay: def complex(real, imag=0.0): 945 Okay: def complex(real, imag=0.0):
923 Okay: return magic(r=real, i=imag) 946 Okay: return magic(r=real, i=imag)
924 Okay: boolean(a == b) 947 Okay: boolean(a == b)
925 Okay: boolean(a != b) 948 Okay: boolean(a != b)
958 parens += 1 981 parens += 1
959 elif text in ')]': 982 elif text in ')]':
960 parens -= 1 983 parens -= 1
961 elif in_def and text == ':' and parens == 1: 984 elif in_def and text == ':' and parens == 1:
962 annotated_func_arg = True 985 annotated_func_arg = True
963 elif parens and text == ',' and parens == 1: 986 elif parens == 1 and text == ',':
964 annotated_func_arg = False 987 annotated_func_arg = False
965 elif parens and text == '=': 988 elif parens and text == '=':
966 if not annotated_func_arg: 989 if annotated_func_arg and parens == 1:
990 require_space = True
991 if start == prev_end:
992 yield (prev_end, missing_message)
993 else:
967 no_space = True 994 no_space = True
968 if start != prev_end: 995 if start != prev_end:
969 yield (prev_end, message) 996 yield (prev_end, message)
970 else:
971 require_space = True
972 if start == prev_end:
973 yield (prev_end, missing_message)
974 if not parens: 997 if not parens:
975 annotated_func_arg = False 998 annotated_func_arg = False
976 999
977 prev_end = end 1000 prev_end = end
978 1001
979 1002
980 @register_check 1003 @register_check
981 def whitespace_before_comment(logical_line, tokens): 1004 def whitespace_before_comment(logical_line, tokens):
982 r"""Separate inline comments by at least two spaces. 1005 r"""Separate inline comments by at least two spaces.
983 1006
984 An inline comment is a comment on the same line as a statement. Inline 1007 An inline comment is a comment on the same line as a statement.
985 comments should be separated by at least two spaces from the statement. 1008 Inline comments should be separated by at least two spaces from the
986 They should start with a # and a single space. 1009 statement. They should start with a # and a single space.
987 1010
988 Each line of a block comment starts with a # and a single space 1011 Each line of a block comment starts with a # and a single space
989 (unless it is indented text inside the comment). 1012 (unless it is indented text inside the comment).
990 1013
991 Okay: x = x + 1 # Increment x 1014 Okay: x = x + 1 # Increment x
1042 @register_check 1065 @register_check
1043 def module_imports_on_top_of_file( 1066 def module_imports_on_top_of_file(
1044 logical_line, indent_level, checker_state, noqa): 1067 logical_line, indent_level, checker_state, noqa):
1045 r"""Place imports at the top of the file. 1068 r"""Place imports at the top of the file.
1046 1069
1047 Always put imports at the top of the file, just after any module comments 1070 Always put imports at the top of the file, just after any module
1048 and docstrings, and before module globals and constants. 1071 comments and docstrings, and before module globals and constants.
1049 1072
1050 Okay: import os 1073 Okay: import os
1051 Okay: # this is a comment\nimport os 1074 Okay: # this is a comment\nimport os
1052 Okay: '''this is a module docstring'''\nimport os 1075 Okay: '''this is a module docstring'''\nimport os
1053 Okay: r'''this is a module docstring'''\nimport os 1076 Okay: r'''this is a module docstring'''\nimport os
1059 E402: a=1\nimport os 1082 E402: a=1\nimport os
1060 E402: 'One string'\n"Two string"\nimport os 1083 E402: 'One string'\n"Two string"\nimport os
1061 E402: a=1\nfrom sys import x 1084 E402: a=1\nfrom sys import x
1062 1085
1063 Okay: if x:\n import os 1086 Okay: if x:\n import os
1064 """ 1087 """ # noqa
1065 def is_string_literal(line): 1088 def is_string_literal(line):
1066 if line[0] in 'uUbB': 1089 if line[0] in 'uUbB':
1067 line = line[1:] 1090 line = line[1:]
1068 if line and line[0] in 'rR': 1091 if line and line[0] in 'rR':
1069 line = line[1:] 1092 line = line[1:]
1070 return line and (line[0] == '"' or line[0] == "'") 1093 return line and (line[0] == '"' or line[0] == "'")
1071 1094
1072 allowed_try_keywords = ('try', 'except', 'else', 'finally') 1095 allowed_try_keywords = ('try', 'except', 'else', 'finally')
1073 1096
1074 if indent_level: # Allow imports in conditional statements or functions 1097 if indent_level: # Allow imports in conditional statement/function
1075 return 1098 return
1076 if not logical_line: # Allow empty lines or comments 1099 if not logical_line: # Allow empty lines or comments
1077 return 1100 return
1078 if noqa: 1101 if noqa:
1079 return 1102 return
1082 if checker_state.get('seen_non_imports', False): 1105 if checker_state.get('seen_non_imports', False):
1083 yield 0, "E402 module level import not at top of file" 1106 yield 0, "E402 module level import not at top of file"
1084 elif re.match(DUNDER_REGEX, line): 1107 elif re.match(DUNDER_REGEX, line):
1085 return 1108 return
1086 elif any(line.startswith(kw) for kw in allowed_try_keywords): 1109 elif any(line.startswith(kw) for kw in allowed_try_keywords):
1087 # Allow try, except, else, finally keywords intermixed with imports in 1110 # Allow try, except, else, finally keywords intermixed with
1088 # order to support conditional importing 1111 # imports in order to support conditional importing
1089 return 1112 return
1090 elif is_string_literal(line): 1113 elif is_string_literal(line):
1091 # The first literal is a docstring, allow it. Otherwise, report error. 1114 # The first literal is a docstring, allow it. Otherwise, report
1115 # error.
1092 if checker_state.get('seen_docstring', False): 1116 if checker_state.get('seen_docstring', False):
1093 checker_state['seen_non_imports'] = True 1117 checker_state['seen_non_imports'] = True
1094 else: 1118 else:
1095 checker_state['seen_docstring'] = True 1119 checker_state['seen_docstring'] = True
1096 else: 1120 else:
1097 checker_state['seen_non_imports'] = True 1121 checker_state['seen_non_imports'] = True
1098 1122
1099 1123
1100 @register_check 1124 @register_check
1101 def compound_statements(logical_line): 1125 def compound_statements(logical_line):
1102 r"""Compound statements (on the same line) are generally discouraged. 1126 r"""Compound statements (on the same line) are generally
1127 discouraged.
1103 1128
1104 While sometimes it's okay to put an if/for/while with a small body 1129 While sometimes it's okay to put an if/for/while with a small body
1105 on the same line, never do this for multi-clause statements. 1130 on the same line, never do this for multi-clause statements.
1106 Also avoid folding such long lines! 1131 Also avoid folding such long lines!
1107 1132
1128 """ 1153 """
1129 line = logical_line 1154 line = logical_line
1130 last_char = len(line) - 1 1155 last_char = len(line) - 1
1131 found = line.find(':') 1156 found = line.find(':')
1132 prev_found = 0 1157 prev_found = 0
1133 counts = dict((char, 0) for char in '{}[]()') 1158 counts = {char: 0 for char in '{}[]()'}
1134 while -1 < found < last_char: 1159 while -1 < found < last_char:
1135 update_counts(line[prev_found:found], counts) 1160 update_counts(line[prev_found:found], counts)
1136 if ((counts['{'] <= counts['}'] and # {'a': 1} (dict) 1161 if ((counts['{'] <= counts['}'] and # {'a': 1} (dict)
1137 counts['['] <= counts[']'] and # [1:2] (slice) 1162 counts['['] <= counts[']'] and # [1:2] (slice)
1138 counts['('] <= counts[')'])): # (annotation) 1163 counts['('] <= counts[')'])): # (annotation)
1160 1185
1161 @register_check 1186 @register_check
1162 def explicit_line_join(logical_line, tokens): 1187 def explicit_line_join(logical_line, tokens):
1163 r"""Avoid explicit line join between brackets. 1188 r"""Avoid explicit line join between brackets.
1164 1189
1165 The preferred way of wrapping long lines is by using Python's implied line 1190 The preferred way of wrapping long lines is by using Python's
1166 continuation inside parentheses, brackets and braces. Long lines can be 1191 implied line continuation inside parentheses, brackets and braces.
1167 broken over multiple lines by wrapping expressions in parentheses. These 1192 Long lines can be broken over multiple lines by wrapping expressions
1168 should be used in preference to using a backslash for line continuation. 1193 in parentheses. These should be used in preference to using a
1194 backslash for line continuation.
1169 1195
1170 E502: aaa = [123, \\n 123] 1196 E502: aaa = [123, \\n 123]
1171 E502: aaa = ("bbb " \\n "ccc") 1197 E502: aaa = ("bbb " \\n "ccc")
1172 1198
1173 Okay: aaa = [123,\n 123] 1199 Okay: aaa = [123,\n 123]
1200 1226
1201 def _is_binary_operator(token_type, text): 1227 def _is_binary_operator(token_type, text):
1202 is_op_token = token_type == tokenize.OP 1228 is_op_token = token_type == tokenize.OP
1203 is_conjunction = text in ['and', 'or'] 1229 is_conjunction = text in ['and', 'or']
1204 # NOTE(sigmavirus24): Previously the not_a_symbol check was executed 1230 # NOTE(sigmavirus24): Previously the not_a_symbol check was executed
1205 # conditionally. Since it is now *always* executed, text may be None. 1231 # conditionally. Since it is now *always* executed, text may be
1206 # In that case we get a TypeError for `text not in str`. 1232 # None. In that case we get a TypeError for `text not in str`.
1207 not_a_symbol = text and text not in "()[]{},:.;@=%~" 1233 not_a_symbol = text and text not in "()[]{},:.;@=%~"
1208 # The % character is strictly speaking a binary operator, but the 1234 # The % character is strictly speaking a binary operator, but the
1209 # common usage seems to be to put it next to the format parameters, 1235 # common usage seems to be to put it next to the format parameters,
1210 # after a line break. 1236 # after a line break.
1211 return ((is_op_token or is_conjunction) and not_a_symbol) 1237 return ((is_op_token or is_conjunction) and not_a_symbol)
1288 1314
1289 The following should be W504 but unary_context is tricky with these 1315 The following should be W504 but unary_context is tricky with these
1290 Okay: var = (1 /\n -2) 1316 Okay: var = (1 /\n -2)
1291 Okay: var = (1 +\n -1 +\n -2) 1317 Okay: var = (1 +\n -1 +\n -2)
1292 """ 1318 """
1319 prev_start = None
1293 for context in _break_around_binary_operators(tokens): 1320 for context in _break_around_binary_operators(tokens):
1294 (token_type, text, previous_token_type, previous_text, 1321 (token_type, text, previous_token_type, previous_text,
1295 line_break, unary_context, start) = context 1322 line_break, unary_context, start) = context
1296 if (_is_binary_operator(previous_token_type, previous_text) and 1323 if (_is_binary_operator(previous_token_type, previous_text) and
1297 line_break and 1324 line_break and
1298 not unary_context and 1325 not unary_context and
1299 not _is_binary_operator(token_type, text)): 1326 not _is_binary_operator(token_type, text)):
1300 error_pos = (start[0] - 1, start[1]) 1327 yield prev_start, "W504 line break after binary operator"
1301 yield error_pos, "W504 line break after binary operator" 1328 prev_start = start
1302 1329
1303 1330
1304 @register_check 1331 @register_check
1305 def comparison_to_singleton(logical_line, noqa): 1332 def comparison_to_singleton(logical_line, noqa):
1306 r"""Comparison to singletons should use "is" or "is not". 1333 r"""Comparison to singletons should use "is" or "is not".
1312 E711: if arg != None: 1339 E711: if arg != None:
1313 E711: if None == arg: 1340 E711: if None == arg:
1314 E712: if arg == True: 1341 E712: if arg == True:
1315 E712: if False == arg: 1342 E712: if False == arg:
1316 1343
1317 Also, beware of writing if x when you really mean if x is not None -- 1344 Also, beware of writing if x when you really mean if x is not None
1318 e.g. when testing whether a variable or argument that defaults to None was 1345 -- e.g. when testing whether a variable or argument that defaults to
1319 set to some other value. The other value might have a type (such as a 1346 None was set to some other value. The other value might have a type
1320 container) that could be false in a boolean context! 1347 (such as a container) that could be false in a boolean context!
1321 """ 1348 """
1322 match = not noqa and COMPARE_SINGLETON_REGEX.search(logical_line) 1349 match = not noqa and COMPARE_SINGLETON_REGEX.search(logical_line)
1323 if match: 1350 if match:
1324 singleton = match.group(1) or match.group(3) 1351 singleton = match.group(1) or match.group(3)
1325 same = (match.group(2) == '==') 1352 same = (match.group(2) == '==')
1365 Do not compare types directly. 1392 Do not compare types directly.
1366 1393
1367 Okay: if isinstance(obj, int): 1394 Okay: if isinstance(obj, int):
1368 E721: if type(obj) is type(1): 1395 E721: if type(obj) is type(1):
1369 1396
1370 When checking if an object is a string, keep in mind that it might be a 1397 When checking if an object is a string, keep in mind that it might
1371 unicode string too! In Python 2.3, str and unicode have a common base 1398 be a unicode string too! In Python 2.3, str and unicode have a
1372 class, basestring, so you can do: 1399 common base class, basestring, so you can do:
1373 1400
1374 Okay: if isinstance(obj, basestring): 1401 Okay: if isinstance(obj, basestring):
1375 Okay: if type(a1) is type(b1): 1402 Okay: if type(a1) is type(b1):
1376 """ 1403 """
1377 match = COMPARE_TYPE_REGEX.search(logical_line) 1404 match = COMPARE_TYPE_REGEX.search(logical_line)
1382 yield match.start(), "E721 do not compare types, use 'isinstance()'" 1409 yield match.start(), "E721 do not compare types, use 'isinstance()'"
1383 1410
1384 1411
1385 @register_check 1412 @register_check
1386 def bare_except(logical_line, noqa): 1413 def bare_except(logical_line, noqa):
1387 r"""When catching exceptions, mention specific exceptions when possible. 1414 r"""When catching exceptions, mention specific exceptions when
1415 possible.
1388 1416
1389 Okay: except Exception: 1417 Okay: except Exception:
1390 Okay: except BaseException: 1418 Okay: except BaseException:
1391 E722: except: 1419 E722: except:
1392 """ 1420 """
1401 1429
1402 @register_check 1430 @register_check
1403 def ambiguous_identifier(logical_line, tokens): 1431 def ambiguous_identifier(logical_line, tokens):
1404 r"""Never use the characters 'l', 'O', or 'I' as variable names. 1432 r"""Never use the characters 'l', 'O', or 'I' as variable names.
1405 1433
1406 In some fonts, these characters are indistinguishable from the numerals 1434 In some fonts, these characters are indistinguishable from the
1407 one and zero. When tempted to use 'l', use 'L' instead. 1435 numerals one and zero. When tempted to use 'l', use 'L' instead.
1408 1436
1409 Okay: L = 0 1437 Okay: L = 0
1410 Okay: o = 123 1438 Okay: o = 123
1411 Okay: i = 42 1439 Okay: i = 42
1412 E741: l = 0 1440 E741: l = 0
1413 E741: O = 123 1441 E741: O = 123
1414 E741: I = 42 1442 E741: I = 42
1415 1443
1416 Variables can be bound in several other contexts, including class and 1444 Variables can be bound in several other contexts, including class
1417 function definitions, 'global' and 'nonlocal' statements, exception 1445 and function definitions, 'global' and 'nonlocal' statements,
1418 handlers, and 'with' statements. 1446 exception handlers, and 'with' statements.
1419 1447
1420 Okay: except AttributeError as o: 1448 Okay: except AttributeError as o:
1421 Okay: with lock as L: 1449 Okay: with lock as L:
1422 E741: except AttributeError as O: 1450 E741: except AttributeError as O:
1423 E741: with lock as l: 1451 E741: with lock as l:
1433 # identifiers on the lhs of an assignment operator 1461 # identifiers on the lhs of an assignment operator
1434 if token_type == tokenize.OP and '=' in text: 1462 if token_type == tokenize.OP and '=' in text:
1435 if prev_text in idents_to_avoid: 1463 if prev_text in idents_to_avoid:
1436 ident = prev_text 1464 ident = prev_text
1437 pos = prev_start 1465 pos = prev_start
1438 # identifiers bound to a value with 'as', 'global', or 'nonlocal' 1466 # identifiers bound to values with 'as', 'global', or 'nonlocal'
1439 if prev_text in ('as', 'global', 'nonlocal'): 1467 if prev_text in ('as', 'global', 'nonlocal'):
1440 if text in idents_to_avoid: 1468 if text in idents_to_avoid:
1441 ident = text 1469 ident = text
1442 pos = start 1470 pos = start
1443 if prev_text == 'class': 1471 if prev_text == 'class':
1452 prev_start = start 1480 prev_start = start
1453 1481
1454 1482
1455 @register_check 1483 @register_check
1456 def python_3000_has_key(logical_line, noqa): 1484 def python_3000_has_key(logical_line, noqa):
1457 r"""The {}.has_key() method is removed in Python 3: use the 'in' operator. 1485 r"""The {}.has_key() method is removed in Python 3: use the 'in'
1486 operator.
1458 1487
1459 Okay: if "alph" in d:\n print d["alph"] 1488 Okay: if "alph" in d:\n print d["alph"]
1460 W601: assert d.has_key('alph') 1489 W601: assert d.has_key('alph')
1461 """ 1490 """
1462 pos = logical_line.find('.has_key(') 1491 pos = logical_line.find('.has_key(')
1503 if pos > -1: 1532 if pos > -1:
1504 yield pos, "W604 backticks are deprecated, use 'repr()'" 1533 yield pos, "W604 backticks are deprecated, use 'repr()'"
1505 1534
1506 1535
1507 @register_check 1536 @register_check
1508 def python_3000_invalid_escape_sequence(logical_line, tokens): 1537 def python_3000_invalid_escape_sequence(logical_line, tokens, noqa):
1509 r"""Invalid escape sequences are deprecated in Python 3.6. 1538 r"""Invalid escape sequences are deprecated in Python 3.6.
1510 1539
1511 Okay: regex = r'\.png$' 1540 Okay: regex = r'\.png$'
1512 W605: regex = '\.png$' 1541 W605: regex = '\.png$'
1513 """ 1542 """
1543 if noqa:
1544 return
1545
1514 # https://docs.python.org/3/reference/lexical_analysis.html#string-and-bytes-literals 1546 # https://docs.python.org/3/reference/lexical_analysis.html#string-and-bytes-literals
1515 valid = [ 1547 valid = [
1516 '\n', 1548 '\n',
1517 '\\', 1549 '\\',
1518 '\'', 1550 '\'',
1533 'U', 1565 'U',
1534 ] 1566 ]
1535 1567
1536 for token_type, text, start, end, line in tokens: 1568 for token_type, text, start, end, line in tokens:
1537 if token_type == tokenize.STRING: 1569 if token_type == tokenize.STRING:
1570 start_line, start_col = start
1538 quote = text[-3:] if text[-3:] in ('"""', "'''") else text[-1] 1571 quote = text[-3:] if text[-3:] in ('"""', "'''") else text[-1]
1539 # Extract string modifiers (e.g. u or r) 1572 # Extract string modifiers (e.g. u or r)
1540 quote_pos = text.index(quote) 1573 quote_pos = text.index(quote)
1541 prefix = text[:quote_pos].lower() 1574 prefix = text[:quote_pos].lower()
1542 start = quote_pos + len(quote) 1575 start = quote_pos + len(quote)
1545 if 'r' not in prefix: 1578 if 'r' not in prefix:
1546 pos = string.find('\\') 1579 pos = string.find('\\')
1547 while pos >= 0: 1580 while pos >= 0:
1548 pos += 1 1581 pos += 1
1549 if string[pos] not in valid: 1582 if string[pos] not in valid:
1583 line = start_line + string.count('\n', 0, pos)
1584 if line == start_line:
1585 col = start_col + len(prefix) + len(quote) + pos
1586 else:
1587 col = pos - string.rfind('\n', 0, pos) - 1
1550 yield ( 1588 yield (
1551 pos, 1589 (line, col - 1),
1552 "W605 invalid escape sequence '\\%s'", 1590 "W605 invalid escape sequence '\\%s'",
1553 string[pos], 1591 string[pos],
1554 ) 1592 )
1555 pos = string.find('\\', pos + 1) 1593 pos = string.find('\\', pos + 1)
1556 1594
1557 1595
1558 @register_check 1596 @register_check
1559 def python_3000_async_await_keywords(logical_line, tokens): 1597 def python_3000_async_await_keywords(logical_line, tokens):
1560 """'async' and 'await' are reserved keywords starting with Python 3.7 1598 """'async' and 'await' are reserved keywords starting at Python 3.7.
1561 1599
1562 W606: async = 42 1600 W606: async = 42
1563 W606: await = 42 1601 W606: await = 42
1564 Okay: async def read_data(db):\n data = await db.fetch('SELECT ...') 1602 Okay: async def read(db):\n data = await db.fetch('SELECT ...')
1565 """ 1603 """
1566 # The Python tokenize library before Python 3.5 recognizes async/await as a 1604 # The Python tokenize library before Python 3.5 recognizes
1567 # NAME token. Therefore, use a state machine to look for the possible 1605 # async/await as a NAME token. Therefore, use a state machine to
1568 # async/await constructs as defined by the Python grammar: 1606 # look for the possible async/await constructs as defined by the
1607 # Python grammar:
1569 # https://docs.python.org/3/reference/grammar.html 1608 # https://docs.python.org/3/reference/grammar.html
1570 1609
1571 state = None 1610 state = None
1572 for token_type, text, start, end, line in tokens: 1611 for token_type, text, start, end, line in tokens:
1573 error = False 1612 error = False
1613
1614 if token_type == tokenize.NL:
1615 continue
1574 1616
1575 if state is None: 1617 if state is None:
1576 if token_type == tokenize.NAME: 1618 if token_type == tokenize.NAME:
1577 if text == 'async': 1619 if text == 'async':
1578 state = ('async_stmt', start) 1620 state = ('async_stmt', start)
1579 elif text == 'await': 1621 elif text == 'await':
1580 state = ('await', start) 1622 state = ('await', start)
1623 elif (token_type == tokenize.NAME and
1624 text in ('def', 'for')):
1625 state = ('define', start)
1626
1581 elif state[0] == 'async_stmt': 1627 elif state[0] == 'async_stmt':
1582 if token_type == tokenize.NAME and text in ('def', 'with', 'for'): 1628 if token_type == tokenize.NAME and text in ('def', 'with', 'for'):
1583 # One of funcdef, with_stmt, or for_stmt. Return to looking 1629 # One of funcdef, with_stmt, or for_stmt. Return to
1584 # for async/await names. 1630 # looking for async/await names.
1585 state = None 1631 state = None
1586 else: 1632 else:
1587 error = True 1633 error = True
1588 elif state[0] == 'await': 1634 elif state[0] == 'await':
1589 if token_type in (tokenize.NAME, tokenize.NUMBER, tokenize.STRING): 1635 if token_type == tokenize.NAME:
1590 # An await expression. Return to looking for async/await names. 1636 # An await expression. Return to looking for async/await
1637 # names.
1638 state = None
1639 elif token_type == tokenize.OP and text == '(':
1591 state = None 1640 state = None
1592 else: 1641 else:
1593 error = True 1642 error = True
1643 elif state[0] == 'define':
1644 if token_type == tokenize.NAME and text in ('async', 'await'):
1645 error = True
1646 else:
1647 state = None
1594 1648
1595 if error: 1649 if error:
1596 yield ( 1650 yield (
1597 state[1], 1651 state[1],
1598 "W606 'async' and 'await' are reserved keywords starting with " 1652 "W606 'async' and 'await' are reserved keywords starting with "
1607 "W606 'async' and 'await' are reserved keywords starting with " 1661 "W606 'async' and 'await' are reserved keywords starting with "
1608 "Python 3.7", 1662 "Python 3.7",
1609 ) 1663 )
1610 1664
1611 1665
1612 ############################################################################## 1666 ########################################################################
1667 @register_check
1668 def maximum_doc_length(logical_line, max_doc_length, noqa, tokens):
1669 r"""Limit all doc lines to a maximum of 72 characters.
1670
1671 For flowing long blocks of text (docstrings or comments), limiting
1672 the length to 72 characters is recommended.
1673
1674 Reports warning W505
1675 """
1676 if max_doc_length is None or noqa:
1677 return
1678
1679 prev_token = None
1680 skip_lines = set()
1681 # Skip lines that
1682 for token_type, text, start, end, line in tokens:
1683 if token_type not in SKIP_COMMENTS.union([tokenize.STRING]):
1684 skip_lines.add(line)
1685
1686 for token_type, text, start, end, line in tokens:
1687 # Skip lines that aren't pure strings
1688 if token_type == tokenize.STRING and skip_lines:
1689 continue
1690 if token_type in (tokenize.STRING, tokenize.COMMENT):
1691 # Only check comment-only lines
1692 if prev_token is None or prev_token in SKIP_TOKENS:
1693 lines = line.splitlines()
1694 for line_num, physical_line in enumerate(lines):
1695 if hasattr(physical_line, 'decode'): # Python 2
1696 # The line could contain multi-byte characters
1697 try:
1698 physical_line = physical_line.decode('utf-8')
1699 except UnicodeError:
1700 pass
1701 if start[0] + line_num == 1 and line.startswith('#!'):
1702 return
1703 length = len(physical_line)
1704 chunks = physical_line.split()
1705 if token_type == tokenize.COMMENT:
1706 if (len(chunks) == 2 and
1707 length - len(chunks[-1]) < MAX_DOC_LENGTH):
1708 continue
1709 if len(chunks) == 1 and line_num + 1 < len(lines):
1710 if (len(chunks) == 1 and
1711 length - len(chunks[-1]) < MAX_DOC_LENGTH):
1712 continue
1713 if length > max_doc_length:
1714 doc_error = (start[0] + line_num, max_doc_length)
1715 yield (doc_error, "W505 doc line too long "
1716 "(%d > %d characters)",
1717 length, max_doc_length)
1718 prev_token = token_type
1719
1720
1721 ########################################################################
1613 # Helper functions 1722 # Helper functions
1614 ############################################################################## 1723 ########################################################################
1615 1724
1616 1725
1617 if sys.version_info < (3,): 1726 if sys.version_info < (3,):
1618 # Python 2: implicit encoding. 1727 # Python 2: implicit encoding.
1619 def readlines(filename): 1728 def readlines(filename):
1706 hunk_match = HUNK_REGEX.match(line) 1815 hunk_match = HUNK_REGEX.match(line)
1707 (row, nrows) = [int(g or '1') for g in hunk_match.groups()] 1816 (row, nrows) = [int(g or '1') for g in hunk_match.groups()]
1708 rv[path].update(range(row, row + nrows)) 1817 rv[path].update(range(row, row + nrows))
1709 elif line[:3] == '+++': 1818 elif line[:3] == '+++':
1710 path = line[4:].split('\t', 1)[0] 1819 path = line[4:].split('\t', 1)[0]
1711 # Git diff will use (i)ndex, (w)ork tree, (c)ommit and (o)bject 1820 # Git diff will use (i)ndex, (w)ork tree, (c)ommit and
1712 # instead of a/b/c/d as prefixes for patches 1821 # (o)bject instead of a/b/c/d as prefixes for patches
1713 if path[:2] in ('b/', 'w/', 'i/'): 1822 if path[:2] in ('b/', 'w/', 'i/'):
1714 path = path[2:] 1823 path = path[2:]
1715 rv[path] = set() 1824 rv[path] = set()
1716 return dict([(os.path.join(parent, path), rows) 1825 return {
1717 for (path, rows) in rv.items() 1826 os.path.join(parent, filepath): rows
1718 if rows and filename_match(path, patterns)]) 1827 for (filepath, rows) in rv.items()
1828 if rows and filename_match(filepath, patterns)
1829 }
1719 1830
1720 1831
1721 def normalize_paths(value, parent=os.curdir): 1832 def normalize_paths(value, parent=os.curdir):
1722 """Parse a comma-separated list of paths. 1833 """Parse a comma-separated list of paths.
1723 1834
1756 1867
1757 def _is_eol_token(token): 1868 def _is_eol_token(token):
1758 return token[0] in NEWLINE or token[4][token[3][1]:].lstrip() == '\\\n' 1869 return token[0] in NEWLINE or token[4][token[3][1]:].lstrip() == '\\\n'
1759 1870
1760 1871
1761 if COMMENT_WITH_NL: 1872 ########################################################################
1762 def _is_eol_token(token, _eol_token=_is_eol_token):
1763 return _eol_token(token) or (token[0] == tokenize.COMMENT and
1764 token[1] == token[4])
1765
1766 ##############################################################################
1767 # Framework to run all checks 1873 # Framework to run all checks
1768 ############################################################################## 1874 ########################################################################
1769 1875
1770 1876
1771 class Checker(object): 1877 class Checker(object):
1772 """Load a Python source file, tokenize it, check coding style.""" 1878 """Load a Python source file, tokenize it, check coding style."""
1773 1879
1780 self._io_error = None 1886 self._io_error = None
1781 self._physical_checks = options.physical_checks 1887 self._physical_checks = options.physical_checks
1782 self._logical_checks = options.logical_checks 1888 self._logical_checks = options.logical_checks
1783 self._ast_checks = options.ast_checks 1889 self._ast_checks = options.ast_checks
1784 self.max_line_length = options.max_line_length 1890 self.max_line_length = options.max_line_length
1891 self.max_doc_length = options.max_doc_length
1785 self.multiline = False # in a multiline string? 1892 self.multiline = False # in a multiline string?
1786 self.hang_closing = options.hang_closing 1893 self.hang_closing = options.hang_closing
1787 self.verbose = options.verbose 1894 self.verbose = options.verbose
1788 self.filename = filename 1895 self.filename = filename
1789 # Dictionary where a checker can store its custom state. 1896 # Dictionary where a checker can store its custom state.
1951 lineno = args[0] 2058 lineno = args[0]
1952 if not self.lines or not noqa(self.lines[lineno - 1]): 2059 if not self.lines or not noqa(self.lines[lineno - 1]):
1953 self.report_error_args(lineno, *args[1:]) 2060 self.report_error_args(lineno, *args[1:])
1954 2061
1955 def generate_tokens(self): 2062 def generate_tokens(self):
1956 """Tokenize the file, run physical line checks and yield tokens.""" 2063 """Tokenize file, run physical line checks and yield tokens."""
1957 if self._io_error: 2064 if self._io_error:
1958 self.report_error_args(1, 0, 'E902', self._io_error, readlines) 2065 self.report_error_args(1, 0, 'E902', self._io_error, readlines)
1959 tokengen = tokenize.generate_tokens(self.readline) 2066 tokengen = tokenize.generate_tokens(self.readline)
1960 try: 2067 try:
1961 for token in tokengen: 2068 for token in tokengen:
1966 yield token 2073 yield token
1967 except (SyntaxError, tokenize.TokenError): 2074 except (SyntaxError, tokenize.TokenError):
1968 self.report_invalid_syntax() 2075 self.report_invalid_syntax()
1969 2076
1970 def maybe_check_physical(self, token): 2077 def maybe_check_physical(self, token):
1971 """If appropriate (based on token), check current physical line(s).""" 2078 """If appropriate for token, check current physical line(s)."""
1972 # Called after every token, but act only on end of line. 2079 # Called after every token, but act only on end of line.
1973 if _is_eol_token(token): 2080 if _is_eol_token(token):
1974 # Obviously, a newline token ends a single physical line. 2081 # Obviously, a newline token ends a single physical line.
1975 self.check_physical(token[4]) 2082 self.check_physical(token[4])
1976 elif token[0] == tokenize.STRING and '\n' in token[1]: 2083 elif token[0] == tokenize.STRING and '\n' in token[1]:
1977 # Less obviously, a string that contains newlines is a 2084 # Less obviously, a string that contains newlines is a
1978 # multiline string, either triple-quoted or with internal 2085 # multiline string, either triple-quoted or with internal
1979 # newlines backslash-escaped. Check every physical line in the 2086 # newlines backslash-escaped. Check every physical line in
1980 # string *except* for the last one: its newline is outside of 2087 # the string *except* for the last one: its newline is
1981 # the multiline string, so we consider it a regular physical 2088 # outside of the multiline string, so we consider it a
1982 # line, and will check it like any other physical line. 2089 # regular physical line, and will check it like any other
2090 # physical line.
1983 # 2091 #
1984 # Subtleties: 2092 # Subtleties:
1985 # - we don't *completely* ignore the last line; if it contains 2093 # - we don't *completely* ignore the last line; if it
1986 # the magical "# noqa" comment, we disable all physical 2094 # contains the magical "# noqa" comment, we disable all
1987 # checks for the entire multiline string 2095 # physical checks for the entire multiline string
1988 # - have to wind self.line_number back because initially it 2096 # - have to wind self.line_number back because initially it
1989 # points to the last line of the string, and we want 2097 # points to the last line of the string, and we want
1990 # check_physical() to give accurate feedback 2098 # check_physical() to give accurate feedback
1991 if noqa(token[4]): 2099 if noqa(token[4]):
1992 return 2100 return
2037 # The physical line contains only this token. 2145 # The physical line contains only this token.
2038 self.blank_lines += 1 2146 self.blank_lines += 1
2039 del self.tokens[0] 2147 del self.tokens[0]
2040 else: 2148 else:
2041 self.check_logical() 2149 self.check_logical()
2042 elif COMMENT_WITH_NL and token_type == tokenize.COMMENT:
2043 if len(self.tokens) == 1:
2044 # The comment also ends a physical line
2045 token = list(token)
2046 token[1] = text.rstrip('\r\n')
2047 token[3] = (token[2][0], token[2][1] + len(token[1]))
2048 self.tokens = [tuple(token)]
2049 self.check_logical()
2050 if self.tokens: 2150 if self.tokens:
2051 self.check_physical(self.lines[-1]) 2151 self.check_physical(self.lines[-1])
2052 self.check_logical() 2152 self.check_logical()
2053 return self.report.get_file_results() 2153 return self.report.get_file_results()
2054 2154
2131 """Return the count of errors and warnings for this file.""" 2231 """Return the count of errors and warnings for this file."""
2132 return self.file_errors 2232 return self.file_errors
2133 2233
2134 def get_count(self, prefix=''): 2234 def get_count(self, prefix=''):
2135 """Return the total count of errors and warnings.""" 2235 """Return the total count of errors and warnings."""
2136 return sum([self.counters[key] 2236 return sum(self.counters[key]
2137 for key in self.messages if key.startswith(prefix)]) 2237 for key in self.messages if key.startswith(prefix))
2138 2238
2139 def get_statistics(self, prefix=''): 2239 def get_statistics(self, prefix=''):
2140 """Get statistics for message codes that start with the prefix. 2240 """Get statistics for message codes that start with the prefix.
2141 2241
2142 prefix='' matches all errors and warnings 2242 prefix='' matches all errors and warnings
2161 (self.counters[key] / self.elapsed, key, 2261 (self.counters[key] / self.elapsed, key,
2162 self.counters[key])) 2262 self.counters[key]))
2163 2263
2164 2264
2165 class FileReport(BaseReport): 2265 class FileReport(BaseReport):
2166 """Collect the results of the checks and print only the filenames.""" 2266 """Collect the results of the checks and print the filenames."""
2167 2267
2168 print_filename = True 2268 print_filename = True
2169 2269
2170 2270
2171 class StandardReport(BaseReport): 2271 class StandardReport(BaseReport):
2202 self._deferred_print.append( 2302 self._deferred_print.append(
2203 (line_number, offset, code, args, check.__doc__)) 2303 (line_number, offset, code, args, check.__doc__))
2204 return code 2304 return code
2205 2305
2206 def get_file_results(self): 2306 def get_file_results(self):
2207 """Print the result and return the overall count for this file.""" 2307 """Print results and return the overall count for this file."""
2208 self._deferred_print.sort() 2308 self._deferred_print.sort()
2209 for line_number, offset, code, text, doc in self._deferred_print: 2309 for line_number, offset, code, text, doc in self._deferred_print:
2210 print(self._fmt % { 2310 print(self._fmt % {
2211 'path': self.filename, 2311 'path': self.filename,
2212 'row': self.line_offset + line_number, 'col': offset + 1, 2312 'row': self.line_offset + line_number, 'col': offset + 1,
2221 print(re.sub(r'\S', ' ', line[:offset]) + '^') 2321 print(re.sub(r'\S', ' ', line[:offset]) + '^')
2222 if self._show_pep8 and doc: 2322 if self._show_pep8 and doc:
2223 print(' ' + doc.strip()) 2323 print(' ' + doc.strip())
2224 2324
2225 # stdout is block buffered when not stdout.isatty(). 2325 # stdout is block buffered when not stdout.isatty().
2226 # line can be broken where buffer boundary since other processes 2326 # line can be broken where buffer boundary since other
2227 # write to same file. 2327 # processes write to same file.
2228 # flush() after print() to avoid buffer boundary. 2328 # flush() after print() to avoid buffer boundary.
2229 # Typical buffer size is 8192. line written safely when 2329 # Typical buffer size is 8192. line written safely when
2230 # len(line) < 8192. 2330 # len(line) < 8192.
2231 sys.stdout.flush() 2331 sys.stdout.flush()
2232 return self.file_errors 2332 return self.file_errors
2343 runner(os.path.join(root, filename)) 2443 runner(os.path.join(root, filename))
2344 2444
2345 def excluded(self, filename, parent=None): 2445 def excluded(self, filename, parent=None):
2346 """Check if the file should be excluded. 2446 """Check if the file should be excluded.
2347 2447
2348 Check if 'options.exclude' contains a pattern that matches filename. 2448 Check if 'options.exclude' contains a pattern matching filename.
2349 """ 2449 """
2350 if not self.options.exclude: 2450 if not self.options.exclude:
2351 return False 2451 return False
2352 basename = os.path.basename(filename) 2452 basename = os.path.basename(filename)
2353 if filename_match(basename, self.options.exclude): 2453 if filename_match(basename, self.options.exclude):
2371 not code.startswith(self.options.select)) 2471 not code.startswith(self.options.select))
2372 2472
2373 def get_checks(self, argument_name): 2473 def get_checks(self, argument_name):
2374 """Get all the checks for this category. 2474 """Get all the checks for this category.
2375 2475
2376 Find all globally visible functions where the first argument name 2476 Find all globally visible functions where the first argument
2377 starts with argument_name and which contain selected tests. 2477 name starts with argument_name and which contain selected tests.
2378 """ 2478 """
2379 checks = [] 2479 checks = []
2380 for check, attrs in _checks[argument_name].items(): 2480 for check, attrs in _checks[argument_name].items():
2381 (codes, args) = attrs 2481 (codes, args) = attrs
2382 if any(not (code and self.ignore_code(code)) for code in codes): 2482 if any(not (code and self.ignore_code(code)) for code in codes):
2388 """Create the parser for the program.""" 2488 """Create the parser for the program."""
2389 parser = OptionParser(prog=prog, version=version, 2489 parser = OptionParser(prog=prog, version=version,
2390 usage="%prog [options] input ...") 2490 usage="%prog [options] input ...")
2391 parser.config_options = [ 2491 parser.config_options = [
2392 'exclude', 'filename', 'select', 'ignore', 'max-line-length', 2492 'exclude', 'filename', 'select', 'ignore', 'max-line-length',
2393 'hang-closing', 'count', 'format', 'quiet', 'show-pep8', 2493 'max-doc-length', 'hang-closing', 'count', 'format', 'quiet',
2394 'show-source', 'statistics', 'verbose'] 2494 'show-pep8', 'show-source', 'statistics', 'verbose']
2395 parser.add_option('-v', '--verbose', default=0, action='count', 2495 parser.add_option('-v', '--verbose', default=0, action='count',
2396 help="print status messages, or debug with -vv") 2496 help="print status messages, or debug with -vv")
2397 parser.add_option('-q', '--quiet', default=0, action='count', 2497 parser.add_option('-q', '--quiet', default=0, action='count',
2398 help="report only file names, or nothing with -qq") 2498 help="report only file names, or nothing with -qq")
2399 parser.add_option('-r', '--repeat', default=True, action='store_true', 2499 parser.add_option('-r', '--repeat', default=True, action='store_true',
2425 "total is not null") 2525 "total is not null")
2426 parser.add_option('--max-line-length', type='int', metavar='n', 2526 parser.add_option('--max-line-length', type='int', metavar='n',
2427 default=MAX_LINE_LENGTH, 2527 default=MAX_LINE_LENGTH,
2428 help="set maximum allowed line length " 2528 help="set maximum allowed line length "
2429 "(default: %default)") 2529 "(default: %default)")
2530 parser.add_option('--max-doc-length', type='int', metavar='n',
2531 default=None,
2532 help="set maximum allowed doc line length and perform "
2533 "these checks (unchecked if not set)")
2430 parser.add_option('--hang-closing', action='store_true', 2534 parser.add_option('--hang-closing', action='store_true',
2431 help="hang closing bracket instead of matching " 2535 help="hang closing bracket instead of matching "
2432 "indentation of opening bracket's line") 2536 "indentation of opening bracket's line")
2433 parser.add_option('--format', metavar='format', default='default', 2537 parser.add_option('--format', metavar='format', default='default',
2434 help="set the error format [default|pylint|<custom>]") 2538 help="set the error format [default|pylint|<custom>]")
2447 2551
2448 2552
2449 def read_config(options, args, arglist, parser): 2553 def read_config(options, args, arglist, parser):
2450 """Read and parse configurations. 2554 """Read and parse configurations.
2451 2555
2452 If a config file is specified on the command line with the "--config" 2556 If a config file is specified on the command line with the
2453 option, then only it is used for configuration. 2557 "--config" option, then only it is used for configuration.
2454 2558
2455 Otherwise, the user configuration (~/.config/pycodestyle) and any local 2559 Otherwise, the user configuration (~/.config/pycodestyle) and any
2456 configurations in the current directory or above will be merged together 2560 local configurations in the current directory or above will be
2457 (in that order) using the read method of ConfigParser. 2561 merged together (in that order) using the read method of
2562 ConfigParser.
2458 """ 2563 """
2459 config = RawConfigParser() 2564 config = RawConfigParser()
2460 2565
2461 cli_conf = options.config 2566 cli_conf = options.config
2462 2567
2487 elif config.has_section('pep8'): 2592 elif config.has_section('pep8'):
2488 pycodestyle_section = 'pep8' # Deprecated 2593 pycodestyle_section = 'pep8' # Deprecated
2489 warnings.warn('[pep8] section is deprecated. Use [pycodestyle].') 2594 warnings.warn('[pep8] section is deprecated. Use [pycodestyle].')
2490 2595
2491 if pycodestyle_section: 2596 if pycodestyle_section:
2492 option_list = dict([(o.dest, o.type or o.action) 2597 option_list = {o.dest: o.type or o.action for o in parser.option_list}
2493 for o in parser.option_list])
2494 2598
2495 # First, read the default values 2599 # First, read the default values
2496 (new_options, __) = parser.parse_args([]) 2600 (new_options, __) = parser.parse_args([])
2497 2601
2498 # Second, parse the configuration 2602 # Second, parse the configuration
2521 return options 2625 return options
2522 2626
2523 2627
2524 def process_options(arglist=None, parse_argv=False, config_file=None, 2628 def process_options(arglist=None, parse_argv=False, config_file=None,
2525 parser=None, verbose=None): 2629 parser=None, verbose=None):
2526 """Process options passed either via arglist or via command line args. 2630 """Process options passed either via arglist or command line args.
2527 2631
2528 Passing in the ``config_file`` parameter allows other tools, such as flake8 2632 Passing in the ``config_file`` parameter allows other tools, such as
2529 to specify their own options to be processed in pycodestyle. 2633 flake8 to specify their own options to be processed in pycodestyle.
2530 """ 2634 """
2531 if not parser: 2635 if not parser:
2532 parser = get_parser() 2636 parser = get_parser()
2533 if not parser.has_option('--config'): 2637 if not parser.has_option('--config'):
2534 group = parser.add_option_group("Configuration", description=( 2638 group = parser.add_option_group("Configuration", description=(
2544 # If parse_argv is True and arglist is None, arguments are 2648 # If parse_argv is True and arglist is None, arguments are
2545 # parsed from the command line (sys.argv) 2649 # parsed from the command line (sys.argv)
2546 (options, args) = parser.parse_args(arglist) 2650 (options, args) = parser.parse_args(arglist)
2547 options.reporter = None 2651 options.reporter = None
2548 2652
2549 # If explicity specified verbosity, override any `-v` CLI flag 2653 # If explicitly specified verbosity, override any `-v` CLI flag
2550 if verbose is not None: 2654 if verbose is not None:
2551 options.verbose = verbose 2655 options.verbose = verbose
2552 2656
2553 if options.ensure_value('testsuite', False): 2657 if options.ensure_value('testsuite', False):
2554 args.append(options.testsuite) 2658 args.append(options.testsuite)

eric ide

mercurial