Skip to content

Commit 26893da

Browse files
committed
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
1 parent a99bcec commit 26893da

3 files changed

Lines changed: 34 additions & 0 deletions

File tree

CHANGES.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Changes:
1616
* Added check E275 for whitespace on `from ... import ...` lines; #489 / #491
1717
* Added W503 to the list of codes ignored by default ignore list; #498
1818
* Removed use of project level `.pep8` configuration file; #364
19+
* Added check E741 for using variables named 'l', 'O', or 'I'; #341
1920

2021
Bugs:
2122

docs/intro.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,8 @@ This is the current list of error and warning codes:
358358
+------------+----------------------------------------------------------------------+
359359
| E731 | do not assign a lambda expression, use a def |
360360
+------------+----------------------------------------------------------------------+
361+
| E741 | do not use variables named 'l', 'O', or 'I' |
362+
+------------+----------------------------------------------------------------------+
361363
+------------+----------------------------------------------------------------------+
362364
| **E9** | *Runtime* |
363365
+------------+----------------------------------------------------------------------+

pycodestyle.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1150,6 +1150,37 @@ def comparison_type(logical_line, noqa):
11501150
yield match.start(), "E721 do not compare types, use 'isinstance()'"
11511151

11521152

1153+
def ambiguous_identifier(logical_line, tokens):
1154+
r"""Never use the characters 'l', 'O', or 'I' as variable names.
1155+
1156+
In some fonts, these characters are indistinguishable from the numerals
1157+
one and zero. When tempted to use 'l', use 'L' instead.
1158+
1159+
Okay: L = 0
1160+
E741: l = 0
1161+
"""
1162+
idents_to_avoid = ('l', 'O', 'I')
1163+
prev_type, prev_text, prev_start, prev_end, __ = tokens[0]
1164+
for token_type, text, start, end, line in tokens[1:]:
1165+
ident = pos = None
1166+
# identifiers on the lhs of an assignment operator
1167+
if token_type == tokenize.OP and '=' in text:
1168+
if prev_text in idents_to_avoid:
1169+
ident = prev_text
1170+
pos = prev_start
1171+
# identifiers after 'as'
1172+
if prev_text == 'as':
1173+
if text in idents_to_avoid:
1174+
ident = text
1175+
pos = start
1176+
if ident:
1177+
yield pos, "E741 ambiguous variable name '%s'" % ident
1178+
prev_type = token_type
1179+
prev_text = text
1180+
prev_start = start
1181+
prev_end = end
1182+
1183+
11531184
def python_3000_has_key(logical_line, noqa):
11541185
r"""The {}.has_key() method is removed in Python 3: use the 'in' operator.
11551186

0 commit comments

Comments
 (0)