From a495203b32e24be4e679bb9d011e56d4151440a3 Mon Sep 17 00:00:00 2001 From: Ian Stapleton Cordasco Date: Thu, 9 Nov 2017 04:04:09 -0600 Subject: [PATCH] WIP: Fix up E741 for ambiguous identifiers Closes #700 --- pycodestyle.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/pycodestyle.py b/pycodestyle.py index dc486c8d..45cfd201 100755 --- a/pycodestyle.py +++ b/pycodestyle.py @@ -146,6 +146,11 @@ def lru_cache(maxsize=128): # noqa as it's a fake implementation. ))) ) DUNDER_REGEX = re.compile(r'^__([^\s]+)__ = ') +AMBIGUOUS_IDENTIFIER_REGEX = re.compile( + r'^\s*(?:for (?Pl|I|O) in [^:]+|' + r'with .+ as (?Pl|I|O)|' + r'(?:async )?def [^(]+\([^)]*\b(?Pl|I|O)\b.*\)):' +) # Work around Python < 2.6 behaviour, which does not generate NL after # a comment which is on a line by itself. @@ -1303,13 +1308,23 @@ def ambiguous_identifier(logical_line, tokens): Okay: except AttributeError as o: Okay: with lock as L: + Okay: foo(l=12) E741: except AttributeError as O: E741: with lock as l: E741: global I E741: nonlocal l + E741: def foo(l): + E741: for I in range(10,\n step=2): + E741: for O in range(10, step=2): E742: class I(object): E743: def l(x): """ + match = AMBIGUOUS_IDENTIFIER_REGEX.match(logical_line) + if match: + idents = match.groupdict() + ident = (idents['withident'] or idents['forident'] or + idents['defident']) + yield match.start(), "E741 ambiguous variable name '%s'" % ident idents_to_avoid = ('l', 'O', 'I') prev_type, prev_text, prev_start, prev_end, __ = tokens[0] for token_type, text, start, end, line in tokens[1:]: