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");
}
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");
}
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");
}
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");
}
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);
}
Aggregations