diff --git a/python-frontend/src/test/java/org/sonar/python/semantic/v2/TypeInferenceV2Test.java b/python-frontend/src/test/java/org/sonar/python/semantic/v2/TypeInferenceV2Test.java index b43fa8d5e9..62aa615c81 100644 --- a/python-frontend/src/test/java/org/sonar/python/semantic/v2/TypeInferenceV2Test.java +++ b/python-frontend/src/test/java/org/sonar/python/semantic/v2/TypeInferenceV2Test.java @@ -596,7 +596,7 @@ def foo(): """); var functionDef = (FunctionDef) root.statements().statements().get(1); - var lastExpressionStatement = (ExpressionStatement) functionDef.body().statements().get(functionDef.body().statements().size() -1); + var lastExpressionStatement = (ExpressionStatement) functionDef.body().statements().get(functionDef.body().statements().size() - 1); assertThat(lastExpressionStatement.expressions().get(0).typeV2().unwrappedType()).isEqualTo(PythonType.UNKNOWN); } @@ -610,7 +610,7 @@ def foo(): """); var functionDef = (FunctionDef) root.statements().statements().get(1); - var lastExpressionStatement = (ExpressionStatement) functionDef.body().statements().get(functionDef.body().statements().size() -1); + var lastExpressionStatement = (ExpressionStatement) functionDef.body().statements().get(functionDef.body().statements().size() - 1); assertThat(lastExpressionStatement.expressions().get(0).typeV2().unwrappedType()).isEqualTo(PythonType.UNKNOWN); } @@ -625,7 +625,7 @@ def foo(): """); var functionDef = (FunctionDef) root.statements().statements().get(1); - var lastExpressionStatement = (ExpressionStatement) functionDef.body().statements().get(functionDef.body().statements().size() -1); + var lastExpressionStatement = (ExpressionStatement) functionDef.body().statements().get(functionDef.body().statements().size() - 1); assertThat(lastExpressionStatement.expressions().get(0).typeV2().unwrappedType()).isEqualTo(INT_TYPE); } @@ -637,7 +637,7 @@ def foo(): x """); - var lastExpressionStatement = (ExpressionStatement) root.statements().statements().get(root.statements().statements().size() -1); + var lastExpressionStatement = (ExpressionStatement) root.statements().statements().get(root.statements().statements().size() - 1); assertThat(lastExpressionStatement.expressions().get(0).typeV2().unwrappedType()).isEqualTo(PythonType.UNKNOWN); } @@ -713,7 +713,8 @@ void typeSourceIsExactByDefault() { CallExpression callExpressionSpy = Mockito.spy(callExpression); Expression calleeSpy = Mockito.spy(callExpression.callee()); - FunctionType functionType = new FunctionType("foo", "my_package.foo", List.of(), List.of(), List.of(), new SimpleTypeWrapper(new ObjectType(INT_TYPE)), TypeOrigin.STUB, false, false, false, false, null, null); + FunctionType functionType = new FunctionType("foo", "my_package.foo", List.of(), List.of(), List.of(), new SimpleTypeWrapper(new ObjectType(INT_TYPE)), TypeOrigin.STUB, + false, false, false, false, null, null); Mockito.when(calleeSpy.typeV2()).thenReturn(functionType); Mockito.when(callExpressionSpy.callee()).thenReturn(calleeSpy); @@ -732,7 +733,7 @@ def foo(param: int): """); var functionDef = (FunctionDef) root.statements().statements().get(0); - var lastExpressionStatement = (ExpressionStatement) functionDef.body().statements().get(functionDef.body().statements().size() -1); + var lastExpressionStatement = (ExpressionStatement) functionDef.body().statements().get(functionDef.body().statements().size() - 1); assertThat(lastExpressionStatement.expressions().get(0).typeV2().unwrappedType()).isEqualTo(STR_TYPE); } @@ -745,7 +746,7 @@ def foo(param): """); var functionDef = (FunctionDef) root.statements().statements().get(0); - var lastExpressionStatement = (ExpressionStatement) functionDef.body().statements().get(functionDef.body().statements().size() -1); + var lastExpressionStatement = (ExpressionStatement) functionDef.body().statements().get(functionDef.body().statements().size() - 1); assertThat(lastExpressionStatement.expressions().get(0).typeV2().unwrappedType()).isEqualTo(STR_TYPE); } @@ -758,7 +759,7 @@ def foo(param: int): """); var functionDef = (FunctionDef) root.statements().statements().get(0); - var lastExpressionStatement = (ExpressionStatement) functionDef.body().statements().get(functionDef.body().statements().size() -1); + var lastExpressionStatement = (ExpressionStatement) functionDef.body().statements().get(functionDef.body().statements().size() - 1); assertThat(lastExpressionStatement.expressions().get(0).typeV2().unwrappedType()).isEqualTo(INT_TYPE); assertThat(lastExpressionStatement.expressions().get(0).typeV2().typeSource()).isEqualTo(TypeSource.TYPE_HINT); } @@ -771,7 +772,7 @@ def foo(param: list[int]): """); var functionDef = (FunctionDef) root.statements().statements().get(0); - var lastExpressionStatement = (ExpressionStatement) functionDef.body().statements().get(functionDef.body().statements().size() -1); + var lastExpressionStatement = (ExpressionStatement) functionDef.body().statements().get(functionDef.body().statements().size() - 1); var type = (ObjectType) lastExpressionStatement.expressions().get(0).typeV2(); assertThat(type.unwrappedType()).isEqualTo(LIST_TYPE); assertThat(type.typeSource()).isEqualTo(TypeSource.TYPE_HINT); @@ -788,7 +789,7 @@ def foo(param: something_unknown): """); var functionDef = (FunctionDef) root.statements().statements().get(0); - var lastExpressionStatement = (ExpressionStatement) functionDef.body().statements().get(functionDef.body().statements().size() -1); + var lastExpressionStatement = (ExpressionStatement) functionDef.body().statements().get(functionDef.body().statements().size() - 1); assertThat(lastExpressionStatement.expressions().get(0).typeV2().unwrappedType()).isEqualTo(PythonType.UNKNOWN); } @@ -800,7 +801,7 @@ def foo(param: something_unknown[int]): """); var functionDef = (FunctionDef) root.statements().statements().get(0); - var lastExpressionStatement = (ExpressionStatement) functionDef.body().statements().get(functionDef.body().statements().size() -1); + var lastExpressionStatement = (ExpressionStatement) functionDef.body().statements().get(functionDef.body().statements().size() - 1); assertThat(lastExpressionStatement.expressions().get(0).typeV2().unwrappedType()).isEqualTo(PythonType.UNKNOWN); } @@ -813,7 +814,7 @@ def foo(param: o): """); var functionDef = (FunctionDef) root.statements().statements().get(1); - var lastExpressionStatement = (ExpressionStatement) functionDef.body().statements().get(functionDef.body().statements().size() -1); + var lastExpressionStatement = (ExpressionStatement) functionDef.body().statements().get(functionDef.body().statements().size() - 1); assertThat(lastExpressionStatement.expressions().get(0).typeV2().unwrappedType()).isEqualTo(PythonType.UNKNOWN); } @@ -825,7 +826,7 @@ def foo(param: int | str): """); var functionDef = (FunctionDef) root.statements().statements().get(0); - var lastExpressionStatement = (ExpressionStatement) functionDef.body().statements().get(functionDef.body().statements().size() -1); + var lastExpressionStatement = (ExpressionStatement) functionDef.body().statements().get(functionDef.body().statements().size() - 1); var type = (UnionType) lastExpressionStatement.expressions().get(0).typeV2(); assertThat(type.candidates()) @@ -1063,19 +1064,19 @@ def foo(): @Test void global_variable() { assertThat(lastExpression(""" - global a - a = 42 - a - """).typeV2().unwrappedType()).isEqualTo(PythonType.UNKNOWN); + global a + a = 42 + a + """).typeV2().unwrappedType()).isEqualTo(PythonType.UNKNOWN); } @Test void global_variable_builtin() { assertThat(lastExpression(""" - global list - list = 42 - list - """).typeV2().unwrappedType()).isEqualTo(PythonType.UNKNOWN); + global list + list = 42 + list + """).typeV2().unwrappedType()).isEqualTo(PythonType.UNKNOWN); } @Test @@ -1102,9 +1103,10 @@ def foo(): x """); var functionDef = (FunctionDef) fileInput.statements().statements().get(0); - var lastExpressionStatement = (ExpressionStatement) functionDef.body().statements().get(functionDef.body().statements().size() -1); + var lastExpressionStatement = (ExpressionStatement) functionDef.body().statements().get(functionDef.body().statements().size() - 1); assertThat(lastExpressionStatement.expressions().get(0).typeV2()).isInstanceOf(UnionType.class); - assertThat(((UnionType) lastExpressionStatement.expressions().get(0).typeV2()).candidates()).extracting(PythonType::unwrappedType).containsExactlyInAnyOrder(INT_TYPE, STR_TYPE); + assertThat(((UnionType) lastExpressionStatement.expressions().get(0).typeV2()).candidates()).extracting(PythonType::unwrappedType).containsExactlyInAnyOrder(INT_TYPE, + STR_TYPE); } @Test @@ -1204,9 +1206,9 @@ void inferTypeForConditionallyReassignedVariables() { void unpacking_assignment() { assertThat(lastExpression( """ - x, = 42, - x - """ + x, = 42, + x + """ ).typeV2()).isEqualTo(PythonType.UNKNOWN); } @@ -1214,9 +1216,9 @@ void unpacking_assignment() { void unpacking_assignment_2() { assertThat(lastExpression( """ - x, y = 42, 43 - x - """ + x, y = 42, 43 + x + """ ).typeV2()).isEqualTo(PythonType.UNKNOWN); } @@ -1224,9 +1226,9 @@ void unpacking_assignment_2() { void multiple_lhs_expressions() { assertThat(lastExpression( """ - x = y = 42 - x - """ + x = y = 42 + x + """ ).typeV2()).isEqualTo(PythonType.UNKNOWN); } @@ -1242,28 +1244,28 @@ void compoundAssignmentStr() { @Test void compoundAssignmentList() { assertThat(lastExpression(""" - a = [] - b = 'world' - a += b - a - """).typeV2().unwrappedType()).isEqualTo(LIST_TYPE); + a = [] + b = 'world' + a += b + a + """).typeV2().unwrappedType()).isEqualTo(LIST_TYPE); } @Test void annotation_with_reassignment() { assertThat(lastExpression(""" - a = "foo" - b: int = a - b - """).typeV2().unwrappedType()).isEqualTo(STR_TYPE); + a = "foo" + b: int = a + b + """).typeV2().unwrappedType()).isEqualTo(STR_TYPE); } @Test void annotation_without_reassignment() { assertThat(lastExpression(""" - a: int - a - """).typeV2().unwrappedType()).isEqualTo(PythonType.UNKNOWN); + a: int + a + """).typeV2().unwrappedType()).isEqualTo(PythonType.UNKNOWN); } @Test @@ -1272,15 +1274,15 @@ void call_expression() { "f()").typeV2()).isEqualTo(PythonType.UNKNOWN); assertThat(lastExpression(""" - def f(): pass - f() - """).typeV2()).isEqualTo(PythonType.UNKNOWN); + def f(): pass + f() + """).typeV2()).isEqualTo(PythonType.UNKNOWN); assertThat(lastExpression( """ - class A: pass - A() - """).typeV2().displayName()).contains("A"); + class A: pass + A() + """).typeV2().displayName()).contains("A"); } @Test @@ -1301,21 +1303,21 @@ def foo(): a void variable_outside_function_3() { assertThat(lastExpression( """ - def foo(): - a = 42 - a - """).typeV2()).isEqualTo(PythonType.UNKNOWN); + def foo(): + a = 42 + a + """).typeV2()).isEqualTo(PythonType.UNKNOWN); } @Test void variable_outside_function_4() { assertThat(lastExpression( """ - a = 42 - def foo(): - a = 'hello' - a - """).typeV2().unwrappedType()).isEqualTo(INT_TYPE); + a = 42 + def foo(): + a = 'hello' + a + """).typeV2().unwrappedType()).isEqualTo(INT_TYPE); } @Test @@ -1432,7 +1434,7 @@ class MyClass: PythonType myClassPythonType = ((ClassDef) fileInput.statements().statements().get(0)).name().typeV2(); List calls = PythonTestUtils.getAllDescendant(fileInput, tree -> tree.is(Tree.Kind.CALL_EXPR)); - for (CallExpression call : calls){ + for (CallExpression call : calls) { PythonType calleeType = call.callee().typeV2(); assertThat(calleeType).isInstanceOf(UnionType.class); assertThat(calleeType.isCompatibleWith(nonePythonType)).isTrue(); @@ -1466,32 +1468,32 @@ void flow_insensitive_when_try_except() { @Test void nested_try_except() { FileInput fileInput = inferTypes(""" - def f(p): - try: - if p: - x = 42 - type(x) - else: - x = "foo" - type(x) - except: - type(x) - def g(p): + def f(p): + try: if p: - y = 42 - type(y) + x = 42 + type(x) else: - y = "hello" - type(y) + x = "foo" + type(x) + except: + type(x) + def g(p): + if p: + y = 42 type(y) - if cond: - z = 42 - type(z) else: - z = "hello" - type(z) + y = "hello" + type(y) + type(y) + if cond: + z = 42 type(z) - """); + else: + z = "hello" + type(z) + type(z) + """); List calls = PythonTestUtils.getAllDescendant(fileInput, tree -> tree.is(Tree.Kind.CALL_EXPR)); RegularArgument firstX = (RegularArgument) calls.get(0).arguments().get(0); RegularArgument secondX = (RegularArgument) calls.get(1).arguments().get(0); @@ -1518,31 +1520,31 @@ def g(p): @Test void nested_try_except_2() { FileInput fileInput = inferTypes(""" - try: - if p: - x = 42 - type(x) - else: - x = "foo" - type(x) - except: + try: + if p: + x = 42 type(x) - def g(p): - if p: - y = 42 - type(y) - else: - y = "hello" - type(y) + else: + x = "foo" + type(x) + except: + type(x) + def g(p): + if p: + y = 42 type(y) - if cond: - z = 42 - type(z) else: - z = "hello" - type(z) + y = "hello" + type(y) + type(y) + if cond: + z = 42 type(z) - """); + else: + z = "hello" + type(z) + type(z) + """); List calls = PythonTestUtils.getAllDescendant(fileInput, tree -> tree.is(Tree.Kind.CALL_EXPR)); RegularArgument firstX = (RegularArgument) calls.get(0).arguments().get(0); RegularArgument secondX = (RegularArgument) calls.get(1).arguments().get(0); @@ -2256,9 +2258,9 @@ def reassigned_param(a, param): void list_comprehension() { assertThat(lastExpression( """ - x = [a for a in foo()] - x - """ + x = [a for a in foo()] + x + """ ).typeV2().unwrappedType()).isEqualTo(LIST_TYPE); } @@ -2266,9 +2268,9 @@ void list_comprehension() { void set_comprehension() { assertThat(lastExpression( """ - x = {a for a in foo()} - x - """ + x = {a for a in foo()} + x + """ ).typeV2().unwrappedType()).isEqualTo(SET_TYPE); } @@ -2276,9 +2278,9 @@ void set_comprehension() { void dict_comprehension() { assertThat(lastExpression( """ - x = {num: num**2 for num in numbers()} - x - """ + x = {num: num**2 for num in numbers()} + x + """ ).typeV2().unwrappedType()).isEqualTo(DICT_TYPE); } @@ -2286,9 +2288,9 @@ void dict_comprehension() { void comprehension_if() { assertThat(lastExpression( """ - x = [num for num in numbers if num % 2 == 0] - x - """ + x = [num for num in numbers if num % 2 == 0] + x + """ ).typeV2().unwrappedType()).isEqualTo(LIST_TYPE); } @@ -2296,9 +2298,9 @@ void comprehension_if() { void generator_expression() { assertThat(lastExpression( """ - x = (num**2 for num in numbers()) - x - """ + x = (num**2 for num in numbers()) + x + """ ).typeV2().unwrappedType()).isEqualTo(PythonType.UNKNOWN); } @@ -2306,10 +2308,10 @@ void generator_expression() { void return_type_of_call_expression_1() { assertThat(lastExpression( """ - x = [1,2,3] - a = x.append(42) - a - """ + x = [1,2,3] + a = x.append(42) + a + """ ).typeV2().unwrappedType()).isEqualTo(NONE_TYPE); } @@ -2317,10 +2319,10 @@ void return_type_of_call_expression_1() { void return_type_of_call_expression_2() { assertThat(lastExpression( """ - x = [1,2,3] - a = x.sort() - a - """ + x = [1,2,3] + a = x.sort() + a + """ ).typeV2().unwrappedType()).isEqualTo(NONE_TYPE); } @@ -2372,12 +2374,14 @@ void imported_ambiguous_symbol() { UnionType acosType = (UnionType) ((ExpressionStatement) fileInput.statements().statements().get(1)).expressions().get(0).typeV2(); assertThat(acosType.candidates()).allMatch(p -> p instanceof FunctionType); assertThat(acosType.candidates()).extracting(PythonType::name).containsExactly("acos", "acos"); - assertThat(acosType.candidates()).map(FunctionType.class::cast).extracting(FunctionType::returnType).extracting(PythonType::unwrappedType).containsExactly(FLOAT_TYPE, FLOAT_TYPE); + assertThat(acosType.candidates()).map(FunctionType.class::cast).extracting(FunctionType::returnType).extracting(PythonType::unwrappedType).containsExactly(FLOAT_TYPE, + FLOAT_TYPE); UnionType atanType = (UnionType) ((ExpressionStatement) fileInput.statements().statements().get(2)).expressions().get(0).typeV2(); assertThat(atanType.candidates()).allMatch(p -> p instanceof FunctionType); assertThat(atanType.candidates()).extracting(PythonType::name).containsExactly("atan", "atan"); - assertThat(atanType.candidates()).map(FunctionType.class::cast).extracting(FunctionType::returnType).extracting(PythonType::unwrappedType).containsExactly(FLOAT_TYPE, FLOAT_TYPE); + assertThat(atanType.candidates()).map(FunctionType.class::cast).extracting(FunctionType::returnType).extracting(PythonType::unwrappedType).containsExactly(FLOAT_TYPE, + FLOAT_TYPE); } @Test @@ -2398,11 +2402,13 @@ void imported_ambiguous_symbol_try_except() { .get(); UnionType acosType1 = (UnionType) acosExpr1.typeV2(); assertThat(acosType1.candidates()).allMatch(p -> p instanceof FunctionType); - assertThat(acosType1.candidates()).map(FunctionType.class::cast).extracting(FunctionType::returnType).extracting(PythonType::unwrappedType).containsExactly(FLOAT_TYPE, FLOAT_TYPE); + assertThat(acosType1.candidates()).map(FunctionType.class::cast).extracting(FunctionType::returnType).extracting(PythonType::unwrappedType).containsExactly(FLOAT_TYPE, + FLOAT_TYPE); UnionType acosType2 = (UnionType) ((ExpressionStatement) fileInput.statements().statements().get(1)).expressions().get(0).typeV2(); assertThat(acosType2.candidates()).allMatch(p -> p instanceof FunctionType); - assertThat(acosType2.candidates()).map(FunctionType.class::cast).extracting(FunctionType::returnType).extracting(PythonType::unwrappedType).containsExactly(FLOAT_TYPE, FLOAT_TYPE); + assertThat(acosType2.candidates()).map(FunctionType.class::cast).extracting(FunctionType::returnType).extracting(PythonType::unwrappedType).containsExactly(FLOAT_TYPE, + FLOAT_TYPE); } @Test @@ -2554,10 +2560,10 @@ void return_type_of_call_expression_inconsistent_3() { void imported_symbol_call_return_type() { assertThat(lastExpression( """ - import fcntl - ret = fcntl.flock(..., ...) - ret - """ + import fcntl + ret = fcntl.flock(..., ...) + ret + """ ).typeV2().unwrappedType()).isEqualTo(NONE_TYPE); } @@ -2565,9 +2571,9 @@ void imported_symbol_call_return_type() { void basic_imported_symbol() { assertThat(lastExpression( """ - import fcntl - fcntl - """ + import fcntl + fcntl + """ ).typeV2()).isInstanceOf(ModuleType.class); } @@ -2686,10 +2692,10 @@ def lock(): void basic_imported_symbols() { FileInput fileInput = inferTypes( """ - import fcntl, math - fcntl - math - """ + import fcntl, math + fcntl + math + """ ); PythonType fnctlModule = ((ExpressionStatement) fileInput.statements().statements().get(1)).expressions().get(0).typeV2(); assertThat(fnctlModule).isInstanceOf(ModuleType.class); @@ -2700,8 +2706,8 @@ void basic_imported_symbols() { assertThat(((UnionType) mathModule.resolveMember("acos").get()).candidates()).allMatch(FunctionType.class::isInstance); } - @Test // TODO SONARPY-2176 ProjectLevelSymbolTable#getType should be able to resolve types when there is a conflict between a member and a subpackage + @Test void import_conflict_between_member_and_submodule() { var statement = lastExpression(""" import opentracing.tracer.Tracer as ottt diff --git a/python-frontend/src/test/java/org/sonar/python/tree/TreeUtilsTest.java b/python-frontend/src/test/java/org/sonar/python/tree/TreeUtilsTest.java index e025a28778..836b288e14 100644 --- a/python-frontend/src/test/java/org/sonar/python/tree/TreeUtilsTest.java +++ b/python-frontend/src/test/java/org/sonar/python/tree/TreeUtilsTest.java @@ -132,8 +132,8 @@ void getSymbolFromTree() { assertThat(TreeUtils.getSymbolFromTree(null)).isEmpty(); Expression expression = lastExpression( - "x = 42", - "x"); + "x = 42", + "x"); assertThat(TreeUtils.getSymbolFromTree(expression)).contains(((HasSymbol) expression).symbol()); expression = lastExpression("foo()"); @@ -152,8 +152,7 @@ void getClassSymbolFromDef() { fileInput = PythonTestUtils.parse( "class A:", " pass", - "A = 42" - ); + "A = 42"); classDef = PythonTestUtils.getLastDescendant(fileInput, t -> t.is(Kind.CLASSDEF)); assertThat(TreeUtils.getClassSymbolFromDef(classDef)).isNull(); } @@ -211,8 +210,7 @@ void getFunctionSymbolFromDef() { fileInput = PythonTestUtils.parse( "def foo():", " pass", - "foo = 42" - ); + "foo = 42"); functionDef = PythonTestUtils.getLastDescendant(fileInput, t -> t.is(Kind.FUNCDEF)); assertThat(TreeUtils.getFunctionSymbolFromDef(functionDef)).isNull(); } @@ -262,8 +260,7 @@ void positionalParameters() { functionDef = PythonTestUtils.getLastDescendant(fileInput, t -> t.is(Kind.FUNCDEF)); List parameters = functionDef.parameters().all(); assertThat(TreeUtils.positionalParameters(functionDef)).isEqualTo( - Arrays.asList(parameters.get(0), parameters.get(2) - )); + Arrays.asList(parameters.get(0), parameters.get(2))); } @Test @@ -275,8 +272,7 @@ void topLevelFunctionDefs() { " if x:", " def bar(self, x): return 1", " else:", - " def baz(self, x, y): return x + y" - ); + " def baz(self, x, y): return x + y"); ClassDef classDef = PythonTestUtils.getLastDescendant(fileInput, t -> t.is(Kind.CLASSDEF)); List functionDefs = PythonTestUtils.getAllDescendant(fileInput, t -> t.is(Kind.FUNCDEF)); @@ -290,8 +286,7 @@ void topLevelFunctionDefs() { " def foo2(x, y): return x + y", " return foo2(1, 1)", " class B:", - " def bar(self): pass" - ); + " def bar(self): pass"); classDef = PythonTestUtils.getFirstChild(fileInput, t -> t.is(Kind.CLASSDEF)); FunctionDef fooDef = PythonTestUtils.getLastDescendant(fileInput, t -> t.is(Kind.FUNCDEF) && ((FunctionDef) t).name().name().equals("foo")); @@ -302,8 +297,7 @@ void topLevelFunctionDefs() { void test_nthArgumentOrKeyword() { FileInput fileInput = PythonTestUtils.parse( "def foo(p0, p1, p2): ...", - "foo(1, 2, p2 = 3)" - ); + "foo(1, 2, p2 = 3)"); CallExpression callExpr = PythonTestUtils.getLastDescendant(fileInput, tree -> tree.is(Kind.CALL_EXPR)); RegularArgument p1 = TreeUtils.nthArgumentOrKeyword(1, "p1", callExpr.arguments()); assertThat(p1.expression().is(Kind.NUMERIC_LITERAL)).isTrue(); @@ -319,8 +313,7 @@ void test_nthArgumentOrKeyword_unpacking() { FileInput fileInput = PythonTestUtils.parse( "def foo(p0, p1, p2): ...", "args = [1, 2, 3]", - "foo(*args)" - ); + "foo(*args)"); CallExpression callExpr = PythonTestUtils.getLastDescendant(fileInput, tree -> tree.is(Kind.CALL_EXPR)); RegularArgument p1 = TreeUtils.nthArgumentOrKeyword(1, "p1", callExpr.arguments()); @@ -331,8 +324,7 @@ void test_nthArgumentOrKeyword_unpacking() { void test_nthArgumentOrKeyword_no_positional() { FileInput fileInput = PythonTestUtils.parse( "def foo(p0, p1 = 2, p2 = 3): ...", - "foo(0, p2 = 4)" - ); + "foo(0, p2 = 4)"); CallExpression callExpr = PythonTestUtils.getLastDescendant(fileInput, tree -> tree.is(Kind.CALL_EXPR)); RegularArgument p1 = TreeUtils.nthArgumentOrKeyword(1, "p1", callExpr.arguments()); @@ -343,8 +335,7 @@ void test_nthArgumentOrKeyword_no_positional() { void test_argumentByKeyword() { FileInput fileInput = PythonTestUtils.parse( "def foo(p0, p1, p2): ...", - "foo(p1 = 1, p2 = 2)" - ); + "foo(p1 = 1, p2 = 2)"); CallExpression callExpr = PythonTestUtils.getLastDescendant(fileInput, tree -> tree.is(Kind.CALL_EXPR)); RegularArgument p1 = TreeUtils.argumentByKeyword("p1", callExpr.arguments()); assertThat(p1.expression().is(Kind.NUMERIC_LITERAL)).isTrue(); @@ -386,22 +377,19 @@ void test_nameFromQualifiedOrCallExpression() { void test_fullyQualifiedNameFromQualifiedExpression() { FileInput fileInput = PythonTestUtils.parse( "from third_party_lib import (element as alias)", - "a = alias.attribute" - ); + "a = alias.attribute"); QualifiedExpression qualifiedExpression = PythonTestUtils.getLastDescendant(fileInput, t -> t.is(Kind.QUALIFIED_EXPR)); assertThat(TreeUtils.fullyQualifiedNameFromQualifiedExpression(qualifiedExpression)).contains("third_party_lib.element.attribute"); fileInput = PythonTestUtils.parse( "from third_party_lib import (element as alias)", - "a = alias().attribute" - ); + "a = alias().attribute"); qualifiedExpression = PythonTestUtils.getLastDescendant(fileInput, t -> t.is(Kind.QUALIFIED_EXPR)); assertThat(TreeUtils.fullyQualifiedNameFromQualifiedExpression(qualifiedExpression)).contains("third_party_lib.element.attribute"); fileInput = PythonTestUtils.parse( "from third_party_lib import (element as alias)", - "a = alias.attr" - ); + "a = alias.attr"); qualifiedExpression = PythonTestUtils.getLastDescendant(fileInput, t -> t.is(Kind.QUALIFIED_EXPR)); assertThat(TreeUtils.fullyQualifiedNameFromQualifiedExpression(qualifiedExpression)).contains("third_party_lib.element.attr"); } @@ -429,8 +417,7 @@ void test_isFunctionWithGivenDecoratorFQN() { @some_module.some_decorator def foo(): ... - """ - ); + """); assertThat(TreeUtils.isFunctionWithGivenDecoratorFQN(fileInput, "some_module.some_decorator")).isFalse(); FunctionDef functionDef = ((FunctionDef) fileInput.statements().statements().get(1)); assertThat(TreeUtils.isFunctionWithGivenDecoratorFQN(functionDef, "some_module.some_decorator")).isTrue(); @@ -477,8 +464,7 @@ void test_toOptionalInstanceOf() { " def foo2(x, y): return x + y", " return foo2(1, 1)", " class B:", - " def bar(self): pass" - ); + " def bar(self): pass"); Tree tree = PythonTestUtils.getFirstChild(fileInput, t -> t.is(Kind.CLASSDEF)); boolean classPresent = TreeUtils.toOptionalInstanceOf(ClassDef.class, tree) @@ -501,8 +487,7 @@ void test_toOptionalInstanceOfMapper() { " def foo2(x, y): return x + y", " return foo2(1, 1)", " class B:", - " def bar(self): pass" - ); + " def bar(self): pass"); Tree tree = PythonTestUtils.getFirstChild(fileInput, t -> t.is(Kind.CLASSDEF)); boolean classPresent = Optional.of(tree) @@ -527,8 +512,7 @@ void test_toInstanceOfMapper() { " def foo2(x, y): return x + y", " return foo2(1, 1)", " class B:", - " def bar(self): pass" - ); + " def bar(self): pass"); Tree tree = PythonTestUtils.getFirstChild(fileInput, t -> t.is(Kind.CLASSDEF)); boolean classPresent = Optional.of(tree) @@ -553,8 +537,7 @@ void test_toStreamInstanceOfMapper() { " def foo2(x, y): return x + y", " return foo2(1, 1)", " class B:", - " def bar(self): pass" - ); + " def bar(self): pass"); Tree tree = PythonTestUtils.getFirstChild(fileInput, t -> t.is(Kind.CLASSDEF)); boolean classPresent = Stream.of(tree) @@ -569,6 +552,7 @@ void test_toStreamInstanceOfMapper() { assertThat(funcDefPresent).isFalse(); } + @Test void test_findIndentationSize() { var fileInput = PythonTestUtils.parse("def foo():\n" + @@ -634,7 +618,7 @@ void test_findIndentationSizeEmpty() { } @Test - void test_firstChild() { + void test_firstChild() { var fileInput = PythonTestUtils.parse( "class A:", " x = True", @@ -642,8 +626,7 @@ void test_firstChild() { " def foo2(x, y): return x + y", " return foo2(1, 1)", " class B:", - " def bar(self): pass" - ); + " def bar(self): pass"); var functionOpt = TreeUtils.firstChild(fileInput, t -> t.is(Kind.FUNCDEF)); assertThat(functionOpt).isPresent();