PyClass (@NotNull ScopeOwner referenceOwner, @NotNull ScopeOwner resolvedOwner) { PyClass current = PyUtil.as(referenceOwner, PyClass.class); ScopeOwner outer = ScopeUtil.getScopeOwner(current); while (outer != resolvedOwner) { current = PyUtil.as(outer, PyClass.class); if (current == null) return null; outer = ScopeUtil.getScopeOwner(outer); } return current; }