From a07b4762d02d142e20c23be0a4e03031bf3c2055 Mon Sep 17 00:00:00 2001 From: Andrey Vlasovskikh Date: Wed, 2 Sep 2015 20:19:17 +0300 Subject: [PATCH] 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. --- .../numpy/codeInsight/NumpyDocStringTypeProvider.java | 7 ++++++- .../com/jetbrains/numpy/documentation/NumPyDocString.java | 4 ++-- python/testData/quickdoc/NumPyOnesDoc.html | 2 +- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/python/src/com/jetbrains/numpy/codeInsight/NumpyDocStringTypeProvider.java b/python/src/com/jetbrains/numpy/codeInsight/NumpyDocStringTypeProvider.java index 0406f4694ba2..e63abd21a548 100644 --- a/python/src/com/jetbrains/numpy/codeInsight/NumpyDocStringTypeProvider.java +++ b/python/src/com/jetbrains/numpy/codeInsight/NumpyDocStringTypeProvider.java @@ -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 types = new LinkedHashSet(); + if (withoutOptional != null) { + typeString = withoutOptional; + types.add(PyNoneType.INSTANCE); + } for (String typeName : NumPyDocString.getNumpyUnionType(typeString)) { PyType parsedType = parseSingleNumpyDocType(anchor, typeName); if (parsedType != null) { diff --git a/python/src/com/jetbrains/numpy/documentation/NumPyDocString.java b/python/src/com/jetbrains/numpy/documentation/NumPyDocString.java index 68771828f248..760369d40779 100644 --- a/python/src/com/jetbrains/numpy/documentation/NumPyDocString.java +++ b/python/src/com/jetbrains/numpy/documentation/NumPyDocString.java @@ -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 diff --git a/python/testData/quickdoc/NumPyOnesDoc.html b/python/testData/quickdoc/NumPyOnesDoc.html index 8110ae02e828..edea7b565a85 100644 --- a/python/testData/quickdoc/NumPyOnesDoc.html +++ b/python/testData/quickdoc/NumPyOnesDoc.html @@ -1,2 +1,2 @@ def ones(shape, dtype=None, order='C') -Inferred type: (shape: Union[int, Iterable[int]], dtype: object, order: str) -> ndarray
\ No newline at end of file +Inferred type: (shape: Union[int, Iterable[int]], dtype: Optional[object], order: Optional[str]) -> ndarray
\ No newline at end of file