mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
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:
@@ -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,2 +1,2 @@
|
||||
def ones(shape, dtype=None, order='C')
|
||||
Inferred type: (shape: Union[<a href="psi_element://#typename#int">int</a>, Iterable[<a href="psi_element://#typename#int">int</a>]], dtype: <a href="psi_element://#typename#object">object</a>, order: <a href="psi_element://#typename#str">str</a>) -> ndarray<br>
|
||||
Inferred type: (shape: Union[<a href="psi_element://#typename#int">int</a>, Iterable[<a href="psi_element://#typename#int">int</a>]], dtype: Optional[<a href="psi_element://#typename#object">object</a>], order: Optional[<a href="psi_element://#typename#str">str</a>]) -> ndarray<br>
|
||||
Reference in New Issue
Block a user