Stop at PyStatements when looking for a containing type hint

GitOrigin-RevId: d12b04002c8b0a495e59592fe8e60da863d4a3a5
This commit is contained in:
Mikhail Golubev
2021-09-17 19:57:28 +03:00
committed by intellij-monorepo-bot
parent 61c123049b
commit 3cfef388ae
2 changed files with 5 additions and 5 deletions

View File

@@ -786,7 +786,7 @@ public class PyTypingTypeProvider extends PyTypeProviderBase {
PsiFile file = element.getContainingFile();
if (file instanceof PyFile && ((PyFile)file).hasImportFromFuture(FutureFeature.ANNOTATIONS)) {
return file == element || PsiTreeUtil.getParentOfType(element, PyAnnotation.class, false, ScopeOwner.class) != null;
return file == element || PsiTreeUtil.getParentOfType(element, PyAnnotation.class, false, PyStatement.class) != null;
}
return false;
@@ -1729,16 +1729,16 @@ public class PyTypingTypeProvider extends PyTypeProviderBase {
public static boolean isInsideTypeHint(@NotNull PsiElement element, @NotNull TypeEvalContext context) {
final PsiElement realContext = PyPsiUtils.getRealContext(element);
if (PsiTreeUtil.getParentOfType(realContext, PyAnnotation.class, false, ScopeOwner.class) != null) {
if (PsiTreeUtil.getParentOfType(realContext, PyAnnotation.class, false, PyStatement.class) != null) {
return true;
}
final PsiComment comment = PsiTreeUtil.getParentOfType(realContext, PsiComment.class, false, ScopeOwner.class);
final PsiComment comment = PsiTreeUtil.getParentOfType(realContext, PsiComment.class, false, PyStatement.class);
if (comment != null && getTypeCommentValue(comment.getText()) != null) {
return true;
}
PyAssignmentStatement assignment = PsiTreeUtil.getParentOfType(realContext, PyAssignmentStatement.class);
PyAssignmentStatement assignment = PsiTreeUtil.getParentOfType(realContext, PyAssignmentStatement.class, false, PyStatement.class);
if (assignment != null &&
PsiTreeUtil.isAncestor(assignment.getAssignedValue(), realContext, false) &&
isExplicitTypeAlias(assignment, context)) {

View File

@@ -695,7 +695,7 @@ public abstract class CompatibilityVisitor extends PyAnnotator {
if (node.getOperator() != PyTokenTypes.OR) return;
final PsiFile file = node.getContainingFile();
final boolean isInAnnotation = PsiTreeUtil.getParentOfType(node, PyAnnotation.class, false, ScopeOwner.class) != null;
final boolean isInAnnotation = PsiTreeUtil.getParentOfType(node, PyAnnotation.class, false, PyStatement.class) != null;
if (file == null ||
file instanceof PyFile &&
((PyFile)file).hasImportFromFuture(FutureFeature.ANNOTATIONS) &&