Try to get parameter type from annotations/docstrings, then check if it is 'self' (PY-8953)

This commit is contained in:
Andrey Vlasovskikh
2013-09-26 19:37:44 +04:00
parent 872e6978be
commit feea24c68c
2 changed files with 32 additions and 22 deletions
@@ -165,7 +165,26 @@ public class PyNamedParameterImpl extends PyPresentableElementImpl<PyNamedParame
PyParameterList parameterList = (PyParameterList)parent;
PyFunction func = parameterList.getContainingFunction();
if (func != null) {
final PyFunction.Modifier modifier = func.getModifier();
PyAnnotation anno = getAnnotation();
if (anno != null) {
final PyClass pyClass = anno.resolveToClass();
if (pyClass != null) {
return new PyClassTypeImpl(pyClass, false);
}
}
StructuredDocString docString = func.getStructuredDocString();
if (PyNames.INIT.equals(func.getName()) && docString == null) {
PyClass pyClass = func.getContainingClass();
if (pyClass != null) {
docString = pyClass.getStructuredDocString();
}
}
if (docString != null) {
String typeName = docString.getParamType(getName());
if (typeName != null) {
return PyTypeParser.getTypeByName(this, typeName);
}
}
if (isSelf()) {
// must be 'self' or 'cls'
final PyClass containingClass = func.getContainingClass();
@@ -190,6 +209,7 @@ public class PyNamedParameterImpl extends PyPresentableElementImpl<PyNamedParame
if (initType != null && !(initType instanceof PyNoneType)) {
return initType;
}
final PyFunction.Modifier modifier = func.getModifier();
return new PyClassTypeImpl(containingClass, modifier == PyFunction.Modifier.CLASSMETHOD);
}
}
@@ -199,27 +219,6 @@ public class PyNamedParameterImpl extends PyPresentableElementImpl<PyNamedParame
if (isPositionalContainer()) {
return PyBuiltinCache.getInstance(this).getTupleType();
}
PyAnnotation anno = getAnnotation();
if (anno != null) {
final PyClass pyClass = anno.resolveToClass();
if (pyClass != null) {
return new PyClassTypeImpl(pyClass, false);
}
}
StructuredDocString docString = func.getStructuredDocString();
if (PyNames.INIT.equals(func.getName()) && docString == null) {
PyClass pyClass = func.getContainingClass();
if (pyClass != null) {
docString = pyClass.getStructuredDocString();
}
}
if (docString != null) {
String typeName = docString.getParamType(getName());
if (typeName != null) {
return PyTypeParser.getTypeByName(this, typeName);
}
}
for(PyTypeProvider provider: Extensions.getExtensions(PyTypeProvider.EP_NAME)) {
PyType result = provider.getParameterType(this, func, context);
if (result != null) return result;
@@ -760,6 +760,17 @@ public class PyTypeTest extends PyTestCase {
" print(expr)\n");
}
// PY-8953
public void testSelfInDocString() {
doTest("int",
"class C(object):\n" +
" def foo(self):\n" +
" '''\n" +
" :type self: int\n" +
" '''\n" +
" expr = self\n");
}
private static TypeEvalContext getTypeEvalContext(@NotNull PyExpression element) {
return TypeEvalContext.userInitiated(element.getContainingFile()).withTracing();
}