PageRenderTime 64ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/python/helpers/pydev/third_party/pep8/autopep8.py

http://github.com/JetBrains/intellij-community
Python | 3827 lines | 3787 code | 5 blank | 35 comment | 7 complexity | 2d5b239baef85c8d2be553f23f1cd584 MD5 | raw file
Possible License(s): BSD-3-Clause, Apache-2.0, MPL-2.0-no-copyleft-exception, MIT, EPL-1.0, AGPL-1.0

Large files files are truncated, but you can click here to view the full file

  1. #!/usr/bin/env python
  2. # Copyright (C) 2010-2011 Hideo Hattori
  3. # Copyright (C) 2011-2013 Hideo Hattori, Steven Myint
  4. # Copyright (C) 2013-2016 Hideo Hattori, Steven Myint, Bill Wendling
  5. #
  6. # Permission is hereby granted, free of charge, to any person obtaining
  7. # a copy of this software and associated documentation files (the
  8. # "Software"), to deal in the Software without restriction, including
  9. # without limitation the rights to use, copy, modify, merge, publish,
  10. # distribute, sublicense, and/or sell copies of the Software, and to
  11. # permit persons to whom the Software is furnished to do so, subject to
  12. # the following conditions:
  13. #
  14. # The above copyright notice and this permission notice shall be
  15. # included in all copies or substantial portions of the Software.
  16. #
  17. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  21. # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  22. # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  23. # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  24. # SOFTWARE.
  25. """Automatically formats Python code to conform to the PEP 8 style guide.
  26. Fixes that only need be done once can be added by adding a function of the form
  27. "fix_<code>(source)" to this module. They should return the fixed source code.
  28. These fixes are picked up by apply_global_fixes().
  29. Fixes that depend on pycodestyle should be added as methods to FixPEP8. See the
  30. class documentation for more information.
  31. """
  32. from __future__ import absolute_import
  33. from __future__ import division
  34. from __future__ import print_function
  35. from __future__ import unicode_literals
  36. import codecs
  37. import collections
  38. import copy
  39. import difflib
  40. import fnmatch
  41. import inspect
  42. import io
  43. import keyword
  44. import locale
  45. import os
  46. import re
  47. import signal
  48. import sys
  49. import textwrap
  50. import token
  51. import tokenize
  52. import pycodestyle
  53. def check_lib2to3():
  54. try:
  55. import lib2to3
  56. except ImportError:
  57. sys.path.append(os.path.join(os.path.dirname(__file__), 'lib2to3'))
  58. import lib2to3
  59. try:
  60. unicode
  61. except NameError:
  62. unicode = str
  63. __version__ = '1.3'
  64. CR = '\r'
  65. LF = '\n'
  66. CRLF = '\r\n'
  67. PYTHON_SHEBANG_REGEX = re.compile(r'^#!.*\bpython[23]?\b\s*$')
  68. LAMBDA_REGEX = re.compile(r'([\w.]+)\s=\slambda\s*([\(\)\w,\s.]*):')
  69. COMPARE_NEGATIVE_REGEX = re.compile(r'\b(not)\s+([^][)(}{]+)\s+(in|is)\s')
  70. BARE_EXCEPT_REGEX = re.compile(r'except\s*:')
  71. STARTSWITH_DEF_REGEX = re.compile(r'^(async\s+def|def)\s.*\):')
  72. # For generating line shortening candidates.
  73. SHORTEN_OPERATOR_GROUPS = frozenset([
  74. frozenset([',']),
  75. frozenset(['%']),
  76. frozenset([',', '(', '[', '{']),
  77. frozenset(['%', '(', '[', '{']),
  78. frozenset([',', '(', '[', '{', '%', '+', '-', '*', '/', '//']),
  79. frozenset(['%', '+', '-', '*', '/', '//']),
  80. ])
  81. DEFAULT_IGNORE = 'E24,W503'
  82. DEFAULT_INDENT_SIZE = 4
  83. # W602 is handled separately due to the need to avoid "with_traceback".
  84. CODE_TO_2TO3 = {
  85. 'E231': ['ws_comma'],
  86. 'E721': ['idioms'],
  87. 'W601': ['has_key'],
  88. 'W603': ['ne'],
  89. 'W604': ['repr'],
  90. 'W690': ['apply',
  91. 'except',
  92. 'exitfunc',
  93. 'numliterals',
  94. 'operator',
  95. 'paren',
  96. 'reduce',
  97. 'renames',
  98. 'standarderror',
  99. 'sys_exc',
  100. 'throw',
  101. 'tuple_params',
  102. 'xreadlines']}
  103. if sys.platform == 'win32': # pragma: no cover
  104. DEFAULT_CONFIG = os.path.expanduser(r'~\.pep8')
  105. else:
  106. DEFAULT_CONFIG = os.path.join(os.getenv('XDG_CONFIG_HOME') or
  107. os.path.expanduser('~/.config'), 'pep8')
  108. PROJECT_CONFIG = ('setup.cfg', 'tox.ini', '.pep8')
  109. MAX_PYTHON_FILE_DETECTION_BYTES = 1024
  110. def open_with_encoding(filename,
  111. encoding=None, mode='r', limit_byte_check=-1):
  112. """Return opened file with a specific encoding."""
  113. if not encoding:
  114. encoding = detect_encoding(filename, limit_byte_check=limit_byte_check)
  115. return io.open(filename, mode=mode, encoding=encoding,
  116. newline='') # Preserve line endings
  117. def detect_encoding(filename, limit_byte_check=-1):
  118. """Return file encoding."""
  119. try:
  120. with open(filename, 'rb') as input_file:
  121. from lib2to3.pgen2 import tokenize as lib2to3_tokenize
  122. encoding = lib2to3_tokenize.detect_encoding(input_file.readline)[0]
  123. with open_with_encoding(filename, encoding) as test_file:
  124. test_file.read(limit_byte_check)
  125. return encoding
  126. except (LookupError, SyntaxError, UnicodeDecodeError):
  127. return 'latin-1'
  128. def readlines_from_file(filename):
  129. """Return contents of file."""
  130. with open_with_encoding(filename) as input_file:
  131. return input_file.readlines()
  132. def extended_blank_lines(logical_line,
  133. blank_lines,
  134. blank_before,
  135. indent_level,
  136. previous_logical):
  137. """Check for missing blank lines after class declaration."""
  138. if previous_logical.startswith('def '):
  139. if blank_lines and pycodestyle.DOCSTRING_REGEX.match(logical_line):
  140. yield (0, 'E303 too many blank lines ({0})'.format(blank_lines))
  141. elif pycodestyle.DOCSTRING_REGEX.match(previous_logical):
  142. # Missing blank line between class docstring and method declaration.
  143. if (
  144. indent_level and
  145. not blank_lines and
  146. not blank_before and
  147. logical_line.startswith(('def ')) and
  148. '(self' in logical_line
  149. ):
  150. yield (0, 'E301 expected 1 blank line, found 0')
  151. pycodestyle.register_check(extended_blank_lines)
  152. def continued_indentation(logical_line, tokens, indent_level, indent_char,
  153. noqa):
  154. """Override pycodestyle's function to provide indentation information."""
  155. first_row = tokens[0][2][0]
  156. nrows = 1 + tokens[-1][2][0] - first_row
  157. if noqa or nrows == 1:
  158. return
  159. # indent_next tells us whether the next block is indented. Assuming
  160. # that it is indented by 4 spaces, then we should not allow 4-space
  161. # indents on the final continuation line. In turn, some other
  162. # indents are allowed to have an extra 4 spaces.
  163. indent_next = logical_line.endswith(':')
  164. row = depth = 0
  165. valid_hangs = (
  166. (DEFAULT_INDENT_SIZE,)
  167. if indent_char != '\t' else (DEFAULT_INDENT_SIZE,
  168. 2 * DEFAULT_INDENT_SIZE)
  169. )
  170. # Remember how many brackets were opened on each line.
  171. parens = [0] * nrows
  172. # Relative indents of physical lines.
  173. rel_indent = [0] * nrows
  174. # For each depth, collect a list of opening rows.
  175. open_rows = [[0]]
  176. # For each depth, memorize the hanging indentation.
  177. hangs = [None]
  178. # Visual indents.
  179. indent_chances = {}
  180. last_indent = tokens[0][2]
  181. indent = [last_indent[1]]
  182. last_token_multiline = None
  183. line = None
  184. last_line = ''
  185. last_line_begins_with_multiline = False
  186. for token_type, text, start, end, line in tokens:
  187. newline = row < start[0] - first_row
  188. if newline:
  189. row = start[0] - first_row
  190. newline = (not last_token_multiline and
  191. token_type not in (tokenize.NL, tokenize.NEWLINE))
  192. last_line_begins_with_multiline = last_token_multiline
  193. if newline:
  194. # This is the beginning of a continuation line.
  195. last_indent = start
  196. # Record the initial indent.
  197. rel_indent[row] = pycodestyle.expand_indent(line) - indent_level
  198. # Identify closing bracket.
  199. close_bracket = (token_type == tokenize.OP and text in ']})')
  200. # Is the indent relative to an opening bracket line?
  201. for open_row in reversed(open_rows[depth]):
  202. hang = rel_indent[row] - rel_indent[open_row]
  203. hanging_indent = hang in valid_hangs
  204. if hanging_indent:
  205. break
  206. if hangs[depth]:
  207. hanging_indent = (hang == hangs[depth])
  208. visual_indent = (not close_bracket and hang > 0 and
  209. indent_chances.get(start[1]))
  210. if close_bracket and indent[depth]:
  211. # Closing bracket for visual indent.
  212. if start[1] != indent[depth]:
  213. yield (start, 'E124 {0}'.format(indent[depth]))
  214. elif close_bracket and not hang:
  215. pass
  216. elif indent[depth] and start[1] < indent[depth]:
  217. # Visual indent is broken.
  218. yield (start, 'E128 {0}'.format(indent[depth]))
  219. elif (hanging_indent or
  220. (indent_next and
  221. rel_indent[row] == 2 * DEFAULT_INDENT_SIZE)):
  222. # Hanging indent is verified.
  223. if close_bracket:
  224. yield (start, 'E123 {0}'.format(indent_level +
  225. rel_indent[open_row]))
  226. hangs[depth] = hang
  227. elif visual_indent is True:
  228. # Visual indent is verified.
  229. indent[depth] = start[1]
  230. elif visual_indent in (text, unicode):
  231. # Ignore token lined up with matching one from a previous line.
  232. pass
  233. else:
  234. one_indented = (indent_level + rel_indent[open_row] +
  235. DEFAULT_INDENT_SIZE)
  236. # Indent is broken.
  237. if hang <= 0:
  238. error = ('E122', one_indented)
  239. elif indent[depth]:
  240. error = ('E127', indent[depth])
  241. elif not close_bracket and hangs[depth]:
  242. error = ('E131', one_indented)
  243. elif hang > DEFAULT_INDENT_SIZE:
  244. error = ('E126', one_indented)
  245. else:
  246. hangs[depth] = hang
  247. error = ('E121', one_indented)
  248. yield (start, '{0} {1}'.format(*error))
  249. # Look for visual indenting.
  250. if (
  251. parens[row] and
  252. token_type not in (tokenize.NL, tokenize.COMMENT) and
  253. not indent[depth]
  254. ):
  255. indent[depth] = start[1]
  256. indent_chances[start[1]] = True
  257. # Deal with implicit string concatenation.
  258. elif (token_type in (tokenize.STRING, tokenize.COMMENT) or
  259. text in ('u', 'ur', 'b', 'br')):
  260. indent_chances[start[1]] = unicode
  261. # Special case for the "if" statement because len("if (") is equal to
  262. # 4.
  263. elif not indent_chances and not row and not depth and text == 'if':
  264. indent_chances[end[1] + 1] = True
  265. elif text == ':' and line[end[1]:].isspace():
  266. open_rows[depth].append(row)
  267. # Keep track of bracket depth.
  268. if token_type == tokenize.OP:
  269. if text in '([{':
  270. depth += 1
  271. indent.append(0)
  272. hangs.append(None)
  273. if len(open_rows) == depth:
  274. open_rows.append([])
  275. open_rows[depth].append(row)
  276. parens[row] += 1
  277. elif text in ')]}' and depth > 0:
  278. # Parent indents should not be more than this one.
  279. prev_indent = indent.pop() or last_indent[1]
  280. hangs.pop()
  281. for d in range(depth):
  282. if indent[d] > prev_indent:
  283. indent[d] = 0
  284. for ind in list(indent_chances):
  285. if ind >= prev_indent:
  286. del indent_chances[ind]
  287. del open_rows[depth + 1:]
  288. depth -= 1
  289. if depth:
  290. indent_chances[indent[depth]] = True
  291. for idx in range(row, -1, -1):
  292. if parens[idx]:
  293. parens[idx] -= 1
  294. break
  295. assert len(indent) == depth + 1
  296. if (
  297. start[1] not in indent_chances and
  298. # This is for purposes of speeding up E121 (GitHub #90).
  299. not last_line.rstrip().endswith(',')
  300. ):
  301. # Allow to line up tokens.
  302. indent_chances[start[1]] = text
  303. last_token_multiline = (start[0] != end[0])
  304. if last_token_multiline:
  305. rel_indent[end[0] - first_row] = rel_indent[row]
  306. last_line = line
  307. if (
  308. indent_next and
  309. not last_line_begins_with_multiline and
  310. pycodestyle.expand_indent(line) == indent_level + DEFAULT_INDENT_SIZE
  311. ):
  312. pos = (start[0], indent[0] + 4)
  313. desired_indent = indent_level + 2 * DEFAULT_INDENT_SIZE
  314. if visual_indent:
  315. yield (pos, 'E129 {0}'.format(desired_indent))
  316. else:
  317. yield (pos, 'E125 {0}'.format(desired_indent))
  318. del pycodestyle._checks['logical_line'][pycodestyle.continued_indentation]
  319. pycodestyle.register_check(continued_indentation)
  320. class FixPEP8(object):
  321. """Fix invalid code.
  322. Fixer methods are prefixed "fix_". The _fix_source() method looks for these
  323. automatically.
  324. The fixer method can take either one or two arguments (in addition to
  325. self). The first argument is "result", which is the error information from
  326. pycodestyle. The second argument, "logical", is required only for
  327. logical-line fixes.
  328. The fixer method can return the list of modified lines or None. An empty
  329. list would mean that no changes were made. None would mean that only the
  330. line reported in the pycodestyle error was modified. Note that the modified
  331. line numbers that are returned are indexed at 1. This typically would
  332. correspond with the line number reported in the pycodestyle error
  333. information.
  334. [fixed method list]
  335. - e111,e114,e115,e116
  336. - e121,e122,e123,e124,e125,e126,e127,e128,e129
  337. - e201,e202,e203
  338. - e211
  339. - e221,e222,e223,e224,e225
  340. - e231
  341. - e251
  342. - e261,e262
  343. - e271,e272,e273,e274
  344. - e301,e302,e303,e304,e306
  345. - e401
  346. - e502
  347. - e701,e702,e703,e704
  348. - e711,e712,e713,e714
  349. - e722
  350. - e731
  351. - w291
  352. - w503
  353. """
  354. def __init__(self, filename,
  355. options,
  356. contents=None,
  357. long_line_ignore_cache=None):
  358. self.filename = filename
  359. if contents is None:
  360. self.source = readlines_from_file(filename)
  361. else:
  362. sio = io.StringIO(contents)
  363. self.source = sio.readlines()
  364. self.options = options
  365. self.indent_word = _get_indentword(''.join(self.source))
  366. self.long_line_ignore_cache = (
  367. set() if long_line_ignore_cache is None
  368. else long_line_ignore_cache)
  369. # Many fixers are the same even though pycodestyle categorizes them
  370. # differently.
  371. self.fix_e115 = self.fix_e112
  372. self.fix_e116 = self.fix_e113
  373. self.fix_e121 = self._fix_reindent
  374. self.fix_e122 = self._fix_reindent
  375. self.fix_e123 = self._fix_reindent
  376. self.fix_e124 = self._fix_reindent
  377. self.fix_e126 = self._fix_reindent
  378. self.fix_e127 = self._fix_reindent
  379. self.fix_e128 = self._fix_reindent
  380. self.fix_e129 = self._fix_reindent
  381. self.fix_e202 = self.fix_e201
  382. self.fix_e203 = self.fix_e201
  383. self.fix_e211 = self.fix_e201
  384. self.fix_e221 = self.fix_e271
  385. self.fix_e222 = self.fix_e271
  386. self.fix_e223 = self.fix_e271
  387. self.fix_e226 = self.fix_e225
  388. self.fix_e227 = self.fix_e225
  389. self.fix_e228 = self.fix_e225
  390. self.fix_e241 = self.fix_e271
  391. self.fix_e242 = self.fix_e224
  392. self.fix_e261 = self.fix_e262
  393. self.fix_e272 = self.fix_e271
  394. self.fix_e273 = self.fix_e271
  395. self.fix_e274 = self.fix_e271
  396. self.fix_e306 = self.fix_e301
  397. self.fix_e501 = (
  398. self.fix_long_line_logically if
  399. options and (options.aggressive >= 2 or options.experimental) else
  400. self.fix_long_line_physically)
  401. self.fix_e703 = self.fix_e702
  402. self.fix_w293 = self.fix_w291
  403. def _fix_source(self, results):
  404. try:
  405. (logical_start, logical_end) = _find_logical(self.source)
  406. logical_support = True
  407. except (SyntaxError, tokenize.TokenError): # pragma: no cover
  408. logical_support = False
  409. completed_lines = set()
  410. for result in sorted(results, key=_priority_key):
  411. if result['line'] in completed_lines:
  412. continue
  413. fixed_methodname = 'fix_' + result['id'].lower()
  414. if hasattr(self, fixed_methodname):
  415. fix = getattr(self, fixed_methodname)
  416. line_index = result['line'] - 1
  417. original_line = self.source[line_index]
  418. is_logical_fix = len(_get_parameters(fix)) > 2
  419. if is_logical_fix:
  420. logical = None
  421. if logical_support:
  422. logical = _get_logical(self.source,
  423. result,
  424. logical_start,
  425. logical_end)
  426. if logical and set(range(
  427. logical[0][0] + 1,
  428. logical[1][0] + 1)).intersection(
  429. completed_lines):
  430. continue
  431. modified_lines = fix(result, logical)
  432. else:
  433. modified_lines = fix(result)
  434. if modified_lines is None:
  435. # Force logical fixes to report what they modified.
  436. assert not is_logical_fix
  437. if self.source[line_index] == original_line:
  438. modified_lines = []
  439. if modified_lines:
  440. completed_lines.update(modified_lines)
  441. elif modified_lines == []: # Empty list means no fix
  442. if self.options.verbose >= 2:
  443. print(
  444. '---> Not fixing {error} on line {line}'.format(
  445. error=result['id'], line=result['line']),
  446. file=sys.stderr)
  447. else: # We assume one-line fix when None.
  448. completed_lines.add(result['line'])
  449. else:
  450. if self.options.verbose >= 3:
  451. print(
  452. "---> '{0}' is not defined.".format(fixed_methodname),
  453. file=sys.stderr)
  454. info = result['info'].strip()
  455. print('---> {0}:{1}:{2}:{3}'.format(self.filename,
  456. result['line'],
  457. result['column'],
  458. info),
  459. file=sys.stderr)
  460. def fix(self):
  461. """Return a version of the source code with PEP 8 violations fixed."""
  462. pep8_options = {
  463. 'ignore': self.options.ignore,
  464. 'select': self.options.select,
  465. 'max_line_length': self.options.max_line_length,
  466. }
  467. results = _execute_pep8(pep8_options, self.source)
  468. if self.options.verbose:
  469. progress = {}
  470. for r in results:
  471. if r['id'] not in progress:
  472. progress[r['id']] = set()
  473. progress[r['id']].add(r['line'])
  474. print('---> {n} issue(s) to fix {progress}'.format(
  475. n=len(results), progress=progress), file=sys.stderr)
  476. if self.options.line_range:
  477. start, end = self.options.line_range
  478. results = [r for r in results
  479. if start <= r['line'] <= end]
  480. self._fix_source(filter_results(source=''.join(self.source),
  481. results=results,
  482. aggressive=self.options.aggressive))
  483. if self.options.line_range:
  484. # If number of lines has changed then change line_range.
  485. count = sum(sline.count('\n')
  486. for sline in self.source[start - 1:end])
  487. self.options.line_range[1] = start + count - 1
  488. return ''.join(self.source)
  489. def _fix_reindent(self, result):
  490. """Fix a badly indented line.
  491. This is done by adding or removing from its initial indent only.
  492. """
  493. num_indent_spaces = int(result['info'].split()[1])
  494. line_index = result['line'] - 1
  495. target = self.source[line_index]
  496. self.source[line_index] = ' ' * num_indent_spaces + target.lstrip()
  497. def fix_e112(self, result):
  498. """Fix under-indented comments."""
  499. line_index = result['line'] - 1
  500. target = self.source[line_index]
  501. if not target.lstrip().startswith('#'):
  502. # Don't screw with invalid syntax.
  503. return []
  504. self.source[line_index] = self.indent_word + target
  505. def fix_e113(self, result):
  506. """Fix over-indented comments."""
  507. line_index = result['line'] - 1
  508. target = self.source[line_index]
  509. indent = _get_indentation(target)
  510. stripped = target.lstrip()
  511. if not stripped.startswith('#'):
  512. # Don't screw with invalid syntax.
  513. return []
  514. self.source[line_index] = indent[1:] + stripped
  515. def fix_e125(self, result):
  516. """Fix indentation undistinguish from the next logical line."""
  517. num_indent_spaces = int(result['info'].split()[1])
  518. line_index = result['line'] - 1
  519. target = self.source[line_index]
  520. spaces_to_add = num_indent_spaces - len(_get_indentation(target))
  521. indent = len(_get_indentation(target))
  522. modified_lines = []
  523. while len(_get_indentation(self.source[line_index])) >= indent:
  524. self.source[line_index] = (' ' * spaces_to_add +
  525. self.source[line_index])
  526. modified_lines.append(1 + line_index) # Line indexed at 1.
  527. line_index -= 1
  528. return modified_lines
  529. def fix_e131(self, result):
  530. """Fix indentation undistinguish from the next logical line."""
  531. num_indent_spaces = int(result['info'].split()[1])
  532. line_index = result['line'] - 1
  533. target = self.source[line_index]
  534. spaces_to_add = num_indent_spaces - len(_get_indentation(target))
  535. if spaces_to_add >= 0:
  536. self.source[line_index] = (' ' * spaces_to_add +
  537. self.source[line_index])
  538. else:
  539. offset = abs(spaces_to_add)
  540. self.source[line_index] = self.source[line_index][offset:]
  541. def fix_e201(self, result):
  542. """Remove extraneous whitespace."""
  543. line_index = result['line'] - 1
  544. target = self.source[line_index]
  545. offset = result['column'] - 1
  546. fixed = fix_whitespace(target,
  547. offset=offset,
  548. replacement='')
  549. self.source[line_index] = fixed
  550. def fix_e224(self, result):
  551. """Remove extraneous whitespace around operator."""
  552. target = self.source[result['line'] - 1]
  553. offset = result['column'] - 1
  554. fixed = target[:offset] + target[offset:].replace('\t', ' ')
  555. self.source[result['line'] - 1] = fixed
  556. def fix_e225(self, result):
  557. """Fix missing whitespace around operator."""
  558. target = self.source[result['line'] - 1]
  559. offset = result['column'] - 1
  560. fixed = target[:offset] + ' ' + target[offset:]
  561. # Only proceed if non-whitespace characters match.
  562. # And make sure we don't break the indentation.
  563. if (
  564. fixed.replace(' ', '') == target.replace(' ', '') and
  565. _get_indentation(fixed) == _get_indentation(target)
  566. ):
  567. self.source[result['line'] - 1] = fixed
  568. else:
  569. return []
  570. def fix_e231(self, result):
  571. """Add missing whitespace."""
  572. line_index = result['line'] - 1
  573. target = self.source[line_index]
  574. offset = result['column']
  575. fixed = target[:offset].rstrip() + ' ' + target[offset:].lstrip()
  576. self.source[line_index] = fixed
  577. def fix_e251(self, result):
  578. """Remove whitespace around parameter '=' sign."""
  579. line_index = result['line'] - 1
  580. target = self.source[line_index]
  581. # This is necessary since pycodestyle sometimes reports columns that
  582. # goes past the end of the physical line. This happens in cases like,
  583. # foo(bar\n=None)
  584. c = min(result['column'] - 1,
  585. len(target) - 1)
  586. if target[c].strip():
  587. fixed = target
  588. else:
  589. fixed = target[:c].rstrip() + target[c:].lstrip()
  590. # There could be an escaped newline
  591. #
  592. # def foo(a=\
  593. # 1)
  594. if fixed.endswith(('=\\\n', '=\\\r\n', '=\\\r')):
  595. self.source[line_index] = fixed.rstrip('\n\r \t\\')
  596. self.source[line_index + 1] = self.source[line_index + 1].lstrip()
  597. return [line_index + 1, line_index + 2] # Line indexed at 1
  598. self.source[result['line'] - 1] = fixed
  599. def fix_e262(self, result):
  600. """Fix spacing after comment hash."""
  601. target = self.source[result['line'] - 1]
  602. offset = result['column']
  603. code = target[:offset].rstrip(' \t#')
  604. comment = target[offset:].lstrip(' \t#')
  605. fixed = code + (' # ' + comment if comment.strip() else '\n')
  606. self.source[result['line'] - 1] = fixed
  607. def fix_e271(self, result):
  608. """Fix extraneous whitespace around keywords."""
  609. line_index = result['line'] - 1
  610. target = self.source[line_index]
  611. offset = result['column'] - 1
  612. fixed = fix_whitespace(target,
  613. offset=offset,
  614. replacement=' ')
  615. if fixed == target:
  616. return []
  617. else:
  618. self.source[line_index] = fixed
  619. def fix_e301(self, result):
  620. """Add missing blank line."""
  621. cr = '\n'
  622. self.source[result['line'] - 1] = cr + self.source[result['line'] - 1]
  623. def fix_e302(self, result):
  624. """Add missing 2 blank lines."""
  625. add_linenum = 2 - int(result['info'].split()[-1])
  626. cr = '\n' * add_linenum
  627. self.source[result['line'] - 1] = cr + self.source[result['line'] - 1]
  628. def fix_e303(self, result):
  629. """Remove extra blank lines."""
  630. delete_linenum = int(result['info'].split('(')[1].split(')')[0]) - 2
  631. delete_linenum = max(1, delete_linenum)
  632. # We need to count because pycodestyle reports an offset line number if
  633. # there are comments.
  634. cnt = 0
  635. line = result['line'] - 2
  636. modified_lines = []
  637. while cnt < delete_linenum and line >= 0:
  638. if not self.source[line].strip():
  639. self.source[line] = ''
  640. modified_lines.append(1 + line) # Line indexed at 1
  641. cnt += 1
  642. line -= 1
  643. return modified_lines
  644. def fix_e304(self, result):
  645. """Remove blank line following function decorator."""
  646. line = result['line'] - 2
  647. if not self.source[line].strip():
  648. self.source[line] = ''
  649. def fix_e305(self, result):
  650. """Add missing 2 blank lines after end of function or class."""
  651. cr = '\n'
  652. # check comment line
  653. offset = result['line'] - 2
  654. while True:
  655. if offset < 0:
  656. break
  657. line = self.source[offset].lstrip()
  658. if len(line) == 0:
  659. break
  660. if line[0] != '#':
  661. break
  662. offset -= 1
  663. offset += 1
  664. self.source[offset] = cr + self.source[offset]
  665. def fix_e401(self, result):
  666. """Put imports on separate lines."""
  667. line_index = result['line'] - 1
  668. target = self.source[line_index]
  669. offset = result['column'] - 1
  670. if not target.lstrip().startswith('import'):
  671. return []
  672. indentation = re.split(pattern=r'\bimport\b',
  673. string=target, maxsplit=1)[0]
  674. fixed = (target[:offset].rstrip('\t ,') + '\n' +
  675. indentation + 'import ' + target[offset:].lstrip('\t ,'))
  676. self.source[line_index] = fixed
  677. def fix_long_line_logically(self, result, logical):
  678. """Try to make lines fit within --max-line-length characters."""
  679. if (
  680. not logical or
  681. len(logical[2]) == 1 or
  682. self.source[result['line'] - 1].lstrip().startswith('#')
  683. ):
  684. return self.fix_long_line_physically(result)
  685. start_line_index = logical[0][0]
  686. end_line_index = logical[1][0]
  687. logical_lines = logical[2]
  688. previous_line = get_item(self.source, start_line_index - 1, default='')
  689. next_line = get_item(self.source, end_line_index + 1, default='')
  690. single_line = join_logical_line(''.join(logical_lines))
  691. try:
  692. fixed = self.fix_long_line(
  693. target=single_line,
  694. previous_line=previous_line,
  695. next_line=next_line,
  696. original=''.join(logical_lines))
  697. except (SyntaxError, tokenize.TokenError):
  698. return self.fix_long_line_physically(result)
  699. if fixed:
  700. for line_index in range(start_line_index, end_line_index + 1):
  701. self.source[line_index] = ''
  702. self.source[start_line_index] = fixed
  703. return range(start_line_index + 1, end_line_index + 1)
  704. else:
  705. return []
  706. def fix_long_line_physically(self, result):
  707. """Try to make lines fit within --max-line-length characters."""
  708. line_index = result['line'] - 1
  709. target = self.source[line_index]
  710. previous_line = get_item(self.source, line_index - 1, default='')
  711. next_line = get_item(self.source, line_index + 1, default='')
  712. try:
  713. fixed = self.fix_long_line(
  714. target=target,
  715. previous_line=previous_line,
  716. next_line=next_line,
  717. original=target)
  718. except (SyntaxError, tokenize.TokenError):
  719. return []
  720. if fixed:
  721. self.source[line_index] = fixed
  722. return [line_index + 1]
  723. else:
  724. return []
  725. def fix_long_line(self, target, previous_line,
  726. next_line, original):
  727. cache_entry = (target, previous_line, next_line)
  728. if cache_entry in self.long_line_ignore_cache:
  729. return []
  730. if target.lstrip().startswith('#'):
  731. # Wrap commented lines.
  732. return shorten_comment(
  733. line=target,
  734. max_line_length=self.options.max_line_length,
  735. last_comment=not next_line.lstrip().startswith('#'))
  736. fixed = get_fixed_long_line(
  737. target=target,
  738. previous_line=previous_line,
  739. original=original,
  740. indent_word=self.indent_word,
  741. max_line_length=self.options.max_line_length,
  742. aggressive=self.options.aggressive,
  743. experimental=self.options.experimental,
  744. verbose=self.options.verbose)
  745. if fixed and not code_almost_equal(original, fixed):
  746. return fixed
  747. else:
  748. self.long_line_ignore_cache.add(cache_entry)
  749. return None
  750. def fix_e502(self, result):
  751. """Remove extraneous escape of newline."""
  752. (line_index, _, target) = get_index_offset_contents(result,
  753. self.source)
  754. self.source[line_index] = target.rstrip('\n\r \t\\') + '\n'
  755. def fix_e701(self, result):
  756. """Put colon-separated compound statement on separate lines."""
  757. line_index = result['line'] - 1
  758. target = self.source[line_index]
  759. c = result['column']
  760. fixed_source = (target[:c] + '\n' +
  761. _get_indentation(target) + self.indent_word +
  762. target[c:].lstrip('\n\r \t\\'))
  763. self.source[result['line'] - 1] = fixed_source
  764. return [result['line'], result['line'] + 1]
  765. def fix_e702(self, result, logical):
  766. """Put semicolon-separated compound statement on separate lines."""
  767. if not logical:
  768. return [] # pragma: no cover
  769. logical_lines = logical[2]
  770. line_index = result['line'] - 1
  771. target = self.source[line_index]
  772. if target.rstrip().endswith('\\'):
  773. # Normalize '1; \\\n2' into '1; 2'.
  774. self.source[line_index] = target.rstrip('\n \r\t\\')
  775. self.source[line_index + 1] = self.source[line_index + 1].lstrip()
  776. return [line_index + 1, line_index + 2]
  777. if target.rstrip().endswith(';'):
  778. self.source[line_index] = target.rstrip('\n \r\t;') + '\n'
  779. return [line_index + 1]
  780. offset = result['column'] - 1
  781. first = target[:offset].rstrip(';').rstrip()
  782. second = (_get_indentation(logical_lines[0]) +
  783. target[offset:].lstrip(';').lstrip())
  784. # Find inline comment.
  785. inline_comment = None
  786. if target[offset:].lstrip(';').lstrip()[:2] == '# ':
  787. inline_comment = target[offset:].lstrip(';')
  788. if inline_comment:
  789. self.source[line_index] = first + inline_comment
  790. else:
  791. self.source[line_index] = first + '\n' + second
  792. return [line_index + 1]
  793. def fix_e704(self, result):
  794. """Fix multiple statements on one line def"""
  795. (line_index, _, target) = get_index_offset_contents(result,
  796. self.source)
  797. match = STARTSWITH_DEF_REGEX.match(target)
  798. if match:
  799. self.source[line_index] = '{0}\n{1}{2}'.format(
  800. match.group(0),
  801. _get_indentation(target) + self.indent_word,
  802. target[match.end(0):].lstrip())
  803. def fix_e711(self, result):
  804. """Fix comparison with None."""
  805. (line_index, offset, target) = get_index_offset_contents(result,
  806. self.source)
  807. right_offset = offset + 2
  808. if right_offset >= len(target):
  809. return []
  810. left = target[:offset].rstrip()
  811. center = target[offset:right_offset]
  812. right = target[right_offset:].lstrip()
  813. if not right.startswith('None'):
  814. return []
  815. if center.strip() == '==':
  816. new_center = 'is'
  817. elif center.strip() == '!=':
  818. new_center = 'is not'
  819. else:
  820. return []
  821. self.source[line_index] = ' '.join([left, new_center, right])
  822. def fix_e712(self, result):
  823. """Fix (trivial case of) comparison with boolean."""
  824. (line_index, offset, target) = get_index_offset_contents(result,
  825. self.source)
  826. # Handle very easy "not" special cases.
  827. if re.match(r'^\s*if [\w.]+ == False:$', target):
  828. self.source[line_index] = re.sub(r'if ([\w.]+) == False:',
  829. r'if not \1:', target, count=1)
  830. elif re.match(r'^\s*if [\w.]+ != True:$', target):
  831. self.source[line_index] = re.sub(r'if ([\w.]+) != True:',
  832. r'if not \1:', target, count=1)
  833. else:
  834. right_offset = offset + 2
  835. if right_offset >= len(target):
  836. return []
  837. left = target[:offset].rstrip()
  838. center = target[offset:right_offset]
  839. right = target[right_offset:].lstrip()
  840. # Handle simple cases only.
  841. new_right = None
  842. if center.strip() == '==':
  843. if re.match(r'\bTrue\b', right):
  844. new_right = re.sub(r'\bTrue\b *', '', right, count=1)
  845. elif center.strip() == '!=':
  846. if re.match(r'\bFalse\b', right):
  847. new_right = re.sub(r'\bFalse\b *', '', right, count=1)
  848. if new_right is None:
  849. return []
  850. if new_right[0].isalnum():
  851. new_right = ' ' + new_right
  852. self.source[line_index] = left + new_right
  853. def fix_e713(self, result):
  854. """Fix (trivial case of) non-membership check."""
  855. (line_index, _, target) = get_index_offset_contents(result,
  856. self.source)
  857. match = COMPARE_NEGATIVE_REGEX.search(target)
  858. if match:
  859. if match.group(3) == 'in':
  860. pos_start = match.start(1)
  861. self.source[line_index] = '{0}{1} {2} {3} {4}'.format(
  862. target[:pos_start], match.group(2), match.group(1),
  863. match.group(3), target[match.end():])
  864. def fix_e714(self, result):
  865. """Fix object identity should be 'is not' case."""
  866. (line_index, _, target) = get_index_offset_contents(result,
  867. self.source)
  868. match = COMPARE_NEGATIVE_REGEX.search(target)
  869. if match:
  870. if match.group(3) == 'is':
  871. pos_start = match.start(1)
  872. self.source[line_index] = '{0}{1} {2} {3} {4}'.format(
  873. target[:pos_start], match.group(2), match.group(3),
  874. match.group(1), target[match.end():])
  875. def fix_e722(self, result):
  876. """fix bare except"""
  877. (line_index, _, target) = get_index_offset_contents(result,
  878. self.source)
  879. if BARE_EXCEPT_REGEX.search(target):
  880. self.source[line_index] = '{0}{1}'.format(
  881. target[:result['column'] - 1], "except Exception:")
  882. def fix_e731(self, result):
  883. """Fix do not assign a lambda expression check."""
  884. (line_index, _, target) = get_index_offset_contents(result,
  885. self.source)
  886. match = LAMBDA_REGEX.search(target)
  887. if match:
  888. end = match.end()
  889. self.source[line_index] = '{0}def {1}({2}): return {3}'.format(
  890. target[:match.start(0)], match.group(1), match.group(2),
  891. target[end:].lstrip())
  892. def fix_w291(self, result):
  893. """Remove trailing whitespace."""
  894. fixed_line = self.source[result['line'] - 1].rstrip()
  895. self.source[result['line'] - 1] = fixed_line + '\n'
  896. def fix_w391(self, _):
  897. """Remove trailing blank lines."""
  898. blank_count = 0
  899. for line in reversed(self.source):
  900. line = line.rstrip()
  901. if line:
  902. break
  903. else:
  904. blank_count += 1
  905. original_length = len(self.source)
  906. self.source = self.source[:original_length - blank_count]
  907. return range(1, 1 + original_length)
  908. def fix_w503(self, result):
  909. (line_index, _, target) = get_index_offset_contents(result,
  910. self.source)
  911. one_string_token = target.split()[0]
  912. try:
  913. ts = generate_tokens(one_string_token)
  914. except tokenize.TokenError:
  915. return
  916. if not _is_binary_operator(ts[0][0], one_string_token):
  917. return
  918. i = target.index(one_string_token)
  919. self.source[line_index] = '{0}{1}'.format(
  920. target[:i], target[i + len(one_string_token):])
  921. nl = find_newline(self.source[line_index - 1:line_index])
  922. before_line = self.source[line_index - 1]
  923. bl = before_line.index(nl)
  924. self.source[line_index - 1] = '{0} {1}{2}'.format(
  925. before_line[:bl], one_string_token,
  926. before_line[bl:])
  927. def get_index_offset_contents(result, source):
  928. """Return (line_index, column_offset, line_contents)."""
  929. line_index = result['line'] - 1
  930. return (line_index,
  931. result['column'] - 1,
  932. source[line_index])
  933. def get_fixed_long_line(target, previous_line, original,
  934. indent_word=' ', max_line_length=79,
  935. aggressive=False, experimental=False, verbose=False):
  936. """Break up long line and return result.
  937. Do this by generating multiple reformatted candidates and then
  938. ranking the candidates to heuristically select the best option.
  939. """
  940. indent = _get_indentation(target)
  941. source = target[len(indent):]
  942. assert source.lstrip() == source
  943. # Check for partial multiline.
  944. tokens = list(generate_tokens(source))
  945. candidates = shorten_line(
  946. tokens, source, indent,
  947. indent_word,
  948. max_line_length,
  949. aggressive=aggressive,
  950. experimental=experimental,
  951. previous_line=previous_line)
  952. # Also sort alphabetically as a tie breaker (for determinism).
  953. candidates = sorted(
  954. sorted(set(candidates).union([target, original])),
  955. key=lambda x: line_shortening_rank(
  956. x,
  957. indent_word,
  958. max_line_length,
  959. experimental=experimental))
  960. if verbose >= 4:
  961. print(('-' * 79 + '\n').join([''] + candidates + ['']),
  962. file=wrap_output(sys.stderr, 'utf-8'))
  963. if candidates:
  964. best_candidate = candidates[0]
  965. # Don't allow things to get longer.
  966. if longest_line_length(best_candidate) > longest_line_length(original):
  967. return None
  968. else:
  969. return best_candidate
  970. def longest_line_length(code):
  971. """Return length of longest line."""
  972. return max(len(line) for line in code.splitlines())
  973. def join_logical_line(logical_line):
  974. """Return single line based on logical line input."""
  975. indentation = _get_indentation(logical_line)
  976. return indentation + untokenize_without_newlines(
  977. generate_tokens(logical_line.lstrip())) + '\n'
  978. def untokenize_without_newlines(tokens):
  979. """Return source code based on tokens."""
  980. text = ''
  981. last_row = 0
  982. last_column = -1
  983. for t in tokens:
  984. token_string = t[1]
  985. (start_row, start_column) = t[2]
  986. (end_row, end_column) = t[3]
  987. if start_row > last_row:
  988. last_column = 0
  989. if (
  990. (start_column > last_column or token_string == '\n') and
  991. not text.endswith(' ')
  992. ):
  993. text += ' '
  994. if token_string != '\n':
  995. text += token_string
  996. last_row = end_row
  997. last_column = end_column
  998. return text.rstrip()
  999. def _find_logical(source_lines):
  1000. # Make a variable which is the index of all the starts of lines.
  1001. logical_start = []
  1002. logical_end = []
  1003. last_newline = True
  1004. parens = 0
  1005. for t in generate_tokens(''.join(source_lines)):
  1006. if t[0] in [tokenize.COMMENT, tokenize.DEDENT,
  1007. tokenize.INDENT, tokenize.NL,
  1008. tokenize.ENDMARKER]:
  1009. continue
  1010. if not parens and t[0] in [tokenize.NEWLINE, tokenize.SEMI]:
  1011. last_newline = True
  1012. logical_end.append((t[3][0] - 1, t[2][1]))
  1013. continue
  1014. if last_newline and not parens:
  1015. logical_start.append((t[2][0] - 1, t[2][1]))
  1016. last_newline = False
  1017. if t[0] == tokenize.OP:
  1018. if t[1] in '([{':
  1019. parens += 1
  1020. elif t[1] in '}])':
  1021. parens -= 1
  1022. return (logical_start, logical_end)
  1023. def _get_logical(source_lines, result, logical_start, logical_end):
  1024. """Return the logical line corresponding to the result.
  1025. Assumes input is already E702-clean.
  1026. """
  1027. row = result['line'] - 1
  1028. col = result['column'] - 1
  1029. ls = None
  1030. le = None
  1031. for i in range(0, len(logical_start), 1):
  1032. assert logical_end
  1033. x = logical_end[i]
  1034. if x[0] > row or (x[0] == row and x[1] > col):
  1035. le = x
  1036. ls = logical_start[i]
  1037. break
  1038. if ls is None:
  1039. return None
  1040. original = source_lines[ls[0]:le[0] + 1]
  1041. return ls, le, original
  1042. def get_item(items, index, default=None):
  1043. if 0 <= index < len(items):
  1044. return items[index]
  1045. else:
  1046. return default
  1047. def reindent(source, indent_size):
  1048. """Reindent all lines."""
  1049. reindenter = Reindenter(source)
  1050. return reindenter.run(indent_size)
  1051. def code_almost_equal(a, b):
  1052. """Return True if code is similar.
  1053. Ignore whitespace when comparing specific line.
  1054. """
  1055. split_a = split_and_strip_non_empty_lines(a)
  1056. split_b = split_and_strip_non_empty_lines(b)
  1057. if len(split_a) != len(split_b):
  1058. return False
  1059. for (index, _) in enumerate(split_a):
  1060. if ''.join(split_a[index].split()) != ''.join(split_b[index].split()):
  1061. return False
  1062. return True
  1063. def split_and_strip_non_empty_lines(text):
  1064. """Return lines split by newline.
  1065. Ignore empty lines.
  1066. """
  1067. return [line.strip() for line in text.splitlines() if line.strip()]
  1068. def fix_e265(source, aggressive=False): # pylint: disable=unused-argument
  1069. """Format block comments."""
  1070. if '#' not in source:
  1071. # Optimization.
  1072. return source
  1073. ignored_line_numbers = multiline_string_lines(
  1074. source,
  1075. include_docstrings=True) | set(commented_out_code_lines(source))
  1076. fixed_lines = []
  1077. sio = io.StringIO(source)
  1078. for (line_number, line) in enumerate(sio.readlines(), start=1):
  1079. if (
  1080. line.lstrip().startswith('#') and
  1081. line_number not in ignored_line_numbers and
  1082. not pycodestyle.noqa(line)
  1083. ):
  1084. indentation = _get_indentation(line)
  1085. line = line.lstrip()
  1086. # Normalize beginning if not a shebang.
  1087. if len(line) > 1:
  1088. pos = next((index for index, c in enumerate(line)
  1089. if c != '#'))
  1090. if (
  1091. # Leave multiple spaces like '# ' alone.
  1092. (line[:pos].count('#') > 1 or line[1].isalnum()) and
  1093. # Leave stylistic outlined blocks alone.
  1094. not line.rstrip().endswith('#')
  1095. ):
  1096. line = '# ' + line.lstrip('# \t')
  1097. fixed_lines.append(indentation + line)
  1098. else:
  1099. fixed_lines.append(line)
  1100. return ''.join(fixed_lines)
  1101. def refactor(source, fixer_names, ignore=None, filename=''):
  1102. """Return refactored code using lib2to3.
  1103. Skip if ignore string is produced in the refactored code.
  1104. """
  1105. check_lib2to3()
  1106. from lib2to3 import pgen2
  1107. try:
  1108. new_text = refactor_with_2to3(source,
  1109. fixer_names=fixer_names,
  1110. filename=filename)
  1111. except (pgen2.parse.ParseError,
  1112. SyntaxError,
  1113. UnicodeDecodeError,
  1114. UnicodeEncodeError):
  1115. return source
  1116. if ignore:
  1117. if ignore in new_text and ignore not in source:
  1118. return source
  1119. return new_text
  1120. def code_to_2to3(select, ignore):
  1121. fixes = set()
  1122. for code, fix in CODE_TO_2TO3.items():
  1123. if code_match(code, select=select, ignore=ignore):
  1124. fixes |= set(fix)
  1125. return fixes
  1126. def fix_2to3(source,
  1127. aggressive=True, select=None, ignore=None, filename=''):
  1128. """Fix various deprecated code (via lib2to3)."""
  1129. if not aggressive:
  1130. return source
  1131. select = select or []
  1132. ignore = ignore or []
  1133. return refactor(source,
  1134. code_to_2to3(select=select,
  1135. ignore=ignore),
  1136. filename=filename)
  1137. def fix_w602(source, aggressive=True):
  1138. """Fix deprecated form of raising exception."""
  1139. if not aggressive:
  1140. return source
  1141. return refactor(source, ['raise'],
  1142. ignore='with_traceback')
  1143. def find_newline(source):
  1144. """Return type of newline used in source.
  1145. Input is a list of lines.
  1146. """
  1147. assert not isinstance(source, unicode)
  1148. counter = collections.defaultdict(int)
  1149. for line in source:
  1150. if line.endswith(CRLF):
  1151. counter[CRLF] += 1
  1152. elif line.endswith(CR):
  1153. counter[CR] += 1
  1154. elif line.endswith(LF):
  1155. counter[LF] += 1
  1156. return (sorted(counter, key=counter.get, reverse=True) or [LF])[0]
  1157. def _get_indentword(source):
  1158. """Return indentation type."""
  1159. indent_word = ' ' # Default in case source has no indentation
  1160. try:
  1161. for t in generate_tokens(source):
  1162. if t[0] == token.INDENT:
  1163. indent_word = t[1]
  1164. break
  1165. except (SyntaxError, tokenize.TokenError):
  1166. pass
  1167. return indent_word
  1168. def _get_indentation(line):
  1169. """Return leading whitespace."""
  1170. if line.strip():
  1171. non_whitespace_index = len(line) - len(line.lstrip())
  1172. return line[:non_whitespace_index]
  1173. else:
  1174. return ''
  1175. def get_diff_text(old, new, filename):
  1176. """Return text of unified diff between old and new."""
  1177. newline = '\n'
  1178. diff = difflib.unified_diff(
  1179. old, new,
  1180. 'original/' + filename,
  1181. 'fixed/' + filename,
  1182. lineterm=newline)
  1183. text = ''
  1184. for line in diff:

Large files files are truncated, but you can click here to view the full file