PY-23055 Fixed: Default value of None is not shown in the parameter info popup

Show `None` default value when type was not inferred.
This commit is contained in:
Semyon Proshev
2017-03-14 14:39:05 +03:00
parent 636abcd2e3
commit e8aeb6b502
3 changed files with 22 additions and 10 deletions

View File

@@ -33,9 +33,9 @@ import com.jetbrains.python.PyElementTypes;
import com.jetbrains.python.PyNames;
import com.jetbrains.python.PyTokenTypes;
import com.jetbrains.python.PythonDialectsTokenSetProvider;
import com.jetbrains.python.codeInsight.typing.PyTypingTypeProvider;
import com.jetbrains.python.codeInsight.controlflow.ScopeOwner;
import com.jetbrains.python.codeInsight.dataflow.scope.ScopeUtil;
import com.jetbrains.python.codeInsight.typing.PyTypingTypeProvider;
import com.jetbrains.python.documentation.PythonDocumentationProvider;
import com.jetbrains.python.psi.*;
import com.jetbrains.python.psi.resolve.PyResolveContext;
@@ -179,16 +179,14 @@ public class PyNamedParameterImpl extends PyBaseElementImpl<PyNamedParameterStub
sb.append(getName());
if (context != null) {
final PyType argumentType = getArgumentType(context);
if (argumentType != null) {
sb.append(": ");
sb.append(PythonDocumentationProvider.getTypeDescription(argumentType, context));
}
final PyType argumentType = context == null ? null : getArgumentType(context);
if (argumentType != null) {
sb.append(": ");
sb.append(PythonDocumentationProvider.getTypeDescription(argumentType, context));
}
final PyExpression defaultValue = getDefaultValue();
if (defaultValueShouldBeIncluded(includeDefaultValue, defaultValue)) {
if (defaultValueShouldBeIncluded(includeDefaultValue, defaultValue, argumentType)) {
String representation = PyUtil.getReadableRepr(defaultValue, true);
if (defaultValue instanceof PyStringLiteralExpression) {
final Pair<String, String> quotes = PyStringLiteralUtil.getQuotes(defaultValue.getText());
@@ -202,11 +200,13 @@ public class PyNamedParameterImpl extends PyBaseElementImpl<PyNamedParameterStub
return sb.toString();
}
private static boolean defaultValueShouldBeIncluded(boolean includeDefaultValue, @Nullable PyExpression defaultValue) {
private static boolean defaultValueShouldBeIncluded(boolean includeDefaultValue,
@Nullable PyExpression defaultValue,
@Nullable PyType type) {
if (!includeDefaultValue || defaultValue == null) return false;
// In case of `None` default value, it will be listed in the type as `Optional[...]` or `Union[..., None, ...]`
return !PyNames.NONE.equals(defaultValue.getText());
return type == null || !PyNames.NONE.equals(defaultValue.getText());
}
@Override

View File

@@ -0,0 +1,4 @@
def foo(b=None):
pass
foo(<arg1>)

View File

@@ -452,6 +452,14 @@ public class PyParameterInfoTest extends LightMarkedTestCase {
);
}
// PY-23055
public void testWithoutTypeButWithNoneDefaultValue() {
final int offset = loadTest(1).get("<arg1>").getTextOffset();
final String expectedInfo = "b=None";
feignCtrlP(offset).check(expectedInfo, new String[]{"b=None"});
}
// PY-22004
public void testMultiResolved() {
myFixture.copyDirectoryToProject("typing", "");