From 3b9294a5f9951a6d3a43ef22349d58802abf2ae1 Mon Sep 17 00:00:00 2001 From: Chris Fournier Date: Thu, 17 Aug 2017 21:05:40 -0400 Subject: [PATCH 1/5] Add more operators --- shopify_python/google_styleguide.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/shopify_python/google_styleguide.py b/shopify_python/google_styleguide.py index b5cd012..73ad9c6 100644 --- a/shopify_python/google_styleguide.py +++ b/shopify_python/google_styleguide.py @@ -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 From 4bca6e910d1383e26a0512509ec11a623343b01b Mon Sep 17 00:00:00 2001 From: Chris Fournier Date: Thu, 17 Aug 2017 21:22:48 -0400 Subject: [PATCH 2/5] Don't suggest operators if we can't find the operand name --- shopify_python/google_styleguide.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shopify_python/google_styleguide.py b/shopify_python/google_styleguide.py index 73ad9c6..4b3cde2 100644 --- a/shopify_python/google_styleguide.py +++ b/shopify_python/google_styleguide.py @@ -297,7 +297,7 @@ def __lambda_func(self, node): # type: (astroid.Lambda) -> None if isinstance(node.body, astroid.UnaryOp): operator = self.UNARY_OPERATORS[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 From d5517db100c23c3be046df695240cc9aa937a54c Mon Sep 17 00:00:00 2001 From: Chris Fournier Date: Fri, 18 Aug 2017 14:15:43 -0400 Subject: [PATCH 3/5] Don't throw an exception if we can't find an operand --- shopify_python/google_styleguide.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/shopify_python/google_styleguide.py b/shopify_python/google_styleguide.py index 4b3cde2..bc4af42 100644 --- a/shopify_python/google_styleguide.py +++ b/shopify_python/google_styleguide.py @@ -295,7 +295,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) argname = node.args.args[0].name if operator and hasattr(node.body.operand, 'name') and argname == node.body.operand.name: varname = node.body.operand.name @@ -305,7 +305,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) 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 @@ -315,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 From 574b021e1ec40f7516af1043f2e824709b5c3ab9 Mon Sep 17 00:00:00 2001 From: Chris Fournier Date: Fri, 18 Aug 2017 14:30:16 -0400 Subject: [PATCH 4/5] Add tests --- .../shopify_python/test_google_styleguide.py | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/tests/shopify_python/test_google_styleguide.py b/tests/shopify_python/test_google_styleguide.py index 21aaaea..5ef6bd8 100644 --- a/tests/shopify_python/test_google_styleguide.py +++ b/tests/shopify_python/test_google_styleguide.py @@ -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'), From 37212f7ae39bbb35ebef0f4237f8fec5ffa994d9 Mon Sep 17 00:00:00 2001 From: Chris Fournier Date: Wed, 23 Aug 2017 15:58:22 -0400 Subject: [PATCH 5/5] Locally disable lint warning --- tests/shopify_python/test_google_styleguide.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/shopify_python/test_google_styleguide.py b/tests/shopify_python/test_google_styleguide.py index 5ef6bd8..2326264 100644 --- a/tests/shopify_python/test_google_styleguide.py +++ b/tests/shopify_python/test_google_styleguide.py @@ -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