mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
Show tuple types as 'Tuple[T1, T2, ..., TN]' as in PEP 484 (PY-15206)
This commit is contained in:
@@ -126,6 +126,19 @@ public class PyTypeModelBuilder {
|
||||
}
|
||||
}
|
||||
|
||||
static class TupleType extends TypeModel {
|
||||
private final List<TypeModel> members;
|
||||
|
||||
public TupleType(List<TypeModel> members) {
|
||||
this.members = members;
|
||||
}
|
||||
|
||||
@Override
|
||||
void accept(TypeVisitor visitor) {
|
||||
visitor.tuple(this);
|
||||
}
|
||||
}
|
||||
|
||||
private static TypeModel _(String name) {
|
||||
return new NamedType(name);
|
||||
}
|
||||
@@ -228,6 +241,15 @@ public class PyTypeModelBuilder {
|
||||
else if (type instanceof PyCallableType && !(type instanceof PyClassLikeType)) {
|
||||
result = build((PyCallableType)type);
|
||||
}
|
||||
else if (type instanceof PyTupleType) {
|
||||
final List<TypeModel> elementModels = new ArrayList<TypeModel>();
|
||||
final PyTupleType tupleType = (PyTupleType)type;
|
||||
for (int i = 0; i < tupleType.getElementCount(); i++) {
|
||||
final PyType elementType = tupleType.getElementType(i);
|
||||
elementModels.add(build(elementType, true));
|
||||
}
|
||||
result = new TupleType(elementModels);
|
||||
}
|
||||
if (result == null) {
|
||||
result = type != null ? _(type.getName()) : _(PyNames.UNKNOWN_TYPE);
|
||||
}
|
||||
@@ -284,6 +306,8 @@ public class PyTypeModelBuilder {
|
||||
void unknown(UnknownType type);
|
||||
|
||||
void optional(OptionalType type);
|
||||
|
||||
void tuple(TupleType type);
|
||||
}
|
||||
|
||||
private static class TypeToStringVisitor extends TypeNameVisitor {
|
||||
@@ -446,5 +470,12 @@ public class PyTypeModelBuilder {
|
||||
type.type.accept(this);
|
||||
add("]");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void tuple(TupleType type) {
|
||||
add("Tuple[");
|
||||
processList(type.members, ", ");
|
||||
add("]");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,4 +43,4 @@ def test():
|
||||
f2(1, <weak_warning descr="Expected type 'list[int]' (matched generic type 'list[T]'), got 'list[str]' instead">['foo']</weak_warning>, 'bar')
|
||||
|
||||
result = f3(1, 'foo', True)
|
||||
f4(<warning descr="Expected type '(bool, int, str)', got '(int, str, bool)' instead">result</warning>)
|
||||
f4(<warning descr="Expected type 'Tuple[bool, int, str]', got 'Tuple[int, str, bool]' instead">result</warning>)
|
||||
|
||||
@@ -8,4 +8,4 @@ def f(spam, eggs):
|
||||
|
||||
def test():
|
||||
f(<warning descr="Expected type 'list[Union[str, unicode]]', got 'list[int]' instead">[1, 2, 3]</warning>,
|
||||
(<warning descr="Expected type '(bool, int, unicode)', got '(bool, int, str)' instead">False, 2, ''</warning>))
|
||||
(<warning descr="Expected type 'Tuple[bool, int, unicode]', got 'Tuple[bool, int, str]' instead">False, 2, ''</warning>))
|
||||
|
||||
@@ -845,12 +845,12 @@ public class PyTypeTest extends PyTestCase {
|
||||
|
||||
// PY-12801
|
||||
public void testTupleConcatenation() {
|
||||
doTest("(int, bool, str)",
|
||||
doTest("Tuple[int, bool, str]",
|
||||
"expr = (1,) + (True, 'spam') + ()");
|
||||
}
|
||||
|
||||
public void testTupleMultiplication() {
|
||||
doTest("(int, bool, int, bool)",
|
||||
doTest("Tuple[int, bool, int, bool]",
|
||||
"expr = (1, False) * 2");
|
||||
}
|
||||
|
||||
|
||||
@@ -124,7 +124,7 @@ public class PyTypingTest extends PyTestCase {
|
||||
}
|
||||
|
||||
public void testBuiltinTupleWithParameters() {
|
||||
doTest("(int, str)",
|
||||
doTest("Tuple[int, str]",
|
||||
"from typing import Tuple\n" +
|
||||
"\n" +
|
||||
"def f(expr: Tuple[int, str]):\n" +
|
||||
|
||||
Reference in New Issue
Block a user