diff --git a/shopify_python/google_styleguide.py b/shopify_python/google_styleguide.py index ce3a284..14cbcec 100644 --- a/shopify_python/google_styleguide.py +++ b/shopify_python/google_styleguide.py @@ -131,7 +131,10 @@ class GoogleStyleGuideChecker(checkers.BaseChecker): "==": "eq", "!=": "ne", ">=": "ge", - ">": "gt" + ">": "gt", + "&": "and_", + "|": "or_", + "^": "xor", } def visit_assign(self, node): # type: (astroid.Assign) -> None @@ -285,7 +288,7 @@ 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, None) argname = node.args.args[0].name if operator and not isinstance(node.body.operand, astroid.BinOp) and argname is node.body.operand.name: varname = node.body.operand.name @@ -295,7 +298,7 @@ def __lambda_func(self, node): # type: (astroid.Lambda) -> None 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, None) 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 @@ -305,7 +308,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], None) if operator: left = str(node.left.value) if node.left.name == 'int' else node.left.name right = node.ops[0][1].name diff --git a/tests/shopify_python/test_google_styleguide.py b/tests/shopify_python/test_google_styleguide.py index c90f7b5..5848869 100644 --- a/tests/shopify_python/test_google_styleguide.py +++ b/tests/shopify_python/test_google_styleguide.py @@ -267,7 +267,10 @@ def unaryfnc(): ('x == y', 'eq'), ('x != y', 'ne'), ('x >= y', 'ge'), - ('x > y', 'gt') + ('x > y', 'gt'), + ('x & y', 'and_'), + ('x | y', 'or_'), + ('x ^ y', 'xor'), ]) def test_binary_lambda_func(self, test_case): (expression, op_name) = test_case @@ -286,7 +289,8 @@ def binaryfnc(): @pytest.mark.parametrize('expression', [ 'map(lambda x: x + 3, [1, 2, 3, 4])', - 'reduce(lambda x, y, z: x * y + z, [1, 2, 3])' + 'reduce(lambda x, y, z: x * y + z, [1, 2, 3])', + 'reduce(lambda x, y: x in y, [1, 2, 3])' ]) def test_allowed_binary_operation(self, expression): binary_root = astroid.builder.parse("""