PyClassPattern () { PsiElement grandParent = myElement.getParent().getParent(); if (myElement.getParent() instanceof PyCapturePattern && grandParent instanceof PyPatternArgumentList) { return as(grandParent.getParent(), PyClassPattern.class); } return null; }