Parse "foo, optional" as Union[foo, None] as a workaround for parameters with default values (PY-15298)

It results in false negatives for optional parameters with non-None
default values, but at least we don't need to switch from stubs to AST
in the type checker inspection for every function with optional
parameters.
This commit is contained in:
Andrey Vlasovskikh
2015-09-02 20:22:30 +03:00
parent d5d2e6bd68
commit a07b4762d0
3 changed files with 9 additions and 4 deletions
@@ -30,6 +30,7 @@ import com.jetbrains.python.documentation.PyDocumentationSettings;
import com.jetbrains.python.psi.*;
import com.jetbrains.python.psi.impl.PyBuiltinCache;
import com.jetbrains.python.psi.impl.PyExpressionCodeFragmentImpl;
import com.jetbrains.python.psi.types.PyNoneType;
import com.jetbrains.python.psi.types.PyType;
import com.jetbrains.python.psi.types.PyTypeProviderBase;
import com.jetbrains.python.psi.types.TypeEvalContext;
@@ -244,8 +245,12 @@ public class NumpyDocStringTypeProvider extends PyTypeProviderBase {
@Nullable
private static PyType parseNumpyDocType(@NotNull PsiElement anchor, @NotNull String typeString) {
typeString = NumPyDocString.cleanupOptional(typeString);
final String withoutOptional = NumPyDocString.cleanupOptional(typeString);
final Set<PyType> types = new LinkedHashSet<PyType>();
if (withoutOptional != null) {
typeString = withoutOptional;
types.add(PyNoneType.INSTANCE);
}
for (String typeName : NumPyDocString.getNumpyUnionType(typeString)) {
PyType parsedType = parseSingleNumpyDocType(anchor, typeName);
if (parsedType != null) {
@@ -276,13 +276,13 @@ public class NumPyDocString {
}
}
@NotNull
@Nullable
public static String cleanupOptional(@NotNull String typeString) {
int index = typeString.indexOf(", optional");
if (index >= 0) {
return typeString.substring(0, index);
}
return typeString;
return null;
}
@NotNull
+1 -1
View File
@@ -1,2 +1,2 @@
def ones(shape, dtype=None, order='C')
Inferred type: (shape:&nbsp;Union[<a href="psi_element://#typename#int">int</a>,&nbsp;Iterable[<a href="psi_element://#typename#int">int</a>]],&nbsp;dtype:&nbsp;<a href="psi_element://#typename#object">object</a>,&nbsp;order:&nbsp;<a href="psi_element://#typename#str">str</a>)&nbsp;-&gt;&nbsp;ndarray<br>
Inferred type: (shape:&nbsp;Union[<a href="psi_element://#typename#int">int</a>,&nbsp;Iterable[<a href="psi_element://#typename#int">int</a>]],&nbsp;dtype:&nbsp;Optional[<a href="psi_element://#typename#object">object</a>],&nbsp;order:&nbsp;Optional[<a href="psi_element://#typename#str">str</a>])&nbsp;-&gt;&nbsp;ndarray<br>