PY-18595 Use annotation from type comment for the first parameter of static methods

This commit is contained in:
Mikhail Golubev
2016-03-03 14:52:12 +03:00
parent e1886cd261
commit f62b11ea9f
2 changed files with 16 additions and 1 deletions
@@ -97,7 +97,7 @@ public class PyTypingTypeProvider extends PyTypeProviderBase {
final List<PyCallableParameter> paramTypes = functionType.getParameters(context);
assert paramTypes != null;
final PyParameter[] funcParams = func.getParameterList().getParameters();
final int startOffset = func.getContainingClass() != null ? 1 : 0;
final int startOffset = omitFirstParamInTypeComment(func) ? 1 : 0;
for (int paramIndex = 0; paramIndex < funcParams.length; paramIndex++) {
if (funcParams[paramIndex] == param) {
final int typeIndex = paramIndex - startOffset;
@@ -112,6 +112,10 @@ public class PyTypingTypeProvider extends PyTypeProviderBase {
return null;
}
private static boolean omitFirstParamInTypeComment(@NotNull PyFunction func) {
return func.getContainingClass() != null && func.getModifier() != PyFunction.Modifier.STATICMETHOD;
}
@Nullable
@Override
public Ref<PyType> getReturnType(@NotNull PyCallable callable, @NotNull TypeEvalContext context) {
@@ -461,6 +461,17 @@ public class PyTypingTest extends PyTestCase {
"expr = f");
}
// PY-18595
public void testFunctionTypeCommentForStaticMethod() {
doTest("int",
"class C:\n" +
" @staticmethod\n" +
" def m(some_int, some_bool, some_str):\n" +
" # type: (int, bool, str) -> bool\n" +
" expr = some_int");
}
private void doTestNoInjectedText(@NotNull String text) {
myFixture.configureByText(PythonFileType.INSTANCE, text);
final InjectedLanguageManager languageManager = InjectedLanguageManager.getInstance(myFixture.getProject());