Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ public class InferredTypes {
ALIASED_ANNOTATIONS.put("typing.Dict", BuiltinTypes.DICT);
ALIASED_ANNOTATIONS.put("typing.Set", BuiltinTypes.SET);
ALIASED_ANNOTATIONS.put("typing.FrozenSet", "frozenset");
ALIASED_ANNOTATIONS.put("typing.Deque", "deque");
ALIASED_ANNOTATIONS.put("typing.DefaultDict", "defaultdict");
ALIASED_ANNOTATIONS.put("typing.Type", "type");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.sonar.plugins.python.api.symbols.Symbol;
import org.sonar.plugins.python.api.tree.Tree;
import org.sonar.plugins.python.api.tree.TypeAnnotation;
import org.sonar.plugins.python.api.types.BuiltinTypes;
import org.sonar.plugins.python.api.types.InferredType;
import org.sonar.python.PythonTestUtils;
import org.sonar.python.semantic.AmbiguousSymbolImpl;
Expand All @@ -41,6 +42,8 @@
import static org.sonar.python.types.InferredTypes.fromTypeshedTypeAnnotation;
import static org.sonar.python.types.InferredTypes.or;
import static org.sonar.python.types.InferredTypes.runtimeType;
import static org.sonar.python.types.InferredTypes.typeName;
import static org.sonar.python.types.InferredTypes.typeSymbols;
import static org.sonar.python.types.TypeShed.typeShedClass;

public class InferredTypesTest {
Expand Down Expand Up @@ -92,17 +95,55 @@ public void ambiguous_class_symbol() {

@Test
public void test_aliased_type_annotations() {
TypeAnnotation typeAnnotation = typeAnnotation(
assertAliasedTypeAnnotation(BuiltinTypes.LIST,
"from typing import List",
"l : List[int]"
"l : List[int]");

assertAliasedTypeAnnotation(BuiltinTypes.TUPLE,
"from typing import Tuple",
"l : Tuple[int]"
);
assertThat(fromTypeshedTypeAnnotation(typeAnnotation)).isEqualTo(InferredTypes.LIST);

typeAnnotation = typeAnnotation(
assertAliasedTypeAnnotation(BuiltinTypes.DICT,
"from typing import Dict",
"l : Dict[int, string]"
"l : Dict[int, str]"
);

assertAliasedTypeAnnotation(BuiltinTypes.SET,
"from typing import Set",
"l : Set[int]"
);

assertAliasedTypeAnnotation("frozenset",
"from typing import FrozenSet",
"l : FrozenSet[int]"
);

assertAliasedTypeAnnotation("type",
"from typing import Type",
"l : Type[int]"
);
assertThat(fromTypeshedTypeAnnotation(typeAnnotation)).isEqualTo(InferredTypes.DICT);

TypeAnnotation typeAnnotation = typeAnnotation(
"from typing import DefaultDict",
"l : DefaultDict[int, str]"
);
InferredType type = fromTypeAnnotation(typeAnnotation);
assertThat(typeName(type)).isEqualTo("DefaultDict[int, str]");

typeAnnotation = typeAnnotation(
"from typing import Deque",
"l : Deque[int]"
);
type = fromTypeAnnotation(typeAnnotation);
assertThat(typeName(type)).isEqualTo("Deque[int]");
}

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

@Test
Expand Down