Skip to content

Commit 197043d

Browse files
committed
Refactor the logical line building and checking.
1 parent 634f213 commit 197043d

File tree

1 file changed

+8
-11
lines changed

1 file changed

+8
-11
lines changed

pep8.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1296,7 +1296,7 @@ def check_physical(self, line):
12961296

12971297
def build_tokens_line(self):
12981298
"""Build a logical line from tokens."""
1299-
self.mapping = []
1299+
mapping = []
13001300
logical = []
13011301
comments = []
13021302
length = 0
@@ -1323,23 +1323,21 @@ def build_tokens_line(self):
13231323
fill = self.lines[end_row - 1][end:start]
13241324
logical.append(fill)
13251325
length += len(fill)
1326-
self.mapping.append((length, token))
1326+
mapping.append((length, token))
13271327
logical.append(text)
13281328
length += len(text)
13291329
previous = token
13301330
self.logical_line = ''.join(logical)
13311331
self.noqa = comments and noqa(''.join(comments))
1332-
# With Python 2, if the line ends with '\r\r\n' the assertion fails
1333-
# assert self.logical_line.strip() == self.logical_line
1332+
return mapping or [(0, self.tokens[0])]
13341333

13351334
def check_logical(self):
13361335
"""Build a line from tokens and run all logical checks on it."""
1337-
self.build_tokens_line()
13381336
self.report.increment_logical_line()
1339-
token0 = self.mapping[0][1] if self.mapping else self.tokens[0]
1340-
first_line = self.lines[token0[2][0] - 1]
1341-
indent = first_line[:token0[2][1]]
1342-
self.indent_level = expand_indent(indent)
1337+
mapping = self.build_tokens_line()
1338+
(start_row, start_col) = mapping[0][1][2]
1339+
start_line = self.lines[start_row - 1]
1340+
self.indent_level = expand_indent(start_line[:start_col])
13431341
if self.verbose >= 2:
13441342
print(self.logical_line[:80].rstrip())
13451343
for name, check, argument_names in self._logical_checks:
@@ -1350,8 +1348,7 @@ def check_logical(self):
13501348
if isinstance(offset, tuple):
13511349
(li_number, li_offset) = offset
13521350
else:
1353-
(token_offset, token) = (0, token0)
1354-
for (token_offset, token) in self.mapping:
1351+
for (token_offset, token) in mapping:
13551352
if offset < token_offset:
13561353
break
13571354
li_number = token[2][0]

0 commit comments

Comments
 (0)