From a71f5562ec98ae1181eaf0a3029d7828a2913c33 Mon Sep 17 00:00:00 2001 From: tristanlatr <19967168+tristanlatr@users.noreply.github.com> Date: Fri, 10 Jun 2022 15:07:11 -0400 Subject: [PATCH 01/13] Add a second test for simple augmented assignments --- tests/unittest_inference.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/unittest_inference.py b/tests/unittest_inference.py index 6ae4f40ae3..77a9cc1b23 100644 --- a/tests/unittest_inference.py +++ b/tests/unittest_inference.py @@ -1414,6 +1414,20 @@ def test_augassign(self) -> None: self.assertEqual(len(inferred), 1) self.assertIsInstance(inferred[0], nodes.Const) self.assertEqual(inferred[0].value, 3) + + def test_augassign_multi(self) -> None: + code = """ + a = 1 + a += 1 + a += 1 + print (a) + """ + ast = parse(code, __name__) + inferred = list(test_utils.get_name_node(ast, "a").infer()) + + self.assertEqual(len(inferred), 1) + self.assertIsInstance(inferred[0], nodes.Const) + self.assertEqual(inferred[0].value, 3) def test_nonregr_func_arg(self) -> None: code = """ From f4977dcd02410011226c124423f522fa71d998b6 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 10 Jun 2022 19:11:43 +0000 Subject: [PATCH 02/13] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/unittest_inference.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unittest_inference.py b/tests/unittest_inference.py index 77a9cc1b23..a25f29833a 100644 --- a/tests/unittest_inference.py +++ b/tests/unittest_inference.py @@ -1414,7 +1414,7 @@ def test_augassign(self) -> None: self.assertEqual(len(inferred), 1) self.assertIsInstance(inferred[0], nodes.Const) self.assertEqual(inferred[0].value, 3) - + def test_augassign_multi(self) -> None: code = """ a = 1 From 053601600f20636def93bf529f70132c0c330781 Mon Sep 17 00:00:00 2001 From: tristanlatr <19967168+tristanlatr@users.noreply.github.com> Date: Fri, 10 Jun 2022 15:46:49 -0400 Subject: [PATCH 03/13] Add more tests --- tests/unittest_inference.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/tests/unittest_inference.py b/tests/unittest_inference.py index a25f29833a..be649ca942 100644 --- a/tests/unittest_inference.py +++ b/tests/unittest_inference.py @@ -1428,6 +1428,36 @@ def test_augassign_multi(self) -> None: self.assertEqual(len(inferred), 1) self.assertIsInstance(inferred[0], nodes.Const) self.assertEqual(inferred[0].value, 3) + + def test_augassign_multi_expr(self) -> None: + code = """ + a = 1 + a += 1 + a += 1 + a + """ + ast = astroid.parse(code, __name__) + inferred = list(ast.body[1].infer()) + + self.assertEqual(len(inferred), 1) + self.assertIsInstance(inferred[0], nodes.Const) + self.assertEqual(inferred[0].value, 3) + + def test_augassign_multi_list(self) -> None: + code = """ + a = [] + a += [1] + a += [1] + print (a) + """ + ast = parse(code, __name__) + inferred = list(test_utils.get_name_node(ast, "a").infer()) + + self.assertEqual(len(inferred), 1) + self.assertIsInstance(inferred[0], nodes.List) + self.assertEqual(len(inferred[0].elts[1]), 2) + self.assertEqual(inferred[0].elts[1].value, 1) + self.assertEqual(inferred[0].elts[2].value, 1) def test_nonregr_func_arg(self) -> None: code = """ From 15c63eed951242edee7a88465eb220626048d92e Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 10 Jun 2022 19:47:31 +0000 Subject: [PATCH 04/13] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/unittest_inference.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/unittest_inference.py b/tests/unittest_inference.py index be649ca942..d374bcbee8 100644 --- a/tests/unittest_inference.py +++ b/tests/unittest_inference.py @@ -1428,7 +1428,7 @@ def test_augassign_multi(self) -> None: self.assertEqual(len(inferred), 1) self.assertIsInstance(inferred[0], nodes.Const) self.assertEqual(inferred[0].value, 3) - + def test_augassign_multi_expr(self) -> None: code = """ a = 1 @@ -1442,7 +1442,7 @@ def test_augassign_multi_expr(self) -> None: self.assertEqual(len(inferred), 1) self.assertIsInstance(inferred[0], nodes.Const) self.assertEqual(inferred[0].value, 3) - + def test_augassign_multi_list(self) -> None: code = """ a = [] From a97a101a4ba96114e020007045549ed53503f518 Mon Sep 17 00:00:00 2001 From: tristanlatr <19967168+tristanlatr@users.noreply.github.com> Date: Fri, 10 Jun 2022 15:49:21 -0400 Subject: [PATCH 05/13] Fix name error --- tests/unittest_inference.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unittest_inference.py b/tests/unittest_inference.py index d374bcbee8..2e61072844 100644 --- a/tests/unittest_inference.py +++ b/tests/unittest_inference.py @@ -1436,7 +1436,7 @@ def test_augassign_multi_expr(self) -> None: a += 1 a """ - ast = astroid.parse(code, __name__) + ast = parse(code, __name__) inferred = list(ast.body[1].infer()) self.assertEqual(len(inferred), 1) From c63235bb16e79f0f6344dea73c44bc03a3031da3 Mon Sep 17 00:00:00 2001 From: tristanlatr <19967168+tristanlatr@users.noreply.github.com> Date: Fri, 10 Jun 2022 15:52:21 -0400 Subject: [PATCH 06/13] fix type, went too fast --- tests/unittest_inference.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unittest_inference.py b/tests/unittest_inference.py index 2e61072844..83b28ed95b 100644 --- a/tests/unittest_inference.py +++ b/tests/unittest_inference.py @@ -1455,7 +1455,7 @@ def test_augassign_multi_list(self) -> None: self.assertEqual(len(inferred), 1) self.assertIsInstance(inferred[0], nodes.List) - self.assertEqual(len(inferred[0].elts[1]), 2) + self.assertEqual(len(inferred[0].elts), 2) self.assertEqual(inferred[0].elts[1].value, 1) self.assertEqual(inferred[0].elts[2].value, 1) From 5a34c2dca50901a3b6c95b2f531e34372006b9fc Mon Sep 17 00:00:00 2001 From: tristanlatr <19967168+tristanlatr@users.noreply.github.com> Date: Fri, 10 Jun 2022 16:01:49 -0400 Subject: [PATCH 07/13] index error --- tests/unittest_inference.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unittest_inference.py b/tests/unittest_inference.py index 83b28ed95b..63066e77cb 100644 --- a/tests/unittest_inference.py +++ b/tests/unittest_inference.py @@ -1457,7 +1457,7 @@ def test_augassign_multi_list(self) -> None: self.assertIsInstance(inferred[0], nodes.List) self.assertEqual(len(inferred[0].elts), 2) self.assertEqual(inferred[0].elts[1].value, 1) - self.assertEqual(inferred[0].elts[2].value, 1) + self.assertEqual(inferred[0].elts[0].value, 1) def test_nonregr_func_arg(self) -> None: code = """ From e96a4f789b8ad74f8e23b195f1301db33feb7e7a Mon Sep 17 00:00:00 2001 From: tristanlatr <19967168+tristanlatr@users.noreply.github.com> Date: Fri, 10 Jun 2022 16:15:15 -0400 Subject: [PATCH 08/13] Fix index should be -1 --- tests/unittest_inference.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unittest_inference.py b/tests/unittest_inference.py index 63066e77cb..d027626614 100644 --- a/tests/unittest_inference.py +++ b/tests/unittest_inference.py @@ -1437,7 +1437,7 @@ def test_augassign_multi_expr(self) -> None: a """ ast = parse(code, __name__) - inferred = list(ast.body[1].infer()) + inferred = list(ast.body[-1].infer()) self.assertEqual(len(inferred), 1) self.assertIsInstance(inferred[0], nodes.Const) From c364e9463156cf77a1a82b6ad7a571760467b22c Mon Sep 17 00:00:00 2001 From: tristanlatr <19967168+tristanlatr@users.noreply.github.com> Date: Fri, 10 Jun 2022 16:18:25 -0400 Subject: [PATCH 09/13] Fix test case finally --- tests/unittest_inference.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/unittest_inference.py b/tests/unittest_inference.py index d027626614..ca5a35ca86 100644 --- a/tests/unittest_inference.py +++ b/tests/unittest_inference.py @@ -1437,7 +1437,8 @@ def test_augassign_multi_expr(self) -> None: a """ ast = parse(code, __name__) - inferred = list(ast.body[-1].infer()) + # No inference function for Expr + inferred = list(ast.body[-1].value.infer()) self.assertEqual(len(inferred), 1) self.assertIsInstance(inferred[0], nodes.Const) From 5c491f7e123053cd7098d8935fd3ac572bc408d7 Mon Sep 17 00:00:00 2001 From: tristanlatr <19967168+tristanlatr@users.noreply.github.com> Date: Fri, 10 Jun 2022 17:08:27 -0400 Subject: [PATCH 10/13] Add some tests for getattr() --- tests/unittest_lookup.py | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/tests/unittest_lookup.py b/tests/unittest_lookup.py index 1cb2526188..8c59de131a 100644 --- a/tests/unittest_lookup.py +++ b/tests/unittest_lookup.py @@ -1009,6 +1009,45 @@ def test_except_assign_after_block_overwritten(self) -> None: self.assertEqual(len(stmts), 1) self.assertEqual(stmts[0].lineno, 8) + + def test_except_assign_exclusive_branches_getattr(self) -> None: + """When a variable is assigned in exlcusive branches, both are returned + """ + code = """ + try: + 1 / 0 + except ZeroDivisionError: + x = 10 + except NameError: + x = 100 + print(x) + """ + astroid = builder.parse(code) + stmts = astroid.getattr('x') + self.assertEqual(len(stmts), 2) + + self.assertEqual(stmts[0].lineno, 5) + self.assertEqual(stmts[1].lineno, 7) + + def test_except_assign_after_block_overwritten_getattr(self) -> None: + """When a variable is assigned in an except clause, it is not returned + when it is reassigned and used after the except block. + """ + code = """ + try: + 1 / 0 + except ZeroDivisionError: + x = 10 + except NameError: + x = 100 + x = 1000 + print(x) + """ + astroid = builder.parse(code) + stmts = astroid.getattr('x') + self.assertEqual(len(stmts), 1) + self.assertEqual(stmts[0].lineno, 8) + if __name__ == "__main__": unittest.main() From 3caabf97f77a373831550ec66a4e90c247462ca1 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 10 Jun 2022 21:09:52 +0000 Subject: [PATCH 11/13] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/unittest_lookup.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/tests/unittest_lookup.py b/tests/unittest_lookup.py index 8c59de131a..9e56256ee9 100644 --- a/tests/unittest_lookup.py +++ b/tests/unittest_lookup.py @@ -1009,10 +1009,8 @@ def test_except_assign_after_block_overwritten(self) -> None: self.assertEqual(len(stmts), 1) self.assertEqual(stmts[0].lineno, 8) - def test_except_assign_exclusive_branches_getattr(self) -> None: - """When a variable is assigned in exlcusive branches, both are returned - """ + """When a variable is assigned in exlcusive branches, both are returned""" code = """ try: 1 / 0 @@ -1023,12 +1021,12 @@ def test_except_assign_exclusive_branches_getattr(self) -> None: print(x) """ astroid = builder.parse(code) - stmts = astroid.getattr('x') + stmts = astroid.getattr("x") self.assertEqual(len(stmts), 2) - + self.assertEqual(stmts[0].lineno, 5) self.assertEqual(stmts[1].lineno, 7) - + def test_except_assign_after_block_overwritten_getattr(self) -> None: """When a variable is assigned in an except clause, it is not returned when it is reassigned and used after the except block. @@ -1044,7 +1042,7 @@ def test_except_assign_after_block_overwritten_getattr(self) -> None: print(x) """ astroid = builder.parse(code) - stmts = astroid.getattr('x') + stmts = astroid.getattr("x") self.assertEqual(len(stmts), 1) self.assertEqual(stmts[0].lineno, 8) From 2c71690d862bd5040934b3f1f7cd8ae858d55ff6 Mon Sep 17 00:00:00 2001 From: tristanlatr <19967168+tristanlatr@users.noreply.github.com> Date: Mon, 13 Jun 2022 12:37:24 -0400 Subject: [PATCH 12/13] Add another test --- tests/unittest_lookup.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tests/unittest_lookup.py b/tests/unittest_lookup.py index 9e56256ee9..c27e713db0 100644 --- a/tests/unittest_lookup.py +++ b/tests/unittest_lookup.py @@ -1045,6 +1045,26 @@ def test_except_assign_after_block_overwritten_getattr(self) -> None: stmts = astroid.getattr("x") self.assertEqual(len(stmts), 1) self.assertEqual(stmts[0].lineno, 8) + + def test_except_assign_after_block_overwritten_getattr_class(self) -> None: + """When a variable is assigned in an except clause, it is not returned + when it is reassigned and used after the except block. + """ + code = """ + class C: + try: + 1 / 0 + except ZeroDivisionError: + x = 10 + except NameError: + x = 100 + x = 1000 + print(x) + C.x + """ + astroid = builder.parse(code) + stmts = list(astroid.body[-1].value.infer()) + self.assertEqual(len(stmts), 1) if __name__ == "__main__": From 767e6f17866d3fe81bbfbc456d72d557983fbf1d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 13 Jun 2022 16:38:06 +0000 Subject: [PATCH 13/13] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/unittest_lookup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unittest_lookup.py b/tests/unittest_lookup.py index c27e713db0..f98ec10364 100644 --- a/tests/unittest_lookup.py +++ b/tests/unittest_lookup.py @@ -1045,7 +1045,7 @@ def test_except_assign_after_block_overwritten_getattr(self) -> None: stmts = astroid.getattr("x") self.assertEqual(len(stmts), 1) self.assertEqual(stmts[0].lineno, 8) - + def test_except_assign_after_block_overwritten_getattr_class(self) -> None: """When a variable is assigned in an except clause, it is not returned when it is reassigned and used after the except block.