Skip to content

Commit 75e1dd1

Browse files
committed
updates
1 parent 8aee790 commit 75e1dd1

File tree

2 files changed

+35
-55
lines changed

2 files changed

+35
-55
lines changed

shopify_python/google_styleguide.py

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ class GoogleStyleGuideChecker(checkers.BaseChecker):
8383
"For example: x = 1 if cond else 2. "
8484
"Conditional Expressions okay to use for one-liners. "
8585
"In other cases prefer to use a complete if statement. "),
86-
'C6014': ('Prefer operator module function %(op)s to lambda function %(lamfun)s',
86+
'C6014': ('Prefer operator module function %(op)s to lambda function %(lambda_fcn)s',
8787
'lambda-func',
8888
"For common operations like multiplication, use the functions from the operator module"
8989
"instead of lambda functions. For example, prefer operator.mul to lambda x, y: x * y."),
@@ -112,6 +112,28 @@ class GoogleStyleGuideChecker(checkers.BaseChecker):
112112
'help': 'Number of AST nodes permitted in a lambda'}),
113113
)
114114

115+
UNARY_OPERATORS = {
116+
"~": "invert",
117+
"-": "neg",
118+
"not": "not_",
119+
"+": "pos"
120+
}
121+
122+
BINARY_OPERATORS = {
123+
"+": "add",
124+
"-": "sub",
125+
"*": "mul",
126+
"/": "truediv",
127+
"**": "pow",
128+
"%": "modulo",
129+
"<": "lt",
130+
"<=": "le",
131+
"==": "eq",
132+
"!=": "ne",
133+
">=": "ge",
134+
">": "gt"
135+
}
136+
115137
def visit_assign(self, node): # type: (astroid.Assign) -> None
116138
self.__avoid_global_variables(node)
117139

@@ -264,42 +286,22 @@ def __lambda_func(self, node): # type: (astroid.Lambda) -> None
264286

265287
if isinstance(node.body, astroid.UnaryOp):
266288
if shopify_python.ast.count_tree_size(node.body) == 2 and len(node.args.args) == 1:
267-
unary_operators = {
268-
"~": "invert",
269-
"-": "neg",
270-
"not": "not_",
271-
"+": "pos"
272-
}
273-
preferred_operator_function = unary_operators.get(node.body.op)
289+
preferred_operator_function = self.UNARY_OPERATORS.get(node.body.op)
274290
name_match = node.args.args[0].name is node.body.operand.name
275291
if preferred_operator_function and not isinstance(node.body.operand, astroid.BinOp) and name_match:
276292
varname = node.body.operand.name
277-
lamfun = "lambda " + varname + ": " + node.body.op + " " + varname
293+
lambda_fcn = "lambda " + varname + ": " + node.body.op + " " + varname
278294
opfun = "operator." + preferred_operator_function
279-
self.add_message('lambda-func', node=node, args={'op': opfun, 'lamfun': lamfun})
295+
self.add_message('lambda-func', node=node, args={'op': opfun, 'lambda_fcn': lambda_fcn})
280296
elif isinstance(node.body, astroid.BinOp):
281297
if shopify_python.ast.count_tree_size(node.body) == 3 and len(node.args.args) == 2:
282298
left_arg = node.args.args[0].name
283299
right_arg = node.args.args[1].name
284300
node = node.body
285-
binary_operators = {
286-
"+": "add",
287-
"-": "sub",
288-
"*": "mul",
289-
"/": "truediv",
290-
"**": "pow",
291-
"%": "modulo",
292-
"<": "lt",
293-
"<=": "le",
294-
"==": "eq",
295-
"!=": "ne",
296-
">=": "ge",
297-
">": "gt"
298-
}
299-
preferred_operator_function = binary_operators.get(node.op)
301+
preferred_operator_function = self.BINARY_OPERATORS.get(node.op)
300302
if preferred_operator_function and left_arg is node.left.name and right_arg is node.right.name:
301303
left = str(node.left.value) if node.left.name == 'int' else node.left.name
302304
right = str(node.right.value) if node.right.name == 'int' else node.right.name
303-
lamfun = "lambda " + left + ', ' + right + ": " + " ".join([left, node.op, right])
305+
lambda_fcn = "lambda " + left + ', ' + right + ": " + " ".join([left, node.op, right])
304306
opfun = "operator." + preferred_operator_function
305-
self.add_message('lambda-func', node=node, args={'op': opfun, 'lamfun': lamfun})
307+
self.add_message('lambda-func', node=node, args={'op': opfun, 'lambda_fcn': lambda_fcn})

tests/shopify_python/test_google_styleguide.py

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -230,52 +230,30 @@ def test_unary_lambda_func(self):
230230
unary_root = astroid.builder.parse("""
231231
def unaryfnc():
232232
unary_pass = map(lambda x: not (x+3), [1, 2, 3, 4])
233-
unary_msg = map(lambda x: -x, [1, 2, 3, 4])
233+
unary_fail = map(lambda x: -x, [1, 2, 3, 4])
234234
""")
235235

236-
# Unary Op Test 1: This will not trigger a message
237-
ulam_pass = unary_root.body[0].body[0].value.args[0]
238-
with self.assertNoMessages():
239-
self.walk(ulam_pass)
240-
241-
# Unary Op Test 2: This will trigger a message
242236
ulam_msg = unary_root.body[0].body[1].value.args[0]
243237
unary_message = pylint.testutils.Message('lambda-func', node=ulam_msg, args={
244238
'op': 'operator.neg',
245-
'lamfun': 'lambda x: - x'
239+
'lambda_fcn': 'lambda x: - x'
246240
})
247241
with self.assertAddsMessages(unary_message):
248-
self.walk(ulam_msg)
242+
self.walk(unary_root)
249243

250244
def test_binary_lambda_func(self):
251245
binary_root = astroid.builder.parse("""
252246
def binaryfnc():
253247
binary_pass = reduce(lambda x, y, z: x * y + z, [1, 2, 3])
254248
binary_pass2 = map(lambda x: x + 3, [1, 2, 3, 4])
255249
binary_pass3 = map(lambda x, y: x + 1)
256-
binary_msg = reduce(lambda x, y: x * y, [1, 2, 3, 4])
250+
binary_fail = reduce(lambda x, y: x * y, [1, 2, 3, 4])
257251
""")
258252

259-
# Binary Op Test 1: This will not trigger a message
260-
binary_pass = binary_root.body[0].body[0].value.args[0]
261-
with self.assertNoMessages():
262-
self.walk(binary_pass)
263-
264-
# Binary Op Test 2: This will not trigger a message
265-
binary_pass2 = binary_root.body[0].body[1].value.args[0]
266-
with self.assertNoMessages():
267-
self.walk(binary_pass2)
268-
269-
# Binary Op Test 3: This will not trigger a message
270-
binary_pass2 = binary_root.body[0].body[2].value.args[0]
271-
with self.assertNoMessages():
272-
self.walk(binary_pass2)
273-
274-
# Binary Op Test 3: This will trigger a message
275253
binary_msg = binary_root.body[0].body[3].value.args[0]
276254
bin_message = pylint.testutils.Message('lambda-func', node=binary_msg.body, args={
277255
'op': 'operator.mul',
278-
'lamfun': 'lambda x, y: x * y'
256+
'lambda_fcn': 'lambda x, y: x * y'
279257
})
280258
with self.assertAddsMessages(bin_message):
281-
self.walk(binary_msg)
259+
self.walk(binary_root)

0 commit comments

Comments
 (0)