Skip to content

Commit 024a697

Browse files
authored
Merge pull request #1291 from abebus/speedups/missing_whitespace_after_keyword
reduce "missing_whitespace_after_keyword" execution time by ~17%
2 parents 09bd5bd + 24cec77 commit 024a697

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

pycodestyle.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,17 @@ def whitespace_around_keywords(logical_line):
493493
yield match.start(2), "E271 multiple spaces after keyword"
494494

495495

496+
if sys.version_info < (3, 10):
497+
from itertools import tee
498+
499+
def pairwise(iterable):
500+
a, b = tee(iterable)
501+
next(b, None)
502+
return zip(a, b)
503+
else:
504+
from itertools import pairwise
505+
506+
496507
@register_check
497508
def missing_whitespace_after_keyword(logical_line, tokens):
498509
r"""Keywords should be followed by whitespace.
@@ -502,7 +513,7 @@ def missing_whitespace_after_keyword(logical_line, tokens):
502513
E275: from importable.module import(bar, baz)
503514
E275: if(foo): bar
504515
"""
505-
for tok0, tok1 in zip(tokens, tokens[1:]):
516+
for tok0, tok1 in pairwise(tokens):
506517
# This must exclude the True/False/None singletons, which can
507518
# appear e.g. as "if x is None:", and async/await, which were
508519
# valid identifier names in old Python versions.
@@ -512,7 +523,7 @@ def missing_whitespace_after_keyword(logical_line, tokens):
512523
tok0.string not in SINGLETONS and
513524
not (tok0.string == 'except' and tok1.string == '*') and
514525
not (tok0.string == 'yield' and tok1.string == ')') and
515-
tok1.string not in ':\n'):
526+
(tok1.string and tok1.string != ':' and tok1.string != '\n')):
516527
yield tok0.end, "E275 missing whitespace after keyword"
517528

518529

0 commit comments

Comments
 (0)