summaryrefslogtreecommitdiff
path: root/pycodestyle.py
diff options
context:
space:
mode:
authorMike Miller <mike@mtmxr.com>2016-06-02 12:44:23 -0700
committerMike Miller <mike@mtmxr.com>2016-06-02 14:34:16 -0700
commit26893dafe4562caefeb200c990972bc6262842d9 (patch)
treeb8640109c4755ba4dbd84859248489e23de38f3c /pycodestyle.py
parenta99bcecd4bb690271b5f0c92b22d3b2c8c872ec7 (diff)
downloadpep8-26893dafe4562caefeb200c990972bc6262842d9.tar.gz
Report E741 for prohibited single-letter variables
Check for prohibited identifiers occuring to the lhs of an assignment operator or after the 'as' keyword. Closes #341
Diffstat (limited to 'pycodestyle.py')
-rwxr-xr-xpycodestyle.py31
1 files changed, 31 insertions, 0 deletions
diff --git a/pycodestyle.py b/pycodestyle.py
index 92fe12a..ad1b463 100755
--- a/pycodestyle.py
+++ b/pycodestyle.py
@@ -1150,6 +1150,37 @@ def comparison_type(logical_line, noqa):
yield match.start(), "E721 do not compare types, use 'isinstance()'"
+def ambiguous_identifier(logical_line, tokens):
+ r"""Never use the characters 'l', 'O', or 'I' as variable names.
+
+ In some fonts, these characters are indistinguishable from the numerals
+ one and zero. When tempted to use 'l', use 'L' instead.
+
+ Okay: L = 0
+ E741: l = 0
+ """
+ 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:]:
+ ident = pos = None
+ # identifiers on the lhs of an assignment operator
+ if token_type == tokenize.OP and '=' in text:
+ if prev_text in idents_to_avoid:
+ ident = prev_text
+ pos = prev_start
+ # identifiers after 'as'
+ if prev_text == 'as':
+ if text in idents_to_avoid:
+ ident = text
+ pos = start
+ if ident:
+ yield pos, "E741 ambiguous variable name '%s'" % ident
+ prev_type = token_type
+ prev_text = text
+ prev_start = start
+ prev_end = end
+
+
def python_3000_has_key(logical_line, noqa):
r"""The {}.has_key() method is removed in Python 3: use the 'in' operator.