Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions shopify_python/google_styleguide.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,18 +121,26 @@ class GoogleStyleGuideChecker(checkers.BaseChecker):

BINARY_OPERATORS = {
"+": "add",
"-": "sub",
"*": "mul",
"in": "contains",
"/": "truediv",
"//": "floordiv",
"&": "and_",
"^": "xor",
"|": "or_",
"**": "pow",
"is": "is",
"is not": "is_not",
"<<": "lshift",
"%": "modulo",
"*": "mul",
">>": "rshift",
"-": "sub",
"<": "lt",
"<=": "le",
"==": "eq",
"!=": "ne",
">=": "ge",
">": "gt",
"is": "is"
">": "gt"
}

def visit_assign(self, node): # type: (astroid.Assign) -> None
Expand Down Expand Up @@ -287,17 +295,17 @@ def __lambda_func(self, node): # type: (astroid.Lambda) -> None
"""Prefer Operator Function to Lambda Functions"""

if isinstance(node.body, astroid.UnaryOp):
operator = self.UNARY_OPERATORS[node.body.op]
operator = self.UNARY_OPERATORS.get(node.body.op)
argname = node.args.args[0].name
if operator and not isinstance(node.body.operand, astroid.BinOp) and argname is node.body.operand.name:
if operator and hasattr(node.body.operand, 'name') and argname == node.body.operand.name:
varname = node.body.operand.name
lambda_fun = "lambda " + varname + ": " + node.body.op + " " + varname
op_fun = "operator." + operator
self.add_message('lambda-func', node=node, args={'op': op_fun, 'lambda_fun': lambda_fun})
elif isinstance(node.body, astroid.BinOp):
if shopify_python.ast.count_tree_size(node.body) == 3 and len(node.args.args) == 2:
node = node.body
operator = self.BINARY_OPERATORS[node.op]
operator = self.BINARY_OPERATORS.get(node.op)
if operator:
left = str(node.left.value) if node.left.name == 'int' else node.left.name
right = str(node.right.value) if node.right.name == 'int' else node.right.name
Expand All @@ -307,7 +315,7 @@ def __lambda_func(self, node): # type: (astroid.Lambda) -> None
elif isinstance(node.body, astroid.Compare):
if shopify_python.ast.count_tree_size(node.body) == 3 and len(node.args.args) == 2:
node = node.body
operator = self.BINARY_OPERATORS[node.ops[0][0]]
operator = self.BINARY_OPERATORS.get(node.ops[0][0])
if operator:
left = str(node.left.value) if node.left.name == 'int' else node.left.name
right = node.ops[0][1].name
Expand Down
31 changes: 30 additions & 1 deletion tests/shopify_python/test_google_styleguide.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from shopify_python import google_styleguide


class TestGoogleStyleGuideChecker(pylint.testutils.CheckerTestCase):
class TestGoogleStyleGuideChecker(pylint.testutils.CheckerTestCase): # pylint: disable=too-many-public-methods

CHECKER_CLASS = google_styleguide.GoogleStyleGuideChecker

Expand Down Expand Up @@ -241,6 +241,35 @@ def unaryfnc():
with self.assertNoMessages():
self.walk(unary_root)

def test_unary_lambda_func_without_operand_name_allowed(self):
unary_root = astroid.builder.parse("""
def unaryfnc():
unary_pass = map(lambda x: not x.attribute, [1, 2, 3, 4])
""")
with self.assertNoMessages():
self.walk(unary_root)

def test_unary_lambda_func_with_unknown_operator_allowed(self, monkeypatch):
monkeypatch.setattr(google_styleguide.GoogleStyleGuideChecker, 'UNARY_OPERATORS', dict())
unary_root = astroid.builder.parse("""
def unaryfnc():
unary_pass = map(lambda x: not x, [1, 2, 3, 4])
""")
with self.assertNoMessages():
self.walk(unary_root)

@pytest.mark.parametrize('operator', [
'+', '<'
])
def test_binary_lambda_func_with_unknown_operator_allowed(self, operator, monkeypatch):
monkeypatch.setattr(google_styleguide.GoogleStyleGuideChecker, 'BINARY_OPERATORS', dict())
unary_root = astroid.builder.parse("""
def unaryfnc():
binary_pass = map(lambda x, y: x %s y, [(1, 2), (3, 4)])
""" % operator)
with self.assertNoMessages():
self.walk(unary_root)

@pytest.mark.parametrize('test_case', [
('- x', 'neg'),
('~ x', 'invert'),
Expand Down