Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unary operators and resilience against unrecognized operators #74

Open
wants to merge 4 commits into
base: fix_binary_operator_lambda
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
11 changes: 7 additions & 4 deletions shopify_python/google_styleguide.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)
Copy link
Contributor

@cfournie cfournie Jun 5, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure how to test this while implementing all operators.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 @@ -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
Expand Down
8 changes: 6 additions & 2 deletions tests/shopify_python/test_google_styleguide.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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("""
Expand Down