Search in sources :

Example 1 with TypeAnnotation

use of org.sonar.plugins.python.api.tree.TypeAnnotation in project sonar-python by SonarSource.

the class InferredTypesTest method test_text_annotation.

@Test
public void test_text_annotation() {
    TypeAnnotation typeAnnotation = typeAnnotation("from typing import Text", "l : Text");
    assertThat(fromTypeshedTypeAnnotation(typeAnnotation)).isEqualTo(STR);
    InferredType declaredType = fromTypeAnnotation(typeAnnotation);
    assertThat(declaredType).isInstanceOf(DeclaredType.class);
    assertThat(((DeclaredType) declaredType).alternativeTypeSymbols()).extracting(Symbol::fullyQualifiedName).containsExactlyInAnyOrder("str");
}
Also used : InferredTypes.fromTypeshedTypeAnnotation(org.sonar.python.types.InferredTypes.fromTypeshedTypeAnnotation) InferredTypes.fromTypeAnnotation(org.sonar.python.types.InferredTypes.fromTypeAnnotation) TypeAnnotation(org.sonar.plugins.python.api.tree.TypeAnnotation) InferredType(org.sonar.plugins.python.api.types.InferredType) Test(org.junit.Test)

Example 2 with TypeAnnotation

use of org.sonar.plugins.python.api.tree.TypeAnnotation in project sonar-python by SonarSource.

the class InferredTypesTest method test_none_annotation.

@Test
public void test_none_annotation() {
    TypeAnnotation typeAnnotation = typeAnnotation("l : None");
    assertThat(fromTypeshedTypeAnnotation(typeAnnotation)).isEqualTo(NONE);
    InferredType declaredType = fromTypeAnnotation(typeAnnotation);
    assertThat(declaredType).isInstanceOf(DeclaredType.class);
    assertThat(((DeclaredType) declaredType).alternativeTypeSymbols()).extracting(Symbol::fullyQualifiedName).containsExactlyInAnyOrder("NoneType");
}
Also used : InferredTypes.fromTypeshedTypeAnnotation(org.sonar.python.types.InferredTypes.fromTypeshedTypeAnnotation) InferredTypes.fromTypeAnnotation(org.sonar.python.types.InferredTypes.fromTypeAnnotation) TypeAnnotation(org.sonar.plugins.python.api.tree.TypeAnnotation) InferredType(org.sonar.plugins.python.api.types.InferredType) Test(org.junit.Test)

Example 3 with TypeAnnotation

use of org.sonar.plugins.python.api.tree.TypeAnnotation in project sonar-python by SonarSource.

the class InferredTypesTest method test_union_type_annotations.

@Test
public void test_union_type_annotations() {
    TypeAnnotation typeAnnotation = typeAnnotation("from typing import Union", "l : Union[int, str]");
    assertThat(fromTypeshedTypeAnnotation(typeAnnotation)).isEqualTo(InferredTypes.or(InferredTypes.INT, InferredTypes.STR));
    InferredType declaredType = fromTypeAnnotation(typeAnnotation);
    assertThat(declaredType).isInstanceOf(DeclaredType.class);
    assertThat(((DeclaredType) declaredType).alternativeTypeSymbols()).extracting(Symbol::fullyQualifiedName).containsExactlyInAnyOrder("int", "str");
    typeAnnotation = typeAnnotation("from typing import Union", "l : Union[int, str, bool]");
    assertThat(fromTypeshedTypeAnnotation(typeAnnotation)).isEqualTo(InferredTypes.or(InferredTypes.or(InferredTypes.INT, InferredTypes.STR), InferredTypes.BOOL));
    declaredType = fromTypeAnnotation(typeAnnotation);
    assertThat(declaredType).isInstanceOf(DeclaredType.class);
    assertThat(((DeclaredType) declaredType).alternativeTypeSymbols()).extracting(Symbol::fullyQualifiedName).containsExactlyInAnyOrder("int", "str", "bool");
    typeAnnotation = typeAnnotation("from typing import Union", "l : Union[Union[int, str], bool]");
    assertThat(fromTypeshedTypeAnnotation(typeAnnotation)).isEqualTo(InferredTypes.or(InferredTypes.or(InferredTypes.INT, InferredTypes.STR), InferredTypes.BOOL));
    declaredType = fromTypeAnnotation(typeAnnotation);
    assertThat(declaredType).isInstanceOf(DeclaredType.class);
    assertThat(((DeclaredType) declaredType).alternativeTypeSymbols()).extracting(Symbol::fullyQualifiedName).containsExactlyInAnyOrder("int", "str", "bool");
    typeAnnotation = typeAnnotation("from typing import Union", "l : Union[bool]");
    assertThat(fromTypeshedTypeAnnotation(typeAnnotation)).isEqualTo(InferredTypes.BOOL);
    declaredType = fromTypeAnnotation(typeAnnotation);
    assertThat(declaredType).isInstanceOf(DeclaredType.class);
    assertThat(((DeclaredType) declaredType).alternativeTypeSymbols()).extracting(Symbol::fullyQualifiedName).containsExactlyInAnyOrder("bool");
}
Also used : InferredTypes.fromTypeshedTypeAnnotation(org.sonar.python.types.InferredTypes.fromTypeshedTypeAnnotation) InferredTypes.fromTypeAnnotation(org.sonar.python.types.InferredTypes.fromTypeAnnotation) TypeAnnotation(org.sonar.plugins.python.api.tree.TypeAnnotation) InferredType(org.sonar.plugins.python.api.types.InferredType) Test(org.junit.Test)

Example 4 with TypeAnnotation

use of org.sonar.plugins.python.api.tree.TypeAnnotation in project sonar-python by SonarSource.

the class InferredTypesTest method test_optional_type_annotations.

@Test
public void test_optional_type_annotations() {
    TypeAnnotation typeAnnotation = typeAnnotation("from typing import Optional", "l : Optional[int]");
    assertThat(fromTypeshedTypeAnnotation(typeAnnotation)).isEqualTo(InferredTypes.or(InferredTypes.INT, NONE));
    InferredType declaredType = fromTypeAnnotation(typeAnnotation);
    assertThat(declaredType).isInstanceOf(DeclaredType.class);
    assertThat(((DeclaredType) declaredType).alternativeTypeSymbols()).extracting(Symbol::fullyQualifiedName).containsExactlyInAnyOrder("NoneType", "int");
    typeAnnotation = typeAnnotation("from typing import Optional", "l : Optional[int, str]");
    assertThat(fromTypeshedTypeAnnotation(typeAnnotation)).isEqualTo(InferredTypes.anyType());
    declaredType = fromTypeAnnotation(typeAnnotation);
    assertThat(declaredType).isInstanceOf(DeclaredType.class);
    assertThat(((DeclaredType) declaredType).alternativeTypeSymbols()).extracting(Symbol::fullyQualifiedName).containsExactlyInAnyOrder("typing.Optional");
    typeAnnotation = typeAnnotation("from typing import Optional", "l : Optional[int, unknown_symbol]");
    assertThat(fromTypeshedTypeAnnotation(typeAnnotation)).isEqualTo(InferredTypes.anyType());
    declaredType = fromTypeAnnotation(typeAnnotation);
    assertThat(declaredType).isInstanceOf(DeclaredType.class);
    assertThat(((DeclaredType) declaredType).alternativeTypeSymbols()).extracting(Symbol::fullyQualifiedName).containsExactlyInAnyOrder("typing.Optional");
}
Also used : InferredTypes.fromTypeshedTypeAnnotation(org.sonar.python.types.InferredTypes.fromTypeshedTypeAnnotation) InferredTypes.fromTypeAnnotation(org.sonar.python.types.InferredTypes.fromTypeAnnotation) TypeAnnotation(org.sonar.plugins.python.api.tree.TypeAnnotation) InferredType(org.sonar.plugins.python.api.types.InferredType) Test(org.junit.Test)

Example 5 with TypeAnnotation

use of org.sonar.plugins.python.api.tree.TypeAnnotation in project sonar-python by SonarSource.

the class InferredTypesTest method assertAliasedTypeAnnotation.

private void assertAliasedTypeAnnotation(String type, String... code) {
    TypeAnnotation typeAnnotation = typeAnnotation(code);
    ClassSymbol typeClass = typeShedClass(type);
    assertThat(fromTypeshedTypeAnnotation(typeAnnotation)).isEqualTo(runtimeType(typeClass));
    assertThat(typeSymbols(fromTypeAnnotation(typeAnnotation))).containsExactly(typeClass);
}
Also used : InferredTypes.fromTypeshedTypeAnnotation(org.sonar.python.types.InferredTypes.fromTypeshedTypeAnnotation) InferredTypes.fromTypeAnnotation(org.sonar.python.types.InferredTypes.fromTypeAnnotation) TypeAnnotation(org.sonar.plugins.python.api.tree.TypeAnnotation) ClassSymbol(org.sonar.plugins.python.api.symbols.ClassSymbol)

Aggregations

TypeAnnotation (org.sonar.plugins.python.api.tree.TypeAnnotation)13 InferredTypes.fromTypeAnnotation (org.sonar.python.types.InferredTypes.fromTypeAnnotation)8 InferredTypes.fromTypeshedTypeAnnotation (org.sonar.python.types.InferredTypes.fromTypeshedTypeAnnotation)8 Test (org.junit.Test)7 InferredType (org.sonar.plugins.python.api.types.InferredType)6 Token (org.sonar.plugins.python.api.tree.Token)4 AstNode (com.sonar.sslr.api.AstNode)3 AliasedName (org.sonar.plugins.python.api.tree.AliasedName)3 ImportName (org.sonar.plugins.python.api.tree.ImportName)3 Name (org.sonar.plugins.python.api.tree.Name)3 AnyParameter (org.sonar.plugins.python.api.tree.AnyParameter)2 AssignmentExpression (org.sonar.plugins.python.api.tree.AssignmentExpression)2 ComprehensionExpression (org.sonar.plugins.python.api.tree.ComprehensionExpression)2 ConditionalExpression (org.sonar.plugins.python.api.tree.ConditionalExpression)2 Decorator (org.sonar.plugins.python.api.tree.Decorator)2 DottedName (org.sonar.plugins.python.api.tree.DottedName)2 Expression (org.sonar.plugins.python.api.tree.Expression)2 FormattedExpression (org.sonar.plugins.python.api.tree.FormattedExpression)2 LambdaExpression (org.sonar.plugins.python.api.tree.LambdaExpression)2 QualifiedExpression (org.sonar.plugins.python.api.tree.QualifiedExpression)2